欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-12-04 12:21:08
python实现扫描ip地址的小程序,具体代码如下所示: import os,time import sys start_time=int(time.time...

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

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地址的小程序,希望对大家有所帮助