下面是最初的情况

#/usr/bin/env python
# -*- coding: utf-8 -*- import os
import time
import subprocess
import threading
from threadpool import ThreadPool
import threadpool
import re
from Queue import Queue def ping_call():
while not IP_QUEUE.empty():
ip=IP_QUEUE.get()
command='ping -c 2 -W 1 %s'%ip
print(command)
result=subprocess.Popen(command,
shell=True,stdout=subprocess.PIPE) //A
s=result.stdout.read()
e = "ttl" in s
if e:
print('ip地址:{} ping ok'.format(ip))
else:
print('ip地址:{} ping fall'.format(ip)) //B if __name__ == '__main__':
network=input('请输入网段>>>').strip()
host=input('请输入主机范围以空格隔开>>>').strip().split()
a,b=host[0],host[1]
print(network.split('.'))
if len(network.split('.'))==3 and a.isdigit() and b.isdigit() and re.match('\d{1,3}\.\d{1,3}\.\d{1,3}',network):
a=int(a)
b=int(b)
start_time = time.time()
IP_QUEUE=Queue()
threads=[]
for i in range(a,b+1):
IP_QUEUE.put(network+'.'+str(i))
for i in range(50):
thread=threading.Thread(target=ping_call)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print("程序耗时{:.2f}".format(time.time() - start_time))

上面的例子也可用改为subprocess.call,在linux系统中命令结果成功为真就时0 ,失败则为假,windows系统上面只关心命令本身是不是可用执行,不关心结果,所以只能用在Linux系统上面

修改标注A到B的代码结果为:

#/usr/bin/env python
# -*- coding: utf-8 -*- import os
import time
import subprocess
import threading
import re
from Queue import Queue def ping_call():
while not IP_QUEUE.empty():
ip=IP_QUEUE.get()
command='ping -c 2 -W 1 %s'%ip
print(command)
result=subprocess.call(command,
shell=True,stdout=subprocess.PIPE) if result:
print('ip地址:{} ping fail'.format(ip))
else:
print('ip地址:{} ping ok'.format(ip)) if __name__ == '__main__':
network=input('请输入网段>>>').strip()
host=input('请输入主机范围以空格隔开>>>').strip().split()
a,b=host[0],host[1]
print(network.split('.'))
if len(network.split('.'))==3 and a.isdigit() and b.isdigit() and re.match('\d{1,3}\.\d{1,3}\.\d{1,3}',network):
a=int(a)
b=int(b)
start_time = time.time()
IP_QUEUE=Queue()
threads=[]
for i in range(a,b+1):
IP_QUEUE.put(network+'.'+str(i))
for i in range(50):
thread=threading.Thread(target=ping_call)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print("程序耗时{:.2f}".format(time.time() - start_time))

进一步改版为:

#/usr/bin/env python
# -*- coding: utf-8 -*- import os
import time
import subprocess
import threading
from threadpool import ThreadPool
import threadpool
import re
from Queue import Queue def ping_call():
while not IP_QUEUE.empty():
ip=IP_QUEUE.get()
command='ping -c 2 -W 1 %s'%ip
file=open('/dev/null','w')
result=subprocess.call(command,
shell=True,stdout=file,stderr=file)
if result:
print('ip地址:{} ping fail'.format(ip))
else:
print('ip地址:{} ping ok'.format(ip))
success.append(ip.split('.')[-1])
file.close() if __name__ == '__main__':
network=input('请输入网段>>>').strip()
host=input('请输入主机范围以空格隔开>>>').strip().split()
a,b=host[0],host[1]
print(network.split('.'))
if len(network.split('.'))==3 and a.isdigit() and b.isdigit() and re.match('\d{1,3}\.\d{1,3}\.\d{1,3}',network):
a=int(a)
b=int(b)
start_time = time.time()
IP_QUEUE=Queue()
threads=[]
success=[]
for i in range(a,b+1):
IP_QUEUE.put(network+'.'+str(i))
for i in range(50):
thread=threading.Thread(target=ping_call)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
print("程序耗时{:.2f}".format(time.time() - start_time))
print("成功主机有:")
for i in success:
print(i)

那么上面的几个脚本如何执行呢,这里也是碰到了很多坑,主要是输入的数据要打引号

[root@centos7 ~]# python ping_host.py
请输入网段>>>'192.168.0'
请输入主机范围以空格隔开>>>'1 10'
['', '', '']
ip地址:192.168.0.1 ping ok
ip地址:192.168.0.4 ping fail
ip地址:192.168.0.3 ping fail
ip地址:192.168.0.5 ping fail
ip地址:192.168.0.9 ping fail
ip地址:192.168.0.6 ping fail
ip地址:192.168.0.8 ping fail
ip地址:192.168.0.2 ping fail
ip地址:192.168.0.7 ping fail
程序耗时2.16
成功主机有:
1

Linux中使用python测试主机存活 Linux系统CentOS Linux release 7.3.1611 (Core) py版本Python 2.7.5的更多相关文章

  1. 如何在Linux中显示和设置主机名

    原文链接 随着连接到网络的计算机数量越来越多,每一台计算机都需要有一个属性来区别于其它计算机.和现实世界中的人一样,计算机也有一个叫做hostname(主机名)的属性. 什么是hostname 从它的 ...

  2. 如何在Linux中显示和设置主机名(适用ubantu、centos等版本)

    随着连接到网络的计算机数量越来越多,每一台计算机都需要有一个属性来区别于其它计算机.和现实世界中的人一样,计算机也有一个叫做hostname(主机名)的属性. 什么是hostname 从它的操作手册来 ...

  3. Linux中的nc测试端口是否开放

    nc测试端口是否开放 在Linux中有一个级强大的网络工具netcat,在默认情况下面都是没有安装的,现在介绍一下安装过程 其实安装很简单 一.安装使用 1.只需输入命令yum安装: [root@SZ ...

  4. linux中uptime命令获取主机运行时间和查询系统负载信息

    系统中的uptime命令主要用于获取主机运行时间和查询linux系统负载等信息.uptime命令可以显示系统已经运行了多长时间,信息显示依次为:现在时间.系统已经运行了多长时间.目前有多少登陆用户.系 ...

  5. Linux 中使用 dd 测试磁盘性能

    翻译自 : Linux I/O Performance Tests using dd 基本说明 dd 可以用来做简单的低级别复制文件. 这样做, 一般都是可一直直接访问设备文件. 需要说明的是, 错误 ...

  6. kali 2.0 linux中的Nmap的主机探测

    不多说,直接上干货! 如果是第一次接触Nmap,推荐在MSF终端中输入不加任何参数的Nmap命令,以查看其使用方法. 更多,其实, msf > nmap -h [*] exec: nmap -h ...

  7. 在linux 中wget 无法解析主机

    vim /etc/resolv.cof 在里面加入节点 nameserver 8.8.8.8 / nameserver 8.8.4.4 即可 失败时: 成功时:

  8. fping命令测试主机存活

    author:headsen  chen date: 2018-10-09 20:11:22 1,测试一个范围内的主机: fping  -a  -g 192.168.1.1 192.168.1.255 ...

  9. Python 局域网主机存活扫描

    #! python # -*- coding: utf-8 -*- __author__ = 'Deen' import os import threading import argparse # 从 ...

随机推荐

  1. Quartz定时任务详解一

    以下是我在应用的的一个基本配置: #---------调度器属性---------------- org.quartz.scheduler.instanceName = TestScheduler o ...

  2. Tensorflow之基于MNIST手写识别的入门介绍

    Tensorflow是当下AI热潮下,最为受欢迎的开源框架.无论是从Github上的fork数量还是star数量,还是从支持的语音,开发资料,社区活跃度等多方面,他当之为superstar. 在前面介 ...

  3. TStrings (TStringList)很有功能

    用 TStrings的Object 保存类的方式,来保存除了Items以外的值. 今天才发现,原来,TStrings下,还有 Items,Values,Items.Names,Items.Values ...

  4. 编写dll时的内存分配策略

    前一篇文章介绍了为何要共用内存管理器,有人要问可不可以在编写dll时更通用一些,可以兼顾其它编译器(如果是其它编译器的话,Delphi写的dll不能与其它语言共用内存管理器),采用一定的策略来避免在d ...

  5. [NEWS]Microsoft expands partnerships with AOL and AppNexus, Bing to power search for AOL properties

    http://advertising.microsoft.com/en/blog/33906/microsoft-expands-partnerships-with-aol-and-appnexus- ...

  6. 安装Python时遇到的api-ms-win-crt-runtime-l1-1-0.dll 丢失问题

    api-ms-win-crt-runtime-l1-1-0.dll 丢失 电脑找不到api-ms-win-crt-runtime-l1-1-0.dll文件解决方法: 问题描述: 1.开机提示“api- ...

  7. 黄聪:移动应用抓包调试利器Charles

    一.Charles是什么?   Charles是在 Mac或Windows下常用的http协议网络包截取工具,是一款屌的不行的抓包工具,在平常的测试与调式过程中,掌握此工具就基本可以不用其他抓包工具了 ...

  8. 无后缀名伪静态路径在IIS7.0的网站提示 "404 - File or directory not found"

    新配置服务器(windows server 2008,not sp1) 经测试情况如下: ①无后缀名伪静态路径行在IIS7.0的网站提示 ”404 - File or directory not fo ...

  9. ALGO-152_蓝桥杯_算法训练_8-2求完数

    记: 掌握完数的概念 AC代码: #include <stdio.h> int main(void) { int i,j,sum; ; i <= ; i ++) { sum = ; ...

  10. 记录一次OOM分析过程

    工具: jstat jmap jhat 1.jstat查看gc情况 S0C.S1C.S0U.S1U:Survivor 0/1区容量(Capacity)和使用量(Used) EC.EU:Eden区容量和 ...