python-扫描某一网段下的ip

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 ############################
 4 #File Name: ipscaner.py
 5 #Author: frank
 6 #Mail: frank0903@aliyun.com
 7 #Created Time:2017-06-05 16:06:37
 8 ############################
 9 
10 
11 import platform
12 import sys
13 import os
14 import time
15 import thread
16 
17 def get_os():
18     '''
19     get os 类型
20     '''
21     os = platform.system()
22     if os == "Windows":
23         return "n"
24     else:
25         return "c"
26     
27 def ping_ip(ip_str):
28     cmd = ["ping", "-{op}".format(op=get_os()),
29            "1", ip_str]
30     output = os.popen(" ".join(cmd)).readlines()
31     
32     flag = False
33     for line in list(output):
34         if not line:
35             continue
36         if str(line).upper().find("TTL") >=0:
37             flag = True
38             break
39     if flag:
40         print "ip: %s is ok ***"%ip_str
41 
42 def find_ip(ip_prefix):
43     for i in range(1,255):
44         ip = '%s.%s'%(ip_prefix,i)
45         thread.start_new_thread(ping_ip, (ip,))
46         time.sleep(0.3)
47     
48 if __name__ == "__main__":
49     print "start time %s"%time.ctime()
50     commandargs = "192.168.1.1"
51     args = "".join(commandargs)    
52     
53     ip_prefix = '.'.join(args.split('.')[:-1])
54     find_ip(ip_prefix)
55     print "end time %s"%time.ctime()
原文地址:https://www.cnblogs.com/black-mamba/p/7061347.html