python使用长ping命令_在Python中调用Ping命令,批量IP的方法
#!/usr/bin/env python
#coding:UTF-8
'''''''
Author: 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多行存储
'''''''
import argparse
import os
import platform
import time,datetime
def PingCmd():
cmdStr = ''
if platform.system()=='Linux':
cmdStr = 'ping -c 1 %s > /dev/null'
else:
cmdStr = 'ping -n 1 %s > nul'
return cmdStr
def IPV4SegList(aList):
IPSeg = aList[0].split('.')
IP4Begin = int(IPSeg[3])
IP4End = int(aList[1])
segList = []
if IP4End<=IP4Begin:
IP4End += IP4Begin
else:
IP4End +=1
for ip in range(IP4Begin,IP4End):
aIP = IPSeg[0]+'.'+IPSeg[1]+'.'+IPSeg[2]+'.'+str(ip)
segList += [aIP]
return segList
def ParseSeg(aSeg):
segList = []
aList = aSeg.split('-')
if len(aList)==2:
segList = IPV4SegList(aList)
else:
segList = aList
return segList
def AddLineIP(IPList,aLine):
LineList = aLine.split(';')
for aSeg in LineList:
segList = ParseSeg(aSeg)
IPList += segList
return IPList
def GetIPListFromFile(filename):
IPList = []
f = open(filename)
lines = f.readlines()
for line in lines:
line = line.strip()
line = line.replace('\n','')
IPList = AddLineIP(IPList,line)
return IPList
def NotifyAdmin(failureIP):
print('Network error at ',failureIP)
def ReportSummary(success,failure):
print('----DONE! Total: %s nodes. %s ---'%(success+failure,timeFmt()))
def PingList(aList):
sucess,failure = 0,0
failureIP = []
print('Trying...')
for ip in aList:
cmdStr = PingCmd()%ip.replace('\n','')
res = os.system(cmdStr)
if res < 1 :
sucess += 1
else:
failure += 1
failureIP += [ip]
time.sleep(1)
if failure :
NotifyAdmin(failureIP)
else:
ReportSummary(sucess,failure);
def timeFmt():
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
def writeLog(content):
print('[%s] %s' % (timeFmt(), content))
file_object = open('ping.' + time.strftime('%Y-%m-%d', time.localtime(time.time())) + '.log', 'a')
file_object.write('[%s] %s\n' % (timeFmt(), content));
file_object.close()
if __name__ == '__main__':
parse = argparse.ArgumentParser(description='Batch ping utility.')
parse.add_argument('-f','--filename',default='ip.txt',help='IP file name(Default IP.txt)')
parse.add_argument('-d','--destip',type=str,default='',help='destination IPs ');
args = parse.parse_args()
if args.destip!='':
IPList = AddLineIP([],args.destip)
else:
if args.filename!='':
IPFile = args.filename
else:
IPFile = 'ip.txt'
IPList = GetIPListFromFile(IPFile)
PingList(IPList)
python使用长ping命令_在Python中调用Ping命令,批量IP的方法相关推荐
- python执行linux命令返回结果_Python中调用Linux命令并获取返回值
方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256/512表示未找到,该方法适用于she ...
- linux 查找py文件命令_在 Linux 中如何从命令行查找 VirtualBox 版本
如果你想知道如何在 Linux 中从命令行查找 VirtualBox 版本,可以采用以下几种方法. -- Sk(作者) 我使用 Oracle VirtualBox 和 KVM 虚拟化程序 测试不同的 ...
- linux tr 命令_在Linux中使用tr命令玩角色
linux tr 命令 tr command in Linux translates one set of characters to another. It can replace a charac ...
- python运行时间长就卡_解决Python设置函数调用超时,进程卡住的问题
背景: 最近写的Python代码不知为何,总是执行到一半卡住不动,为了使程序能够继续运行,设置了函数调用超时机制. 代码: import time import signal def test(i): ...
- linux comm命令_在Linux中使用Comm命令比较文件
linux comm命令 Linux have a lot of tools to compare files. These tools provides a lot of powerful feat ...
- 如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样)
如何在Python脚本中调用外部命令(就像在linux shell或Windows命令提示符下输入一样) python标准库中的subprocess可以解决这个问题. from subprocess ...
- 安卓程序中调用 linux 命令
博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 在研究串口 jni 开发的时候,遇到了这样一段代码: Process su;su = Runtime.getRuntim ...
- shell 调用matlab,在matlab中调用linux命令 以及后台运行matlab --zz
由于matlab和linux命令各有所长:matlab比较擅长数值计算,linux命令比较擅长文件操作(matlab也基本上能够完成,但会稍微麻烦一点).所以在很多时候会需要将二者结合起来. 可能更多 ...
- 在Android项目中调用FFmpeg命令
概述 FFmpeg,命令行非常强大.在PC机上,调用一行FFmpeg命令,就可以对视频文件进行剪切.合并.转码等功能.本文将介绍如何在Android中调用FFmpeg命令行. 编译 前面已经有两篇文章 ...
- Awk中调用shell命令
Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...
最新文章
- centos设置网络自动启动
- 为什么static成员必须在类外初始化
- 图解 二叉查找树 红黑树
- IO流(文本文件读取练习)
- call,apply,求最大最小值,平均数等基础编程知识
- nodejs的启动方式
- 乘基取整法是什么_十进制小数转二进制小数乘2取整法的直观理解
- python 语音播放_基于Python编写的语音播放软件
- moel vue 自定义v_vue组件,自定义v-model方法
- Linux 格式化分区 报错Could not stat --- No such file or directory 和 partprobe 命令
- MTK驱动(46)---- Android CPU频率设置(MTK平台)
- 现代通信理论与新技术 - 填空自测
- C语言-数组的趣味应用-筛法求素数
- 用python的tkinter做游戏(九)—— 坦克大战 正式篇
- java数字转读音_java 数字转汉语读音的程序
- 奥克兰大学计算机it专业介绍,新西兰奥克兰大学IT硕士专业解析
- anaconda出现Multiple Errors Encountered.
- (数字ic验证)从零开始的apb_watchdog验证模块搭建(一、功能介绍与环境搭建)
- MySQL slave相关参数
- VC++6.0 总是1error 问题的解决
热门文章
- 离散数学计算机科学与技术答案,2计算机科学与技术专业本科离散数学期末复习题2...
- ng bind html br,使用PrimeNG开发angular web项目
- android指定分享到qq,Android使用系统分享文件给微信,QQ指定的用户
- 用java编写数组最小公倍数_[求助]Java编的求最小公倍数的程序,老是不对。。。调试了无数次了,请达人指教。。...
- linux scp命令 将数据从一台linux服务器复制到另一台linux服务器
- stream常用list转map list转list
- springboot日志框架
- 根据配置文件的值创建不同对象
- centos6.8 开启透传
- java.util.BitSet 研究