鱼C论坛

 找回密码
 立即注册
查看: 1954|回复: 0

[技术交流] Python实现ping多个IP地址并返回结果

[复制链接]
发表于 2018-4-9 20:08:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
场景:工作中经常会遇到ping内网的多个IP地址,看看能不能ping通,或者在局域网内做ping探测,于是写了这个;


import os
import sys

iplist1 = ['Ping Error']
iplist2 = ['Ping Success']
ipprefix = '10.225.49.'
for i in range(69,72):
        ip = ipprefix + str(i)
        print('@@~~~Start to check %s ~~~@@' %ip)
        PING = os.system('ping -c 2 -i 1 %s' %ip)
        if PING:
                print('@@ping error,the %s not reachable' %ip)
                iplist1.append(ip)
        else:
                print('@@Ping success')
                iplist2.append(ip)
print(iplist1)
print(iplist2)


问题:
我这里把PING的结果打印出来过,发现能ping成功的话,PING的返回结果是0,如果ping不成功,返回的是pipe2, 256这样的一个数字,我虽然基于ping的结果来做一个if判断,但我并不知道原因;
求大神赐教
==========================================================
实际执行的结果:

[root@k_10_225_49_68 ~/]# python pingcheck.py
@@~~~Start to check 10.225.49.69 ~~~@@
PING 10.225.49.69 (10.225.49.69) 56(84) bytes of data.
64 bytes from 10.225.49.69: icmp_seq=1 ttl=64 time=0.099 ms
64 bytes from 10.225.49.69: icmp_seq=2 ttl=64 time=0.108 ms

--- 10.225.49.69 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.099/0.103/0.108/0.011 ms
Ping success
@@~~~Start to check 10.225.49.70 ~~~@@
PING 10.225.49.70 (10.225.49.70) 56(84) bytes of data.
From 10.225.49.68 icmp_seq=1 Destination Host Unreachable
From 10.225.49.68 icmp_seq=2 Destination Host Unreachable

--- 10.225.49.70 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 2997ms
pipe 2
ping error,the 10.225.49.70 not reachable
@@~~~Start to check 10.225.49.71 ~~~@@
PING 10.225.49.71 (10.225.49.71) 56(84) bytes of data.
From 10.225.49.68 icmp_seq=1 Destination Host Unreachable
From 10.225.49.68 icmp_seq=2 Destination Host Unreachable

--- 10.225.49.71 ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 2996ms
pipe 2
ping error,the 10.225.49.71 not reachable
['Ping Error', '10.225.49.70', '10.225.49.71']
['Ping Success', '10.225.49.69']
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-23 17:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表