Python monitors windows through SSH PowerShell

From , 4 Years ago, written in Python, viewed 51 times.
URL https://pastebin.vip/view/be3087e7
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #Author:pako
  4. #Email:zealzpc@gmail.com
  5. import re
  6. from pexpect import *
  7. class ssh_win32:
  8.     def __init__(self, user, host, password=None,systemroot='c',papath='',timeout=5,verbose=0):
  9.  
  10.         self.user = user#监控机器的username
  11.         self.host = host#监控机器的ip
  12.         self.verbose = verbose
  13.         self.password = password#密码
  14.         self.timeout=timeout#执行命令的timeout
  15.         self.systemroot=systemroot#windows 所安装的盘符
  16.         if not papath:#powershell.exe的路径
  17.             self.powershell_path=self.systemroot+':/WINDOWS/system32/WindowsPowerShell/v1.0/powershell.exe '
  18.         self.key = [
  19.             'authenticity',
  20.             'assword:',
  21.             '@@@@@@@@@@@@',
  22.             'Command not found.',
  23.             EOF,
  24.             ]
  25.        
  26.         self.f = open('ssh.out','w')
  27.  
  28.     def ssh(self,command):
  29.         cmd='ssh -l %s %s %s'%(self.user,self.host,command)
  30.         print "cmd:",cmd
  31.         con=spawn(cmd,timeout=self.timeout)
  32.         seen=con.expect(self.key)
  33.         if seen == 0:
  34.             con.sendline('yes')
  35.             seen = con.expect(self.key)
  36.         if seen == 1:
  37.     #        if not self.password:
  38.     #            self.password = getpass.getpass('Remote password: ')
  39.             con.sendline(self.password)
  40.             try:
  41.                 res=con.read()
  42.             except Exception ,e:
  43.                 res=con.before
  44. #            print "res:",res
  45.         return res
  46.     def ssh_disk(self):
  47.         cmd=self.powershell_path+"Get-WmiObject win32_logicaldisk"
  48.         res=self.ssh(cmd)
  49.         disk={}
  50.         if res:
  51.             res=res.split('No such file or directory')[-1].replace('\r','').split('\n')
  52.             res=[c for c in res if c]
  53. #            print 'res:',res
  54.         predisk='C'
  55.         for d in res:
  56. #            print d
  57.             key,value=d.split(':',1)
  58. #            print d
  59. #            print 'key:',key,'value:',value
  60.             key=key.strip()
  61.             value=value.strip()
  62.             if key=='DeviceID' and value not in disk.keys():
  63.                 predisk=value
  64.                 disk[predisk]={}
  65.                 disk[predisk][key]=value
  66.             else:
  67.                 if key in ['FreeSpace','Size']:
  68.                     if value:
  69.                         value=int(value)/1024/1024/1024
  70.                 disk[predisk][key]=value
  71.         for d in disk.keys():
  72.             if disk[d]['DriveType']!='3':
  73.                 disk.pop(d)
  74. #        print 'disk:',disk
  75.         return disk
  76.    
  77.     def ssh_cpu(self):
  78.         cmd=self.powershell_path+'gwmi -computername localhost win32_Processor'
  79.         res=self.ssh(cmd)
  80.         res=res.split('No such file or directory')[-1].replace('\r','').split('\n')
  81.         res=[r for r in res if r]
  82. #        print res
  83.         cpu={}
  84.         for i in res:
  85. #            print '='*10
  86. #            print i
  87.             i=i.split(':')
  88.         #    print i
  89.             if len(i)==2:
  90.                 key,value=i
  91.             else:
  92.                 continue
  93.             key=key.strip()
  94.             value=value.strip()
  95. #            print 'key:',key
  96. #            print 'value:',value
  97.             cpu[key]=value
  98.         return cpu
  99.    
  100.     def ssh_memory(self):
  101.         totalmem=self.powershell_path+'Get-WmiObject win32_OperatingSystem TotalVisibleMemorySize'
  102.         freemem=self.powershell_path+'Get-WmiObject win32_OperatingSystem FreePhysicalMemory'
  103.         memory={}
  104.         for cmd in [totalmem,freemem]:
  105.             res=self.ssh(cmd)
  106.             if 'Win32_OperatingSystem' in res:
  107.                 res=res=res.replace('\r','').split('\n')
  108.                 res=[m for m in res if m][-1]
  109.                 print 'res:',res
  110.                 key,value=res.split(':')
  111.                 key=key.strip()
  112.                 value=value.strip()
  113.                 memory[key]=value
  114.             else:
  115.                 print "not return data"
  116.                 return None
  117.         return memory
  118.     def ssh_ping(self,host):
  119.         cmd='ping -n 1 %s'%host
  120.         patt=r'.+?(\d*)% loss.*'
  121.         res=self.ssh(cmd).replace('\r','').replace('\n','')
  122.         print res
  123.         m=re.match(patt,res)
  124.         if m:
  125.             lost_percent=m.group(1)
  126.             print 'lost_percent:',lost_percent
  127.             return int(lost_percent)
  128.         else:
  129.             return None
  130.        
  131.     def ssh_ps(self):
  132.         cmd=self.powershell_path+'ps'
  133.         res=self.ssh(cmd)
  134.         ps=[]
  135.         if '-- -----------' in res:
  136.             res=res.replace('\r','').split('-- -----------')[-1].split('\n')
  137.             res=[d for d in res if d.strip()]
  138.             for p in res:
  139.                 process={}
  140.                 row=[para for para in p.split(' ') if para.strip()]
  141.                 process['handles']=row[0]
  142.                 process['npm']=row[1]
  143.                 process['pm']=row[2]
  144.                 process['ws']=row[3]
  145.                 process['vm']=row[4]
  146.                 process['cpu']=row[5]
  147.                 process['id']=row[6]
  148.                 process['process_name']=row[-1]
  149.                 ps.append(process)
  150. #            print ps
  151.             return ps
  152.         else:
  153.             return None
  154.     def ssh_netstat(self):
  155.         cmd='netstat -ao'
  156.         res=self.ssh(cmd)
  157.         netstat=[]
  158.         if 'PID' in res:
  159.             res=res.replace('\r','').split('PID')[-1].split('\n')
  160.             res=[d for d in res if d.strip()]
  161.             for p in res:
  162.                 process={}
  163.                 row=[para for para in p.split(' ') if para.strip()]
  164.                 process['proto']=row[0]
  165.                 process['local_address']=row[1]
  166.                 process['foreign_address']=row[2]
  167.                 process['state']=row[3]
  168.                 process['pid']=row[-1]
  169.                 netstat.append(process)
  170. #            print netstat
  171.             return netstat
  172.         else:
  173.             return None
  174. if __name__ == "__main__":
  175.     cmd="c:/WINDOWS/system32/WindowsPowerShell/v1.0/powershell.exe ps"
  176.     user='admin'
  177.     host='192.168.123.105'
  178.     password='123456'
  179.     ssh=ssh_win32(user,host,password,systemroot='c',timeout=5)
  180. #    print ssh.ssh_cpu()
  181. #    print "\n\n\n\n"
  182. #    print ssh.ssh_disk()
  183. #    print "\n\n\n\n"
  184. #    print ssh.ssh_memory()
  185. #    print ssh.ssh_ping(host)
  186. #    print ssh.ssh_ps()
  187. #    print ssh.ssh_netstat()
  188. #//python/5636

Reply to "Python monitors windows through SSH PowerShell"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website