This article mainly introduces how to use Python to scan active ip addresses in the Lan (scan online computers). This article provides the implementation code directly. if you need it, refer to the intranet hosts where ip addresses are automatically allocated, sometimes you need to check which ip addresses are in use and write a simple script.
Linux and windows can be used, with multithreading to ping1-255 all addresses, efficiency is not high, 2 minutes or so. Join and use it first.
#-*-Coding: UTF-8-*-# author: orangleliu date: 2014-11-12 # python2.7.x ip_scaner.py ''' different platforms, to scan the ip address on the intranet, you sometimes need to know the valid ip address of the LAN, but you do not want to find a specific tool for scanning. Python ip_scaner.py 192.168.1.1 (ip address 192.168.1.1-255 is scanned) ''' import platform import sys import OS import time import thread def get_ OS (): '''''get OS ''' OS = platform. system () if OS = "Windows": return "n" else: return "c" def ping_ip (ip_str): cmd = ["ping ", "-{op }". format (op = get_ OS (), "1", ip_str] output = OS. popen ("". join (cmd )). readlines () flag = False for line in list (output): if not line: continue if str (line ). upper (). find ("TTL")> = 0: flag = True break if flag: print "ip: % s is OK ***" % ip_str def find_ip (ip_prefix ): ''' indicates the current 127.0.0, and then scans all the CIDR blocks ''' for I in range (1,256): ip = '% s. % s' % (ip_prefix, I) thread. start_new_thread (ping_ip, (ip,) time. sleep (0.3) if _ name _ = "_ main _": print "start time % s" % time. ctime () commandargs = sys. argv [1:] args = "". join (commandargs) ip_prefix = '. '. join (args. split ('. ') [:-1]) find_ip (ip_prefix) print "end time % s" % time. ctime ()
When it is an application: python ip_scaner.py 192.168.31.1 will scan all IP addresses from 1.
D:\CodeHouse\python\tools>python ip_scaner.py 10.0.1.38 start time Wed Nov 12 18:50:58 2014 ip: 10.0.1.1 is ok *** ip: 10.0.1.2 is ok *** ip: 10.0.1.24 is ok *** ip: 10.0.1.38 is ok *** ip: 10.0.1.39 is ok *** end time Wed Nov 12 18:52:16 2014
That's it.