有没有办法使用操作系统为了什么?

2024-09-30 01:27:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我制作了一个bash脚本,它的目的是打印设备连接到热点时的IP、MAC地址、信号强度和设备名称等信息。我想改用python,所以我正在重写它。你知道吗

当我执行代码时,它只打印mac地址和信号强度,似乎与for循环无关。此外,我还需要使用一些字符串连接,例如在内部使用python变量操作系统()但为了做到这一点,我读到我必须使用字符串连接作为一种简单的方法来实现它。你知道吗

第一个循环用于查找无线接口wlan0,第二个循环用于检索两个列表的数据:maclist和signallist,其中存储mac地址和信号强度。你知道吗

print("IP address" "\tHostname" "\tMAC address" "\tSignal")
leasefile="/var/lib/misc/dnsmasq.leases"
displayInterface = "iw dev | grep Interface | cut -f 2 -s -d\" \""

for interface in os.popen(displayInterface):

retrieveMac = "iw dev wlan0 station dump | grep Station | cut -f 2 -s -d\" \""
retrieveSignal = "iw dev wlan0 station dump | grep signal: | awk '{print $2}'"
maclist = []
listLength = len(maclist)
maclist.append(os.system(retrieveMac))
signallist = []
signallist.append(os.system(retrieveSignal))

    for i in range(listLength):
        ip = "UNKN"
        host = ""
        retrieveIP = "cut -f 2,3,4 -s -d" " "+"$"+leasefile+"| grep $"+maclist[i]+" | cut -f 2 -s -d\" \""
        retrieveHost = "cut -f 2,3,4 -s -d" " "+"$"+leasefile+"| grep $"+maclist[i]+" | cut -f 3 -s -d\" \""
        ip = os.system(retrieveIP)
        host = os.system(retrieveHost)

        print(ip +"\t"+host +"\t"+maclist[i] +"\t"+signallist[i])

输出如下:

IP address  Hostname    MAC address Signal
b8:27:eb:...
b4:9d:0b:...
-43
-32

应该是这样的:

IP address  Hostname    MAC address      Signal
192.16...   dev name    b8:27:eb:...    -43
192.16...   dev name    b4:9d:0b:...    -32

数据不是问题所在,因为命令本身可以正常工作,但这是布局和使用时显示的事实操作系统()

在这行代码中,我尝试使用interface而不是wlan0,但没有成功

retrieveMac = "iw dev wlan0 station dump | grep Station | cut -f 2 -s -d\" \""

retrieveMac = "iw dev "+interface+" station dump | grep Station | cut -f 2 -s -d\" \""

Tags: deviposaddressmacdumpsystemgrep

热门问题