批量远程机器,采集机器信息。
# coding=utf-8
'''
Created on 2018年5月2日
@author: ***.***
'''
from pickle import FALSE
#!/usr/bin/python
import paramiko
import re
class ssh_test():
def __init__(self,host,passwd,username):
self.pwd = passwd
self.name = username
self.host = host
self.ssh=paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #加上这句话不用担心选yes的问题,会自动选上(用ssh连接远程主机时,第一次连接时会提示是否继续进行远程连接,选择yes)
self.ssh.connect(self.host,22,self.name,self.pwd) #连接远程主机,SSH端口号为22
def ssh_comond(self,commonds):
stdin, stdout, stderr = self.ssh.exec_command(commonds)
return stdout.readlines()
def get_proc(self):
proc_list = []
commonds = "ps -ef | grep -v grep | grep tang | awk '{print $8}'"
get_proc = self.ssh_comond(commonds)
for item in get_proc:
# item_str = item.encode('unicode-escape').decode('string_escape') #unicode 转Str
No = len(item.strip('\n').split('/'))
proc_name = item.strip('\n').split('/')[No-1]
proc_list.append(proc_name)
print proc_list
def get_ip(self):
ip_list=[]
commonds = "ifconfig"
get_ip = self.ssh_comond(commonds)
for item in get_ip:
if (re.findall(r'inet addr',item,re.I) and re.findall(r'Bcast',item,re.I)) :
item_str = item.encode('unicode-escape').decode('string_escape')
ip_list.append(item_str.split('Bcast')[0].split(':')[1])
print "\n获取到的IP地址为:\n",ip_list
#
def get_dpkg(self):
dpkg= {}
dpkg_list = []
commonds = "dpkg -l | grep TANG | awk '{print $2, $3}'"
get_dpkg = self.ssh_comond(commonds)
print "\n\n This is DPKG_NAME DPKG: \n \n "
for item in get_dpkg:
item_str = item.encode('unicode-escape').decode('string_escape')
dpkg[item_str.split()[0]] = item_str.split()[1]
# print dpkg
for key in dpkg:
dpkg_list.append(key)
print dpkg_list
# print dpkg
def get_hostname(self):
commonds = 'hostname'
get_hostname = self.ssh_comond(commonds)[0]
return get_hostname.strip('\n')
def get_sys_version(self):
commonds = 'lsb_release -a'
get_sys_version = self.ssh_comond(commonds)
# print get_sys_version
print "\n\n This is sys_info:\n \n"
for item in get_sys_version:
print item.strip('\n')
def get_crontab(self):
commonds = 'cat /etc/crontab'
get_crontab = self.ssh_comond(commonds)
print "\n\ncrontab list is : \n\n"
for item in get_crontab:
item_str = item.encode('unicode-escape').decode('string_escape')
if ((item_str[0].isdigit()) or (item_str[0] == '*')):
print item_str.strip('\n')
def get_mount(self):
commonds = 'mount'
get_mount = self.ssh_comond(commonds)
print "\n\n This is Mount list : \n\n"
for item in get_mount:
item_str = item.encode('unicode-escape').decode('string_escape')
if ((item_str[0].isdigit()) or (item_str[0] == '*')):
print item_str.strip('\n')
def get_ha(self):
commonds = 'ps -ef |grep heartbeat'
get_ha = self.ssh_comond(commonds)
print "\n \n This is HeartBeat_Proc : \n\n"
for item in get_ha:
item_str = item.encode('unicode-escape').decode('string_escape')
print item_str.split('?')[1][16:].strip('\n')
def get_ef(self):
commonds = 'ps -ef '
get_ha = self.ssh_comond(commonds)
print "\n\n All_proc is : \n\n"
for item in get_ha:
if ((re.findall(r'\[',item,re.I)) or (re.findall(r'heartbeat',item,re.I) )):
continue
elif re.findall(r'\?',item,re.I):
# print item
item_str = item.encode('unicode-escape').decode('string_escape')
if len(item_str.split('?')[1][16:].strip('\n')) > 30:
print item_str.split('?')[1][16:].strip('\n')
def Get_All_Ef(self):
All_EF=[]
commonds = 'ps -A '
Get_All_Ef = self.ssh_comond(commonds)
print "\n\n All_proc is : \n\n"
for item in Get_All_Ef:
# print item
item_str = item.encode('unicode-escape').decode('string_escape')
# print item_str.split(' ')
Proc_Name= item_str.split(' ')[-1].strip('\n')
# print Proc_Name
if ((Proc_Name == 'CMD') or (re.findall(r'<',Proc_Name,re.I)) or (Proc_Name == 'init') or (Proc_Name == 'ps') or (Proc_Name == 'top') or (Proc_Name == 'bash') or (Proc_Name == 'ssh') ):
continue
else:
if re.findall(r'/',Proc_Name,re.I):
# print Proc_Name.split('/')[0]
All_EF.append(Proc_Name.split('/')[0])
else:
All_EF.append(Proc_Name)
print {}.fromkeys(All_EF).keys() #list去重
def closed (self):
self.ssh.close()
if __name__ == '__main__':
ip_tmp = '192.168.1.'
Mr_hosts =['1','2','3','4','5','6']
passwd = 'username'
username = 'password'
for host in Mr_hsot:
host = ip_tmp +host
ssh = ssh_test(host,passwd,username)
print '\n',host,ssh.get_hostname()
ssh.get_ip()
ssh.get_dpkg()
ssh.Get_All_Ef()
# ssh.get_ef()
ssh.get_sys_version()
ssh.get_crontab()
ssh.get_mount()
ssh.get_ha()
ssh.closed()