Saturday, June 3, 2017

Python to get ssid and IP address

Python example to get the ssid of connected network, and my IP address. Work on both Python 2 and 3.

pyWireless.py
import os

#'\' is used to splite pythone line
ipaddress = os.popen("ifconfig wlan0 \
                     | grep 'inet addr' \
                     | awk -F: '{print $2}' \
                     | awk '{print $1}'").read()
ssid = os.popen("iwconfig wlan0 \
                | grep 'ESSID' \
                | awk '{print $4}' \
                | awk -F\\\" '{print $2}'").read()

print("ssid: " + ssid)
print("ipaddress: " + ipaddress)



remark:
The ssid should have no space, otherwise the parts after space will be missed.

Tested on Raspberry Pi 2 with WiFi dongle.

No comments: