列出局域网内可Ping通的IP

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
# -*- coding: utf-8 -*-

import platform
import os
import time
import threading
import socket

live_ip = 0


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()
for line in output:
if str(line).upper().find("TTL") >= 0:
print("ip: %s 在线" % ip_str)
global live_ip
live_ip += 1
break


def find_ip(ip_prefix):
'''''
给出当前的ip地址段 ,然后扫描整个段所有地址
'''
threads = []
for i in range(1, 256):
ip = '%s.%s' % (ip_prefix, i)
threads.append(threading.Thread(target=ping_ip, args={ip, }))
for i in threads:
i.start()
for i in threads:
i.join()


def find_local_ip():
"""
获取本机当前ip地址
:return: 返回本机ip地址
"""
myname = socket.getfqdn(socket.gethostname())
myaddr = socket.gethostbyname(myname)
return myaddr


if __name__ == "__main__":
print("开始扫描时间: %s" % time.ctime())
addr = find_local_ip()
args = "".join(addr)
ip_pre = '.'.join(args.split('.')[:-1])
find_ip(ip_pre)
print("扫描结束时间 %s" % time.ctime())
print('本次扫描共检测到本网络存在%s台设备' % live_ip)


列出局域网内可Ping通的IP
https://xinhaojin.github.io/2022/02/25/列出局域网内可ping通的ip/
作者
xinhaojin
发布于
2022年2月25日
许可协议