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 ip_scaner.py   "' 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 ip_scaner.py 192.168.1.1 (Scans 192.168.1.1-255 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 ip_scaner.py 192.168.31.1 scans 1-255 of all IP addresses.

?

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

That's it.

Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.