-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscanlanip.py
64 lines (54 loc) · 1.63 KB
/
scanlanip.py
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
61
62
63
64
##!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: orangleliu date: 2014-11-12
#python2.7.x ip_scaner.py
#改为python3版本 by 0han date: 2016.9.9
#python3.x scanlanip.py
'''''
不同平台,实现对所在内网端的ip扫描
有时需要知道所在局域网的有效IP地址,但是又不想找特定的工具来扫描。
使用方法 python ip_scaner.py 192.168.1.1
(会扫描192.168.1.1-255的ip)
'''
import platform
import sys
import os
import time
import _thread
def get_os():
'''''
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):
'''''
给出当前的127.0.0 ,然后扫描整个段所有地址
'''
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())