Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python2D数组和循环_Python_Arrays_Memory_Garbage Collection - Fatal编程技术网

Python2D数组和循环

Python2D数组和循环,python,arrays,memory,garbage-collection,Python,Arrays,Memory,Garbage Collection,我是Python新手,正在尝试设置一个脚本来检查本地网络上特定wifi连接物联网设备的状态 从本质上讲,脚本采用我想要检查的mac硬件ID;ping网络以触发ARPs;在对arp-a的响应中搜索相关mac ID,如果成功定位,则会打印日期和时间 两个问题- 1) 目前,我只是在搜索2个mac地址,但未来很可能是20个。如何修改我的detect_mac函数以获取二维数组(或类似数组)并循环遍历结果,以避免重复如果有(str_输出中的地址用于输入地址列表1中的地址):行20次 2) 如果我让这个脚本

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

从本质上讲,脚本采用我想要检查的mac硬件ID;ping网络以触发ARPs;在对arp-a的响应中搜索相关mac ID,如果成功定位,则会打印日期和时间

两个问题- 1) 目前,我只是在搜索2个mac地址,但未来很可能是20个。如何修改我的detect_mac函数以获取二维数组(或类似数组)并循环遍历结果,以避免重复
如果有(str_输出中的地址用于输入地址列表1中的地址):
行20次

2) 如果我让这个脚本持续运行(运行之间有10分钟的计时器),python最终会崩溃吗?是否有一些垃圾收集、日志溢出等我需要考虑?< /P>
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)

我当然可以回答第1点):


我当然可以回答第1点):


这两个代码段应该做同样的事情。唯一不同的是,你们可以在MAC_地址中添加更多的列表。非常感谢。我认为你的答案看起来不错,但我得到了这种类型的错误-如果有(输入地址列表中的地址在str\u输出中):TypeError:“in”需要字符串作为左操作数,而不是列表。你是否像这样调用
detect\u mac(mac\u addresses)
?是的<代码>回溯(最后一次调用):文件“/home/pi/Documents/detect_macs1.py”,第43行,在detect_mac(mac_地址)文件“/home/pi/Documents/detect_macs1.py”第29行,在detect_mac if any(地址列表中地址的str_输出中的地址):文件“/home/pi/Documents/detect_macs1.py”,第29行,如果有的话(在地址输出中的地址输出地址):Type Error:“in”需要字符串作为左操作数,而不是列表< /代码>。我想我已经修复了它。您应该考虑只使用字符串而不是1D1String数组来为单个地址:MaqAddiSrsList1=(“58:E2:ZZ:XX:28:D7”)。这两个代码段应该是相同的。唯一的区别是,您可以在MAC_地址中添加更多的列表。非常感谢。我认为您的答案看起来不错,但我得到了以下类型错误-如果有(str_输出中的地址用于输入_地址_列表中的地址):TypeError:'in'需要字符串作为左操作数,而不是列表。是否调用
detect_MAC(MAC_地址)
像这样吗?是。
回溯(最近一次调用):文件“/home/pi/Documents/detect\u macs1.py”,第43行,在detect\u mac(mac\u地址)文件“/home/pi/Documents/detect\u macs1.py”,第29行,在detect\u mac(如果有)(地址列表中地址的stru输出中的地址):文件“/home/pi/Documents/detect\u macs1.py”第29行,如果有的话(在地址输出中的地址输出地址):Type Error:“in”需要字符串作为左操作数,而不是列表< /代码>。我想我已经修复了它。您应该考虑只使用字符串而不是1D1String数组来为单个地址:MaqAddiSrsList1=(“58:E2:ZZ:XX:28:D7”)。
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