[root@centos7s python]# cat linuxs.py
#!/usr/bin/python
#coding:utf-8
from subprocess import popen,pipe
import re
import os,sys
import paramiko
reload(sys)
sys.setdefaultencoding('utf-8')
def session(host,uname,pwd):
try:
ssh = paramiko.sshclient()
ssh.set_missing_host_key_policy(paramiko.autoaddpolicy())
ssh.connect(host,username=uname, password=pwd)
print "login %s is successful" % host
return ssh
except exception as e:
print e.message
def get_hostname(host,uname,pwd):
cmd_hostname = "hostname"
client = session(host,uname,pwd)
stdin,stdout,stderr=client.exec_command(cmd_hostname)
hostname = stdout.read()
return hostname
#獲取linux網路ipv4資訊
def get_ifconfig(host,uname,pwd):
client = session(host,uname,pwd)
stdin, stdout, stderr = client.exec_command("ifconfig")
data = stdout.read()
#ret = re.compile('((?:1[0-9][0-9]\.)|(?:25[0-5]\.)|(?:2[0-4][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.))((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))')
ret = re.compile('(?:19[0-9]\.)((?:1[0-9][0-9]\.)|(?:25[0-5]\.)|(?:2[0-4][0-9]\.)|(?:[1-9][0-9]\.)|(?:[0-9]\.))((1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([0-9]))')
match = ret.search(data).group()
return match
#獲取linux系統版本資訊
def get_version(host,uname,pwd):
client = session(host,uname,pwd)
stdin, stdout, stderr = client.exec_command("cat /etc/redhat-release")
data = stdout.read()
return data
#獲取linux系統cpu資訊
def get_cpu(host,uname,pwd):
cpunum = 0
processor = 0
client = session(host,uname,pwd)
stdin, stdout, stderr = client.exec_command("cat /proc/cpuinfo")
cpuinfo = stdout.readlines()
#with stdout.read() as cpuinfo:
for i in cpuinfo:
if i.startswith('physical id'):
cpunum = i.split(":")[1]
if i.startswith('processor'):
processor = processor + 1
if i.startswith('model name'):
cpumode = i.split(":")[1]
return int(cpunum)+1, processor,cpumode
#獲取linux系統memory資訊
def get_memory(host,uname,pwd):
client = session(host,uname,pwd)
stdin, stdout, stderr = client.exec_command("cat /proc/meminfo")
meminfo = stdout.readlines()
#with open('/proc/meminfo') as meminfo:
for i in meminfo:
if i.startswith('memtotal'):
memory = int(i.split()[1].strip())
memory = '%.f' %(memory / 1024.0) + 'mb'
else:
pass
return memory
#獲取linux系統網絡卡資訊
def get_ethernet(host,uname,pwd):
client = session(host,uname,pwd)
stdin, stdout, stderr = client.exec_command("lspci")
data = stdout.read()
ret = re.compile('eth[^\d].*')
eth = ret.search(data).group()
return eth
if __name__ == '__main__':
host=sys.argv[1]
uname=sys.argv[2]
pwd=sys.argv[3]
port=22
#host = raw_input("please input the hostname: ")
#result = getlinuxmessage()
result1 = get_hostname(host,uname,pwd)
print ('主機名:%s' %result1)
result2 = get_ifconfig(host,uname,pwd)
print ('主機ip:%s' %result2)
result3 = get_version(host,uname,pwd)
print ('版本資訊:%s' %result3)
result4,result5,result6 = get_cpu(host,uname,pwd)
print ('物理cpu數量:%s' %result4)
print ('邏輯cpu數量:%s' %result5)
print ('物理cpu型號:%s' %result6)
result7 = get_memory(host,uname,pwd)
print ('物理記憶體:%s' %result7)
result8 = get_ethernet(host,uname,pwd)
print ('網絡卡型號:%s' %result8)
執行結果:
[root@centos7s python]# ./linuxs.py '193.168.120.77' 'root' 'pwd'
login 193.168.120.77 is successful
主機名:cons7s
login 193.168.120.77 is successful
主機ip:193.168.120.77
login 193.168.120.77 is successful
版本資訊:centos linux release 7.3.1611 (core)
login 193.168.120.77 is successful
物理cpu數量:1
邏輯cpu數量:2
物理cpu型號: intel(r) xeon(r) cpu 5150 @ 2.66ghz
login 193.168.120.77 is successful
物理記憶體:1832mb
login 193.168.120.77 is successful
網絡卡型號:ethernet controller: intel corporation 82545em gigabit ethernet controller (copper) (rev 01)
Python遠端操作Linux
安裝paramiko 執行下面的命令pip install paramiko default timeout 60 linux主機ssh安裝 保證有一台linux主機 自己搭建虛擬機器 如果沒有臨時使用雲主機 保證ssh服務開啟 在linux機器上執行 建議使用ubuntu sudo apt get...
python遠端ssh連線linux
1 安裝第三方庫paramiko pip install paramiko2 建立ssh客戶端,並連線 ssh paramiko.sshclient 自動認證 ssh.set missing host key policy paramiko.autoaddpolicy ssh.connect 192...
linux直接遠端獲取linux伺服器檔案
1.scp 優點 簡單方便,安全可靠 支援限速引數 缺點 不支援排除目錄 用法 scp就是secure copy,是用來進行遠端檔案拷貝的。資料傳輸使用 ssh,並且和ssh 使用相同的認證方式,提供相同的安全保證 命令格式 scp 引數 源位址 使用者名稱 ip位址或主機名 檔案路徑 目的位址 使...