Python realizes scanning LAN active IP

Source: Internet
Author: User
Tags join

This article mainly introduces the implementation of Python scan LAN activity IP (scan online computer), this article directly gives the implementation code, the need for friends can refer to the following

Intranet hosts are automatically assigned IP address, and sometimes need to look at those IP in use, wrote a simple script.

Linux and Windows can be used, with multithreading to ping1-255 all the addresses, inefficient, about 2 minutes. Let's do it and use it.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the same as the same as the other, at the same-------------------------- #-*-coding:utf-8-*-#author: Orangleliu date:2014-11-12 #python2.7.x   "' different platforms, to achieve IP scanning in the intranet,   sometimes need to know the effective IP of the local area network, but do not want to find a specific tool to scan. Using the method Python (Scans IP) '   Import platform import SYS import OS import time I Mport 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)). R Eadlines ()   flag = False for line in list (output): If does line:continue if STR (line). Upper (). Find ("TTL") >=0:FL AG = True Break if Flag:print ' IP:%s is OK * * *%IP_STR   def find_ip (Ip_prefix): ' Give the current 127.0.0, then scan the entire segment for all addresses ' for I in Range (1,256): ip = '%s.%s '% (ip_prefix,i) thread.start_new_thr EAD (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 applied: Python scans 1-255 of all IP addresses.


1 2 3 4 5 6 7 8 D:codehousepythontools>python start time Wed Nov a 18:50:58 2014 ip: is OK * * * * ip:10.

That's it.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.