0

I've made a bash script which aims to print information like IP, MAC address, signal strength and device name about a device when it's connected to a hotspot. I want to use python instead so I'm rewriting it.

When I execute the code it only prints the mac address and the signal strength and doesn't seem to do anything with the for loops. Also I need to use some string concatenation, for example using python variables inside os.system() but for doing so I read I have to use string concatenation as an easy way to implement it.

The first loop is to find the wireless interface, wlan0 and the second is for retrieve the data of both lists: maclist and signallist where are stored the mac address and signal strengths.

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])

The output is something like this:

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

It should be like:

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

The data is not the problem because the commands works fine on their own but it's both the layout and the fact of being displayed when using os.system()

P.D In this line of code I tried to use interface instead of wlan0 but it didn't work

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\" \""
Alvaromr7
  • 25
  • 8
  • Before the `for`-loop: `for i in range(listLength)`, maybe try checking (by `print`ing) the contents of `listLength` and see if contains expected list contents, and if so, see if it has string elements that contain newline characters ('\n'). – chickity china chinese chicken May 07 '19 at 00:52
  • 1
    `os.system()` does not return any text from the command that was run, it just returns the command's exit code. There are various functions in the `subprocess` module that let you work with the actual command output. – jasonharper May 07 '19 at 00:54
  • davedwards I tried what you said and I have the values I want separated by a newline each, but apparently the for loop does anything with them. – Alvaromr7 May 07 '19 at 14:07

0 Answers0