WMI是Windows系统的一大利器,Python的win32api库提供了对WMI的支持,安装win32api即可使用 WMI。
本例通过WMI的WQL实现ping命令。
import wmi c = wmi.WMI() wql = "SELECT StatusCode FROM Win32_PingStatus WHERE Address = '127.0.0.1'" for i in c.query(wql): if i.StatusCode: print 'offline' else: print 'online'
c.query(wql)的结构如下,其中StatusCode为0表示能够ping通,大于0表示ping不通
instance of Win32_PingStatus { Address = "127.0.0.1"; BufferSize = 32; NoFragmentation = FALSE; RecordRoute = 0; ResolveAddressNames = FALSE; SourceRoute = ""; SourceRouteType = 0; StatusCode = 0; Timeout = 1000; TimestampRoute = 0; TimeToLive = 128; TypeofService = 128; };
以上这篇Python利用WMI实现ping命令的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。