思路:轮询:利用同一wifi下ip可被查询的特性,当有人链接上wifi时,进行主机判断。
第一步 (cmd可行性分析)
for /L %i IN (1,1,254) DO ping -w 1 -n 1 192.168.0.%i
第二步
arp -a
第三步,通过python运行cmd,arp -a 获取目录
def get_macs():
# 运行cmd控制窗口,输入“arp -a”,并将内容传递到res中
res = os.popen("arp -a")
# 读取res数据,转换为可读数据
arps = res.read()
# print(arps)
# 将获得的counts中的数据根据“换行符”来进行分割切片
result = arps.split('\n')[3:]
def get_ip_mac(list):
list = list.split(" ")
list = set(list)
list = [i for i in list][1:] #['动态', '192.168.0.101', '2c-5d-34-07-b9-99']
ip, mac = '', ''
for i in list:
if '.' in i:
ip = i
elif '-' in i:
mac = i
else:
continue
list = []
list.append(ip)
list.append(mac)
return list
# 遍历
result = list(map(get_ip_mac, result))[:-1]
ips = [i[0] for i in result]
macs = [i[1] for i in result]
return ips, macs
第四步,再次ping,再次arp,做mac判断
# 需要跟寻的的Mac地址
bossMac = "0e-95-e7-0d-da-cb"
sleep_time = 5
path = sys.path[0]
path = os.path.join(path, 'test.vbs')
while True:
os.system(path)
time.sleep(120)
time.sleep(sleep_time)
ips, macs = get_macs()
print(ips, macs)
is_come = 0
for mac in macs:
if mac == bossMac:
is_come = 2
# 5分钟扫描一次
sleep_time = 300
# 提示报警
choice = g.msgbox(msg="有内鬼,终止交易!", title="OMG")
break
if is_come == 0:
# 5秒钟扫描一次
sleep_time = 5
g.msgbox(msg="一切正常!", title="OMG")
VBS
Set obj = createobject("wscript.shell")
obj.run "cmd /c for /L %i IN (1,1,254) DO ping -w 1 -n 1 192.168.0.%i"
原项目:github
修改:原项目没有重新ping的过程,这个过程是必须的;对代码重新进行了缩进;另外mac存在随机mac和本机(手机)mac,所以这个判断个人觉得没有ip好用。