亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python通過(guò)ssh-powershell監(jiān)控windows的方法

 更新時(shí)間:2015年06月02日 17:39:38   作者:niuniu  
這篇文章主要介紹了python通過(guò)ssh-powershell監(jiān)控windows的方法,涉及Python操作ssh-powershell的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了python通過(guò)ssh-powershell監(jiān)控windows的方法。分享給大家供大家參考。具體分析如下:

對(duì)于服務(wù)器的監(jiān)控來(lái)說(shuō),監(jiān)控linux不管是自己動(dòng)手寫(xiě)腳本還是用一些開(kāi)源的工具比如nagios,zenoss什么的。但畢竟還是有些公司有windows做服務(wù)器的,相對(duì)linux來(lái)說(shuō),windows沒(méi)有方便的shell,cmd下提供的命令對(duì)于監(jiān)控來(lái)說(shuō)遠(yuǎn)遠(yuǎn)沒(méi)有l(wèi)inux方便。但是現(xiàn)在windows上如果安裝了powershell(win7,2008自帶),就比以前方便多了,linux上的命令基本都能在powershell里執(zhí)行,比如查看進(jìn)程還是ps.

自己封裝了一個(gè)python通過(guò)ssh(通過(guò)pexpect模塊)調(diào)用powershell的腳本,里面包快ps,netstat,ping檢測(cè),查看硬盤(pán),cpu信息和負(fù)載,內(nèi)存信息。通過(guò)創(chuàng)建ssh_win32類對(duì)象,然后調(diào)用它的方法,返回的都是解析好的python對(duì)象。

ssh_powershell.py:

#! /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#監(jiān)控機(jī)器的username
    self.host = host#監(jiān)控機(jī)器的ip
    self.verbose = verbose
    self.password = password#密碼
    self.timeout=timeout#執(zhí)行命令的timeout
    self.systemroot=systemroot#windows 所安裝的盤(pán)符
    if not papath:#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 %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.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_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=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['handles']=row[0]
        process['npm']=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 None
if __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()

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Python元組拆包和具名元組解析實(shí)例詳解

    Python元組拆包和具名元組解析實(shí)例詳解

    這篇文章主要介紹了Python元組拆包和具名元組解析,下面的內(nèi)容就圍繞元組作為數(shù)據(jù)記錄屬性展開(kāi),并介紹帶字段名的具名元組函數(shù)namedtuple,列表屬性不再本文中敘述。
    2018-03-03
  • Python異步編程之協(xié)程任務(wù)的調(diào)度操作實(shí)例分析

    Python異步編程之協(xié)程任務(wù)的調(diào)度操作實(shí)例分析

    這篇文章主要介紹了Python異步編程之協(xié)程任務(wù)的調(diào)度操作,結(jié)合實(shí)例形式分析了Python異步編程中協(xié)程任務(wù)的調(diào)度相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用

    pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用

    這篇文章主要為大家介紹了pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用,文中含有詳細(xì)示例操作有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • python 爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn)

    python 爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn)

    這篇文章主要介紹了python 爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • python聚類算法解決方案(rest接口/mpp數(shù)據(jù)庫(kù)/json數(shù)據(jù)/下載圖片及數(shù)據(jù))

    python聚類算法解決方案(rest接口/mpp數(shù)據(jù)庫(kù)/json數(shù)據(jù)/下載圖片及數(shù)據(jù))

    這篇文章主要介紹了python聚類算法解決方案(rest接口/mpp數(shù)據(jù)庫(kù)/json數(shù)據(jù)/下載圖片及數(shù)據(jù)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python3.5集合及其常見(jiàn)運(yùn)算實(shí)例詳解

    Python3.5集合及其常見(jiàn)運(yùn)算實(shí)例詳解

    這篇文章主要介紹了Python3.5集合及其常見(jiàn)運(yùn)算,結(jié)合實(shí)例形式分析了Python3.5集合的定義、功能、交集、并集、差集等常見(jiàn)操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2019-05-05
  • 使用Python在Excel中實(shí)現(xiàn)自動(dòng)查找并替換數(shù)據(jù)

    使用Python在Excel中實(shí)現(xiàn)自動(dòng)查找并替換數(shù)據(jù)

    隨著項(xiàng)目的進(jìn)展,需要經(jīng)常在Excel業(yè)務(wù)表格中查找及替換數(shù)據(jù),已保證數(shù)據(jù)與實(shí)際項(xiàng)目進(jìn)度一致,手動(dòng)一個(gè)一個(gè)查找,然后替換,效率太低,還容易遺漏,現(xiàn)在我們來(lái)試試用Python自動(dòng)完成查找及替換吧,需要的朋友可以參考下
    2023-12-12
  • selenium3.0+python之環(huán)境搭建的方法步驟

    selenium3.0+python之環(huán)境搭建的方法步驟

    這篇文章主要介紹了selenium3.0+python之環(huán)境搭建的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python中tkinter的應(yīng)用:修改字體的實(shí)例講解

    python中tkinter的應(yīng)用:修改字體的實(shí)例講解

    今天小編就為大家分享一篇python中tkinter的應(yīng)用:修改字體的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • Python進(jìn)階語(yǔ)法之類的繼承

    Python進(jìn)階語(yǔ)法之類的繼承

    這篇文章主要為大家介紹了Python類的繼承,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12

最新評(píng)論