我是Python的新手,我正在尝试设置一个脚本来检查本地网络上特定wifi连接的IOT设备的状态。
本质上,脚本会获取我想要检查的mac硬件ID; ping网络触发ARPs;搜索arp-a的响应,查找相关的mac ID,如果成功找到,则打印出日期和时间。
两个问题 -
1)目前我正在搜索2个mac地址但是前进它更可能是20个。如何修改我的detect_mac函数以获取二维数组(或类似)并循环遍历结果是为了避免重复if any(address in str_output for address in input_address_list1):
行20次。
2)如果我让这个脚本不断运行(它在运行之间有10分钟的计时器),这个python最终会崩溃吗?是否有某种垃圾收集,日志溢出等我需要考虑?
import pdb, os
import subprocess
import re
import time
from subprocess import Popen, PIPE, DEVNULL
lower=1
upper=25
MAC_address_list1 = ["58:e2:zz:xx:28:d7"]
MAC_address_list2 = ["08:05:xx:zz:75:c5"]
MAC_address_list3 = ["##:##:##:##:##:##"]
MAC_address_list4 = ["##:##:##:##:##:##"]
p = {}
# Get The Current Date and Time
def getdatetime():
import time
return time.strftime("%H:%M %d/%m/%Y ")
def detect_mac(input_address_list1, input_address_list2):
# Assign list of devices on the network to "output"
output = subprocess.check_output("arp -a", shell=True)
str_output = output.decode("utf-8")
if any(address in str_output for address in input_address_list1):
print(getdatetime() + str(input_address_list1))
if any(address in str_output for address in input_address_list2):
print(getdatetime() + str(input_address_list2))
#sleep 10 minutes
time.sleep(600)
return True
while 1:
# ping all IPs in range to make ARP available
for i in range(lower,upper):
ip = "192.168.1.%d" % i
p[ip] = Popen(['ping', '-n', '-w5', '-c3', ip], stdout=DEVNULL)
detect_mac(MAC_address_list1, MAC_address_list2)
答案 0 :(得分:2)
我当然可以回答第1点):
import pdb, s
import subprocess
import re
import time
from subprocess import Popen, PIPE, DEVNULL
lower=1
upper=25
MAC_address_list1 = ["58:e2:zz:xx:28:d7"]
MAC_address_list2 = ["08:05:xx:zz:75:c5"]
MAC_address_list3 = ["##:##:##:##:##:##"]
MAC_address_list4 = ["##:##:##:##:##:##"]
MAC_addresses = ([MAC_address_list1],[MAC_address_list2])
[...]
def detect_mac(input_address_list):
# Assign list of devices on the network to "output"
output = subprocess.check_output("arp -a", shell=True)
str_output = output.decode("utf-8")
for address_list in input_address_list:
if any(address[0] in str_output for address in address_list):
print(getdatetime() + str(address_list))
#sleep 10 minutes
time.sleep(600)
return True