#! /Usr/bin/env python #-*-Coding: UTF-8 -*- Import re From pexpect import * Class ssh_win32: Def _ init _ (self, user, host, password = None, systemroot = 'C', papath = '', timeout = 5, verbose = 0 ): Self. user = user # monitor the username of the machine Self. host = host # ip address of the monitored Machine Self. verbose = verbose Self. password = password # password Self. timeout = timeout # timeout of Command Execution Self. systemroot = systemroot # drive letter installed on windows If not papath: path of #powershell.exe Self. powershell_path = self. systemroot + ':/WINDOWS/system32/WindowsPowerShell/v1.0/powershell.exe' Self. key = [ 'Authenticity ', 'Assword :', '@@@@@@@@@@@@', 'COMMAND not found .', EOF, ] Self. f = open ('ssh. out', 'w ') Def ssh (self, command ): Cmd = 'ssh-l % s % s' % (self. user, self. host, command) Print "cmd:", cmd Con = spawn (cmd, timeout = self. timeout) Seen = con. Exact CT (self. key) If seen = 0: Con. sendline ('yes ') Seen = con. Exact CT (self. key) If seen = 1: # If not self. password: # Self. password = getpass. getpass ('remote password :') Con. sendline (self. password) Try: Res = con. read () Except t Exception, e: Res = con. before # Print "res:", res Return res Def ssh_disk (self ): Cmd = self. powershell_path + "Get-WmiObject win32_logicaldisk" Res = self. ssh (cmd) Disk = {} If res: Res = res. split ('no such file or directory') [-1]. replace ('\ R', ''). split (' \ n ') Res = [c for c in res if c] # Print 'res: ', res Predisk = 'C' For d in res: # Print d Key, value = d. split (':', 1) # Print d # Print 'key: ', key, 'value:', value Key = key. strip () Value = value. strip () If key = 'deviceid' and value not in disk. keys (): Predisk = value Disk [predisk] = {} Disk [predisk] [key] = value Else: If key in ['freespace', 'SIZE']: If value: Value = int (value)/1024/1024/1024 Disk [predisk] [key] = value For d in disk. keys (): If disk [d] ['drivetype ']! = '3 ': Disk. pop (d) # Print 'disk: ', disk Return disk Def ssh_cpu (self ): Cmd = self. powershell_path + 'gwmi-computername localhost win32_Processor' Res = self. ssh (cmd) Res = res. split ('no such file or directory') [-1]. replace ('\ R', ''). split (' \ n ') Res = [r for r in res if r] # Print res Cpu = {} For I in res: # Print '=' * 10 # Print I I = I. split (':') # Print I If len (I) = 2: Key, value = I Else: Continue Key = key. strip () Value = value. strip () # Print 'key: ', key # Print 'value: ', value Cpu [key] = value Return cpu Def ssh_memory (self ): Totalmem = self. powershell_path + 'get-WmiObject win32_OperatingSystem totalvisiblememorysize' Freemem = self. powershell_path + 'get-WmiObject win32_OperatingSystem FreePhysicalMemory' Memory = {} For cmd in [totalmem, freemem]: Res = self. ssh (cmd) If 'win32 _ OperatingSystem 'in res: Res = res. replace ('\ R', ''). split (' \ n ') Res = [m for m in res if m] [-1] Print 'res: ', res Key, value = res. split (':') Key = key. strip () Value = value. strip () Memory [key] = value Else: Print "not return data" Return None Return memory Def ssh_ping (self, host ): Cmd = 'Ping-n 1% s' % host Patt = R'. +? (\ D *) % loss .*' Res = self. ssh (cmd). replace ('\ R', ''). replace (' \ n ','') Print res M = re. match (patt, res) If m: Lost_percent = m. group (1) Print 'lost _ percent: ', lost_percent Return int (lost_percent) Else: Return None Def ssh_ps (self ): Cmd = self. powershell_path + 'ps' Res = self. ssh (cmd) Ps = [] If '-- -----------' in res: Res = res. replace ('\ R', ''). split (' -- ----------- ') [-1]. split (' \ n ') Res = [d for d in res if d. strip ()] For p in res: Process = {} Row = [para for para in p. split ('') if para. strip ()] Process ['handle'] = row [0] Process ['npm '] = row [1] Process ['ps'] = row [2] Process ['ws '] = row [3] Process ['vm '] = row [4] Process ['cpu '] = row [5] Process ['id'] = row [6] Process ['process _ name'] = row [-1] Ps. append (process) # Print ps Return ps Else: Return None Def ssh_netstat (self ): Cmd = 'netstat-ao' Res = self. ssh (cmd) Netstat = [] If 'pid 'in res: Res = res. replace ('\ R', ''). split ('pid') [-1]. split ('\ n ') Res = [d for d in res if d. strip ()] For p in res: Process = {} Row = [para for para in p. split ('') if para. strip ()] Process ['proto'] = row [0] Process ['local _ address'] = row [1] Process ['forn N _ address'] = row [2] Process ['state'] = row [3] Process ['pid '] = row [-1] Netstat. append (process) # Print netstat Return netstat Else: Return None If _ name _ = "_ main __": Cmd = "c:/WINDOWS/system32/WindowsPowerShell/v1.0/powershell.exe ps" User = 'admin' Host = '192. 168.123.105' Password = '000000' Ssh = ssh_win32 (user, host, password, systemroot = 'C', timeout = 5) # Print ssh. ssh_cpu () # Print "\ n" # Print ssh. ssh_disk () # Print "\ n" # Print ssh. ssh_memory () # Print ssh. ssh_ping (host) # Print ssh. ssh_ps () # Print ssh. ssh_netstat () |