【问题标题】:Python 2D array and loopPython 2D 数组和循环
【发布时间】:2018-04-25 14:05:58
【问题描述】:

我是 Python 新手,正在尝试设置一个脚本来检查本地网络上特定 wifi 连接的 IOT 设备的状态。

基本上,该脚本采用我要检查的 mac 硬件 ID; ping 网络以触发 ARP;在对 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)

【问题讨论】:

    标签: python arrays memory garbage-collection


    【解决方案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
    

    【讨论】:

    • 两个 sn-ps 应该做同样的事情。唯一不同的是您可以在 MAC_addresses 中添加更多列表。
    • 非常感谢。我认为您的答案看起来不错,但我收到此类型错误 - 如果有的话(str_output 中的地址 for input_address_list 中的地址):TypeError: 'in ' 需要字符串作为左操作数,而不是列表。
    • 你这样叫detect_mac(MAC_addresses)吗?
    • 是的。 Traceback (most recent call last): File "/home/pi/Documents/detect_macs1.py", line 43, in <module> detect_mac(MAC_addresses) File "/home/pi/Documents/detect_macs1.py", line 29, in detect_mac if any(address in str_output for address in address_list): File "/home/pi/Documents/detect_macs1.py", line 29, in <genexpr> if any(address in str_output for address in address_list): TypeError: 'in <string>' requires string as left operand, not list
    • 我想我修好了。您应该考虑为单个地址仅使用字符串而不是 1d-1length 数组:MAC_address_list1 = ("58:e2:zz:xx:28:d7")
    猜你喜欢
    • 2020-10-28
    • 2015-04-30
    • 2021-05-21
    • 2014-06-22
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多