实践:局域网活动ip扫描

[root@www dns]# cat SJ3.py

!/usr/bin/env python

import platform
import sys
import os
import time
import thread

def get_os():
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):
for i in range(1,256):
ip = '%s.%s'%(ip_prefix,i)
thread.start_new_thread(ping_ip, (ip,))
time.sleep(0.2)

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

[root@www dns]#

测试:

脚本后面跟上网段
[root@www dns]# python SJ3.py 192.168.1.0
start time Mon Jan 11 11:38:50 2016
ip: 192.168.1.1 is ok ***
ip: 192.168.1.104 is ok ***
ip: 192.168.1.106 is ok ***
ip: 192.168.1.141 is ok ***
ip: 192.168.1.140 is ok ***
Do you want to ping broadcast? Then -b
end time Mon Jan 11 11:39:41 2016

你可能感兴趣的:(实践:局域网活动ip扫描)