#-*-Coding: UTF-8 -*- # Author: orangleliu date: 2014-11-12 # Python2.7.x ip_scaner.py ''''' Different platforms enable ip address scanning on the Intranet Sometimes you need to know the valid ip address of the local area network, but you do not want to find a specific tool to scan. How to Use python ip_scaner.py 192.168.1.1 (The ip address 192.168.1.1-255 will be scanned) ''' Import platform Import sys Import OS Import time Import thread Def get_ OS (): ''''' Get OS type ''' 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 ): ''''' Returns the current 127.0.0, and then scans all the addresses of the entire segment. ''' 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 () |