python实现扫描ip地址的小程序

时间:2022-09-01 17:18:57

python实现扫描ip地址的小程序,具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os,time
import sys
start_time=int(time.time())
ip_true = open('ip_true.txt','w+')
ip_false = open('ip_false.txt','w+')
iphost = []
ipbegin = (input(u'请输入起始查询ip: '))
ipend = input(u'请输入终止查询ip: ')
ip1 = ipbegin.split('.')[0]
ip2 = ipbegin.split('.')[1]
ip3 = ipbegin.split('.')[2]
ip4 = ipbegin.split('.')[-1]
ipend_last = ipend.split('.')[-1]
count_true,count_false = 0,0
for i in range(int(ip4)-1,int(ipend_last)):
  ip = str(ip1+'.'+ip2+'.'+ip3+'.'+ip4)
  int_ip4 = int(ip4)
  int_ip4 += 1
  ip4 = str(int_ip4)
  return1=os.system('ping -n 1 -w 1 %s'%ip)
  if return1:
    print('ping %s is fail'%ip)
    ip_false.write(ip+'\n')
    count_false += 1
  else:
    print('ping %s is ok'%ip)
    ip_true.write(ip+'\n')
    count_true += 1
ip_true.close()
ip_false.close()
end_time = int(time.time())
print("time(秒):",end_time - start_time,"s")
print("ping通的ip数:",count_true,"  ping不通的ip数:",count_false)

总结

以上所述是小编给大家介绍的python实现扫描ip地址的小程序,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://blog.csdn.net/qq_18831583/article/details/80250644