The examples in this article describe how Python monitors windows through Ssh-powershell. Share to everyone for your reference. The specific analysis is as follows:
For server monitoring, monitoring Linux either by itself or with some open source tools such as Nagios,zenoss or whatever. But after all, some companies have Windows do server, compared to Linux, Windows does not have the convenience of SHELL,CMD commands for monitoring is far from Linux convenience. But now if you install PowerShell (win7,2008) on Windows, it's much more convenient than before, and the commands on Linux can be executed in PowerShell, such as viewing a process or PS.
It encapsulates a python script that invokes PowerShell via SSH (via the Pexpect module), inside the packet fast ps,netstat,ping detection, view hard disk, CPU information and load, memory information. By creating a Ssh_win32 class object and then calling its method, the parsed Python object is returned.
ssh_powershell.py:
#! /usr/bin/env python#-*-coding:utf-8-*-import refrom pexpect import *class ssh_win32:def __init__ (self, user, host, p Assword=none,systemroot= ' C ', papath= ', timeout=5,verbose=0): Self.user = user# monitoring machine username self.host = host# monitoring Machine IP Self.verbose = verbose Self.password = password# password self.timeout=timeout# Execute command timeout Self.systemroot=systemroo T#windows installed letter if not Papath: path to #powershell. exe self.powershell_path=self.systemroot+ ':/windows/system32/ Windowspowershell/v1.0/powershell.exe ' Self.key = [' authenticity ', ' assword: ', ', ' C Ommand not found. ', EOF,] self.f = open (' Ssh.out ', ' W ') def SSH (self,command): cmd= ' ssh-l%s%s '% (sel F.user,self.host,command) print "cmd:", cmd con=spawn (cmd,timeout=self.timeout) seen=con.expect (self.key) if SE En = = 0:con.sendline (' yes ') seen = Con.expect (self.key) if seen = = 1: # If not self.password: # SE Lf.password = Getpass.getpass (' RemOTE Password: ') con.sendline (Self.password) try:res=con.read () except Exception, E:res=con . before# print "Res:", Res return res def ssh_disk (self): cmd=self.powershell_path+ "Get-wmiobject win32_logical Disk "Res=self.ssh (cmd) disk={} if Res:res=res.split (' No such file or directory ') [ -1].replace (' \ R ', ')." Spli T (' \ 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 di Sk[predisk][key]=value for D in Disk.keys (): If disk[d][' DriveType ']!= ' 3 ': Disk.pop (d) # print ' Disk: ', di SK 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_operatingsy Stem 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= 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 Retu RN 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:l Ost_percent=m.group (1) print ' lost_percent: ', lost_percent return int (lost_percent) Else:return None de F 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[' Handles ']=row[0] process[' NP M ']=row[1] process[' PM ']=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[' foreign_address ']=row[2] process[' state ']=row[3] process[' pid ']=row[-1] netstat.append (process) # Print Netstat return Netstat Else:return noneif __name__ = = "__main__": cmd= "C:/windows/system32/windowspowershell/v1.0/powershell.exe PS "user= ' admin ' host= ' 192.168.123.105 ' password= ' 123456 ' Ssh=ssh_win32 (user,host,password,systemroot= ' C ', timeout= 5) # Print ssh.ssh_cpu () # print "\n\n\n\n" # Print Ssh.ssh_disk () # print "\n\n\n\n" # Print ssh.ssh_memory () # Print SSH . ssh_ping (host) # Print SSH.Ssh_ps () # Print Ssh.ssh_netstat ()
Hopefully this article will help you with Python programming.