Python's way of monitoring windows via Ssh-powershell

Source: Internet
Author: User
Tags split ssh

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 Whether it's a do-it-yourself script or some open-source tool such as Nagios,zenoss or something. But after all, some companies have windows to do the server, relative to Linux, Windows is not convenient shell,cmd under the command for monitoring is far from Linux convenience. But now windows if installed PowerShell (win7,2008), more convenient than before, Linux commands are basically able to perform in the PowerShell, such as viewing the process or PS.

itself encapsulates a python via SSH (via Pexpect module) to invoke the PowerShell script, which packs fast ps,netstat,ping detection, viewing hard disk, CPU information and load, memory information. By creating the Ssh_win32 class object, and then calling its method, all of the returned objects are parse-good Python objects.

ssh_powershell.py:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 #! /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# monitoring machine username Self.host = host# Monitor the IP self.verbose = verbose Self.password = password# password self.timeout=timeout# Execute command timeout self.systemroot= Systemroot#windows installed disk letter if not papath: #powershell. exe path self.powershell_path=self.systemroot+ ':/windows/system32 /windowspowershell/v1.0/powershell.exe ' Self.key = [' authenticity ', ' assword: ', ', ' Command not found. ', E OF,] 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.expect (self.key) If seen = 0:con.sendline (' yes ') seen = Con.expect ( Self.key) if seen = = 1: # If not self.password: # Self.password = getpass.getpass (' Remote password: ') con.sendline (SELF.P Assword) 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_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 i n 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[pre disk]={} disk[predisk][key]=value else:if key in [' FreeSpace ', ' Size ']: if Value:value=int (value)/1024/1024/1024 Predisk][key]=value for D in Disk.keys (): If disk[d][' DriveType ']!= ' 3 ': Disk.pop (d) # print ' Disk: ', disk return disk DEF s Sh_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 I n 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 =res.replace (' R ', ') '. Split (' n ') res=[m for M in res if M][-1] print ' res: ', res key,value=res.split (': ') key=key.strip () VA Lue=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.S Trip ()] for p in res:process={} Row=[para to 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= ' NE Tstat-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 to 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 None if __name__ = "__main__": cmd= "C:/windows/system" 32/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 ' nnnn ' # print ssh.ssh_disk () # print ' nnnn ' # print ssh.ssh_memory () # Print Ssh.ssh_pin G (host) # print ssh.ssh_ps () # Print Ssh.ssh_netstat ()

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.