介绍
这篇文章给大家介绍怎么在Python中调用平命令,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
<>强如下所示:强>
# !/usr/bin/env Python utf - 8编码: & # 39;& # 39;& # 39;& # 39;& # 39;& # 39;& # 39; ,作者:jefferchen@163.com ,可在命令行直接带目的IP,也可将IP列表在文本文件中。 ,pingip.py -d  DestIP ,DestIP示例:a)单个:192.168.11.1 ,,,,,,,b)多个:192.168.11.1;172.16.8.1;176.13.18.2 ,,,,,,,c)网段:192.168.11.1-127 文才能本文件:ip.txt ,,,,,目的IP多行存储 & # 39;& # 39;& # 39;& # 39;& # 39;& # 39;& # 39; import argparse import 操作系统 import 平台 import datetime def PingCmd (): 时间=cmdStr 才能;& # 39;& # 39; if 才能;platform.system ()==& # 39; linux # 39;: ,,,cmdStr =, & # 39; ping -c 1, % s 祝辞,/dev/null # 39; 其他的才能: ,,,cmdStr =, & # 39; ping -n 1, % s 祝辞,nul # 39; return cmdStr才能 def IPV4SegList(倾斜的): IPSeg 才能=,倾斜的[0].split(& # 39; # 39;公司) IP4Begin 才能=,int (IPSeg [3]) IP4End 才能;,=,int(倾斜的[1]) 时间=segList 才能;[] if 才能;IP4End<=IP4Begin: ,,,IP4End +=IP4Begin 其他的才能: ,,IP4End +=1 for 才能;ip 拷贝范围(IP4Begin IP4End): ,,,aIP =, IPSeg[0] + & # 39;强生# 39;+ IPSeg[1] + & # 39;强生# 39;+ IPSeg[2] + & # 39;强生# 39;+ str (ip) ,,,segList +=, (aIP) return segList才能 def ParseSeg (aSeg): 时间=segList 才能;[] 时间=aList 才能;aSeg.split (& # 39; & # 39; if 才能len(倾斜的)==2: ,,,segList =, IPV4SegList(倾斜的) 其他的才能: ,,,segList =船向一边倾斜的 return segList才能 def AddLineIP (IPList艾琳): 时间=LineList 才能;aLine.split (& # 39;; & # 39; for 才能;aSeg  LineList:拷贝 ,,,segList =, ParseSeg (aSeg) ,,,IPList +=segList return IPList才能 def GetIPListFromFile(文件名): 时间=IPList 才能;[] f =,开放才能(文件名) 时间=lines 才能;f.readlines () for 才能;line 拷贝: ,,,line =, line.strip () ,,,line =, line.replace (& # 39; \ n # 39; & # 39; & # 39;) ,,,IPList =, AddLineIP (IPList,线) return IPList才能 def NotifyAdmin (failureIP): 打印才能(& # 39;Network error at & # 39;, failureIP) def ReportSummary(成功、失败): 打印才能(& # 39;——完成了!,总:,% s 节点只% s ——& # 39; %(成功+失败,timeFmt ())) def PingList(倾斜的): 成功才能failure =0,0 时间=failureIP 才能;[] 打印才能(& # 39;在…& # 39;) for 才能;ip 拷贝倾斜的: ,,,cmdStr =, PingCmd () % ip.replace (& # 39; \ n # 39; & # 39; & # 39;) ,,,res =, os.system (cmdStr) ,,,if res & lt;, 1,: ,,,,,sucess +=1 ,,,: ,,,,,failure +=1 ,,,,,failureIP +=, (ip) ,,,time . sleep (1) if 才能;failure : ,,,,NotifyAdmin (failureIP) 其他的才能: ,,,ReportSummary(成功、失败); def timeFmt (): return 才能;time.strftime (& # 39; Y % - % - % d % H: % m: % & # 39;,, time.localtime (time.time ())) def writeLog(内容): 打印才能(& # 39;(% s), % & # 39;, %, (timeFmt(),内容)) file_object =,开放才能(& # 39;平# 39;公司,+,time.strftime (& # 39; Y - % - % d % # 39;,, time.localtime (time.time ())), +, & # 39; . log # 39;,, & # 39;一个# 39;) file_object.write才能(& # 39;(% s), % s \ n # 39;, %, (timeFmt(),内容)); file_object.close才能() if __name__ ==, & # 39; __main__ # 39;: parse 才能=,argparse.ArgumentParser(描述=& # 39;Batch ping 效用# 39;公司) parse.add_argument才能(& # 39;- f # 39;, & # 39;——文件名# 39;,默认=& # 39;ip.txt& # 39;,帮助=& # 39;IP file 名字(Default IP.txt) & # 39;) parse.add_argument才能(& # 39;- d # 39;, & # 39;——destip& # 39;, type=str,默认=& # 39;& # 39;,帮助=& # 39;destination IPs & # 39;); 时间=args 才能;parse.parse_args () if 才能;args.destip !=& # 39; & # 39;: ,,,IPList =, AddLineIP ([], args.destip) 其他的才能: ,,,if args.filename !=& # 39; & # 39;: null null null null null怎么在Python中调用平命令