使用Python扫描路由ip

华盟原创文章投稿奖励计划

用于扫描开放了某些端口的ip

timeout可以设置成1秒或2秒。

local_ips是获取多块网卡上绑定的IP,比如我的IP地址是192.168.1.4和192.168.56.1。

而代码所做的事情就是扫描 [192.168.1.1 ~ 192.168.1.254]  [192.168.56.1 ~ 192.168.56.254] 有哪些IP开放80端口。

虽然有许多现成的扫描工具

但是喜欢PY交易的童鞋不妨可以研究研究

使用Python扫描路由ip

  1. import socket

  2. import threading

  3. routers = []

  4. lock = threading.Lock()

  5. def search_routers(): 

  6.     routers = []

  7.     local_ips = socket.gethostbyname_ex(socket.gethostname())[2]    # get local IP

  8.     all_threads = []

  9.     for ip in local_ips:

  10.         for i in range(1, 255):

  11.             array = ip.split('.')

  12.             array[3] = str(i)

  13.             new_ip = '.'.join(array)

  14.             t = threading.Thread(target=check_ip, args=(new_ip,) )

  15.             t.start()

  16.             all_threads.append(t)

  17.     for t in all_threads:

  18.         t.join()

  19. def check_ip(new_ip):

  20.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  21.     s.settimeout(1)

  22.     result = s.connect_ex((new_ip, 80))

  23.     s.close()

  24.     if result == 0:

  25.         lock.acquire()

  26.         print new_ip.ljust(15), ' port 80 is open'

  27.         routers.append((new_ip, 80))

  28.         lock.release()

  29. print 'Searching for routers, please wait...'

  30. search_routers()

本文原创,作者:congtou,其版权均为华盟网所有。如需转载,请注明出处:https://www.77169.net/html/249512.html

发表回复