python调用ping
A. python 判断是否ping通
通过mac查ip
方法一:用ARP -A 查询
这种方法只能查到与本机通讯过(20分钟内)的主机MAC地址和IP地址。可在远程主机所属网段中的任一台主机上运行此命令,这样可查出IP欺骗类病毒的主机。
方法二:用专用软件查,如nbtscan
命令方式是:nbtscan -r 网络号/掩码位,这种方法可查询某网段的所有IP与MAC对应关系,但装有防火墙的主机则禁止查询。
方法三: 如果所连交换机有网管功能,可用ARP SHOW 命令显示交换机的arp缓存信息,这种方式基本可查询所有的IP 与MAC地址,但只有网管才有这个权限。
方法四:用sniffer类的嗅探软件抓包分析,packet中一般都含用IP地址与MAC地址。
方法五:用solarwinds类软件中的MAC ADDRESS DISCOVERY查询,但这个工具好象不能跨网段查询。
B. python 判断ip是否能ping通
python编程下,检查IP是否能ping通,并且分别导入两个文件,代码如下:
#!/usr/bin/python
#-*- coding:gb18030 -*-
'''
Created on 2015-7-7
#判断文件中的ip是否能ping通,并且将通与不通的ip分别写到两个文件中
#文件中的ip一行一个
'''
import time,os
start_Time=int(time.time()) #记录开始时间
def ping_Test():
ips=open('host.txt','r')
ip_True = open('ip_True.txt','w')
ip_False = open('ip_False.txt','w')
count_True,count_False=0,0
for ip in ips.readlines():
ip = ip.replace('\n','') #替换掉换行符
return1=os.system('ping -n 2 -w 1 %s'%ip) #每个ip ping2次,等待时间为1s
if return1:
print 'ping %s is fail'%ip
ip_False.write(ip) #把ping不通的写到ip_False.txt中
count_False += 1
else:
print 'ping %s is ok'%ip
ip_True.write(ip) #把ping通的ip写到ip_True.txt中
count_True += 1
ip_True.close()
ip_False.close()
ips.close()
end_Time = int(time.time()) #记录结束时间
print "time(秒):",end_Time - start_Time,"s" #打印并计算用的时间
print "ping通数:",count_True," ping不通的ip数:",count_False
ping_Test()
C. python2.7 请帮忙写一个函数确定某个ip是否能ping通。我的这个不行
你这样直接使用os.system("ping")==0是不行的,执行ping命令后跟cmd执行一样,也会返回类似于ttl=245 time=36.798 ms这样的信息。所以你要做的是:
在os.system("ping -n 1 "+ip)的返回结果中查找是否存在"TTL="这样的字符,如果存在表示ping通了,不存在就表示超时
D. python执行cmd命令,怎么让他执行类似Ctrl+C效果将其结束命令
python执行cmd命令,怎么让他执行Ctrl+C的效果结束命令?
我在用ping监控一个服务器的网络状态,我执行了ping命令后,他会一直这么ping下去,不停止,怎么让他在10秒后执行ctrl+c的效果
def re(cmd): while True:
os.system(cmd);re("ping 192.168.1.1 -t")
他会这样一直ping下去,想了半天也想不出怎么让他10秒后执行ctrl+c结束的执行效果,请教大神,怎么让他执行结束命令;
10秒后停止命令,类似执行ctrl+c的效果;
[root@jenkins xxxx]# time ping .com -w 10s
PING .com (220.181.57.217) 56(84) bytes of data.64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=1 ttl=52 time=4.07 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=2 ttl=52 time=26.9 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=3 ttl=52 time=6.78 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=4 ttl=52 time=12.9 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=5 ttl=52 time=3.86 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=6 ttl=52 time=3.30 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=7 ttl=52 time=5.63 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=8 ttl=52 time=11.2 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=9 ttl=52 time=4.30 ms64 bytes from 220.181.57.217 (220.181.57.217): icmp_seq=10 ttl=52 time=4.17 ms
--- .com ping statistics ---10 packets transmitted, 10 received, 0% packet loss, time 9013ms
rtt min/avg/max/mdev = 3.300/8.328/26.972/6.954 ms
real 0m10.006s
user 0m0.003s
sys 0m0.006s
不知道能不能满足你的需求
E. 初学者Python os.system自动ping请帮忙看看怎么修改
def ping(host): '''ping 1次指定地址''' import subprocess,traceback, platform if platform.system()=='Windows': cmd = 'ping -n %d %s'%(1,host) else: cmd = 'ping -c %d %s'%(1,host) try: p = subprocess.Popen(args=cmd, shell=True, s...
F. python3 判断IP地址是否ping通
os.system(‘comand’) 会执行括号中的命令,如果命令成功执行,这条语句返回0,否则返回1。
要想得到标准输出,可以使用os.popen(cmd)
import os
p=os.popen("ping 192.168.2.129")
x=p.read()
p.close()
if x.count('temeout'):
print("ping不通")
else:
print("ping通了")
G. python编写ping程序出现socket.error: [Errno 10013] 错误
错误号的解释
以一种访问权限不允许的方式做了一个访问套接字的尝试。
试试管理员权限,不行换到xp上试试。
H. 我想使用python写一个循环执行terminal下面的命令,比如我现在要ping 192.168.1.1,我要循环ping 3次
>>> import os
>>> os.system('ping -c 3 10.34.40.22')
PING 10.34.40.22 (10.34.40.22) 56(84) bytes of data.
64 bytes from 10.34.40.22: icmp_seq=1 ttl=64 time=0.501 ms
64 bytes from 10.34.40.22: icmp_seq=2 ttl=64 time=0.289 ms
64 bytes from 10.34.40.22: icmp_seq=3 ttl=64 time=0.277 ms
--- 10.34.40.22 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.277/0.355/0.501/0.105 ms
0
非常简单的,哈哈