pythonバッチリモートマシン、Linuxコマンド実行


大量のリモートマシン、マシン情報を収集します.
# 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('
').split('/')) proc_name = item.strip('
').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 "
IP :
",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 "

This is DPKG_NAME DPKG:

" 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('
') def get_sys_version(self): commonds = 'lsb_release -a' get_sys_version = self.ssh_comond(commonds) # print get_sys_version print "

This is sys_info:

" for item in get_sys_version: print item.strip('
') def get_crontab(self): commonds = 'cat /etc/crontab' get_crontab = self.ssh_comond(commonds) print "

crontab list is :

" 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('
') def get_mount(self): commonds = 'mount' get_mount = self.ssh_comond(commonds) print "

This is Mount list :

" 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('
') def get_ha(self): commonds = 'ps -ef |grep heartbeat' get_ha = self.ssh_comond(commonds) print "

This is HeartBeat_Proc :

" for item in get_ha: item_str = item.encode('unicode-escape').decode('string_escape') print item_str.split('?')[1][16:].strip('
') def get_ef(self): commonds = 'ps -ef ' get_ha = self.ssh_comond(commonds) print "

All_proc is :

" 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('
')) > 30: print item_str.split('?')[1][16:].strip('
') def Get_All_Ef(self): All_EF=[] commonds = 'ps -A ' Get_All_Ef = self.ssh_comond(commonds) print "

All_proc is :

" 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('
') # print Proc_Name if ((Proc_Name == 'CMD') or (re.findall(r'