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\" \""