Using Python to scan Active ip addresses in a LAN

Source: Internet
Author: User

Using Python to scan Active ip addresses in a LAN

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. For more information, see

Intranet hosts are automatically assigned ip addresses. 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.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

#-*-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 ()

When it is an application: python ip_scaner.py 192.168.31.1 will scan all IP addresses from 1.

?

1

2

3

4

5

6

7

8

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.

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.