[英]Python 2D array and loop
I'm new to Python and am attempting to set up a script to check the status of specific wifi connected IOT devices on my local network. 我是Python的新手,正在尝试设置脚本来检查本地网络上特定的wifi连接的IOT设备的状态。
Essentially, the script takes the mac hardware IDs I want to check; 本质上,该脚本采用了我要检查的mac硬件ID。 pings the network to trigger ARPs's; ping网络以触发ARP; searches the response to arp-a for the relevant mac ID's which are printed with the date and time if located successfully. 搜索对arp-a的响应以找到相关的Mac ID,如果成功找到,则显示日期和时间。
Two questions- 1) At the moment I am just searching this for 2 mac addresses however going forward it's more likely to be 20. How can I amend my detect_mac function to take a two-dimensional array (or similar) and loop through the results to avoid having to repeat the if any(address in str_output for address in input_address_list1):
line 20 times. 两个问题-1)目前,我只是在其中搜索2个mac地址,但是以后很可能是20个。如何修改我的detect_mac函数以采用二维数组(或类似数组)并遍历结果为了避免必须重复if any(address in str_output for address in input_address_list1):
重复20行。
2) If I leave this script running constantly (it has a 10 minute timer between runs) will this python eventually crash? 2)如果我让此脚本持续运行(运行之间有10分钟的计时器),此python最终会崩溃吗? Is there some sort of garbage collection, log overflow etc I would need to consider? 我是否需要考虑某种垃圾收集,日志溢出等问题?
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)
I can certainly answer point 1): 我当然可以回答问题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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.