高效的python IP扫描仪

时间:2018-11-25 00:31:22

标签: python python-3.x multithreading networking ping

尝试以省时的方式在python中进行IP扫描..我正在尝试扫描192.168.0.0/20,并且约有1500台主机还活着..试图创建可以在两分钟内运行的内容..以下是我当前的代码:..有什么建议吗?这段代码似乎效率不高..

def check_ping(hostname):
  response = os.system("ping -c 1 -W 1 " + hostname+" >/dev/null 2>&1")
  if response == 0:
      print hostname
      pingstatus = True
  else:
      pingstatus = False

  return pingstatus

def ipscan():
 output_ip = []
 thehost = "192.168."
 for i in range(0,16):
    host = thehost + str(i) + "."
    for j in  range(0,256):
        newhost = host + str(j)
        output_ip.append(newhost)
        newhost = host

  return output_ip

hosts = ipscan()
num_hosts = len(hosts)
for k in range(num_hosts):
   temp = hosts[k]
   t=threading.Thread(target=check_ping, args=(temp,))
   t.start()  

1 个答案:

答案 0 :(得分:0)

您可以尝试使用ipaddress.hosts(),它会在网络中的可用主机上返回迭代器,之后您就可以对其进行迭代。最好是