The script we write in this article is intended for use with an OLED P4wnP1 running BeBox's P4wnP1 Kali image.
How to Turn Off WiFi in Kali Linux
sudo ifconfig wlan0 down
I'm running Kali for Raspberry Pi Zero W.
How to Write a Python Script to Show WiFi Status on OLED Screen
How can we parse the output of "sudo ifconfig wlan0 down" with Python and write a script to display the WiFi status on an OLED screen?
Let's find out.
1. Start with what you know: the basics - iwconfig
We know that we can check the internet wireless status with "iwconfig" in Linux (like I said, I'm using Raspberry Pi 0 W).
So I ran "iwconfig" while the WiFi was turned on. It gave me a certain output.
2. Try something and see what happens
THEN, I ran the command at the top of this page to turn WiFi OFF.
Once again, I ran "iwconfig" to check the WiFi status now that I turned it off.
BOOM - I noticed a difference in the output of "iwconfig".
From a (Python) programming standpoint, this is great!
Why? Because it gives us something to "key off of" - or identify using a few lines of code to confirm whether or not the WiFi is actually turned off.
3. Re-focus and get to thepoint
That's kind of the whole point of this article: we want to..
- Turn off WiFi (DONE)
- Verify that WiFi was turned off (IN PROGRESS)
- Parse the output of iwconfig (optional - we already can confirm if WiFi is turned Off or On using the previous step)
- Display our output on the OLED screen using BeBox OLED P4wnP1 (which is just a customized OS image for Raspberry Pi)
Here's the output from iwconfig BEFORE and AFTER running the command to turn off WiFi in Kali Linux:
root@kali:~# sudo ifconfig wlan0 down
Before
root@kali:~# iwconfig
wlan0 IEEE 802.11 Mode:Master Tx-Power=31 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
After
wlan0 IEEE 802.11 Mode:Master
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
The before and after output seems to appear identical at first.
But I do see one difference:
After WiFi is turned off, the iwconfig output does not show "Tx-Power" or "=31 dBm" at all!
(This totally makes sense - since the device should not have any transmission power while WiFi is turned Off.)
Therefore, we can use Python's "in" operator to see if "Tx-Power" is in our iwconfig output.
For example..
We can write a command to run "iwconfig" (see further down below). Then in a Python script we could say something like this to parse the output of iwconfig and confirm if WiFi is actually turned off:
# Set a variable to check if a string exists in another string
is_tx_in_iwconfig = "Tx-Power" in iwconfig_results
# Check if the variable is True
if is_tx_in_iwconfig == True:
# Do this if True
print("Do something")
else:
# Do this if not True
print("Do something else")
How to Run a Command Using Python
We can simply use the python subprocess module to run our ifconfig / iwconfig commands.
Let's add this into our wifi-off.py script (near the top of the page) to "try" to run iwconfig:
try
iwconfig_results = subprocess([blah])
except:
Re-cap: Time to Put it All Together
At this point, we basically have two chunks of code:
- A chunk that runs ifconfig / iwconfig
- A chunk that checks if iwconfig output shows "Tx-Power" or not (aka whether or not WiFi is turned ON)
So we just put these chunks in the right order. Then run it and see what happens!
Note: We probably want to add a brief pause inbetween the two chunks of code so that we give our device enough time to cooperate.
We can do this with Python3's "time.sleep(x)" method. So we'll drop a number 2 (lol) or 3 in there for our script to breathe.
Update: Wifi-on.py and wifi-off.py are done!
They both successfully turn on/off WiFi and then check using "iwconfig" to see if it's actually on/off.
To do:
Write wifi-toggle.py to turn WiFi on if it's off, or off if it's on.
Write wifi-yntog.py to Display these lines on OLED:
- WiFi is (on/off) (toggle-on/toggle-off icon)
- Turn (off/on)?
- (YES)
- (NO)
Then just have it run wifi-toggle.py if the user selects Yes.
Or write wifi-question.py that takes parameters (line1, line2, option1, option2, command) for multi-use. Do this and adapt it from the latest wifi-disp.py script. May need to adjust the canvas size to 4 lines.
WiFi-Check Process / Flow
Is WiFi On? If no, display wifi-yntog.py.
Is WiFi connected to a network SSID? If no, display __________.
Is WiFi connected to Internet? If no, display __________.
Show option to run speed test if Connected to Internet.
WiFi Status "Cases":
WiFi OFF (Display wifi-yntog.py - asks if user wants to turn on WiFi)
WiFi ON - SSID No (Display "No WiFi Connection" - ask if user wants to Connect to WiFi)
WiFi ON - SSID Yes - Internet No (Display "Connected w/o Internet")
WiFi ON - SSID Yes - Internet Yes (Display "Connected to Internet!")
Note: a good, concise little alternative to iwconfig is:
ip link show wlan0
Make a WiFi Status script with dashboard type display?
ex:
📶 WiFi is ON (🔘)
🖧 NETGEAR69 (🔘)
🌐 Internet Access (🔘)
Changes to make to wifi-disp.py (or make a fork of it):
- Make font 1 or 2px smaller
- Make 4 or 5 lines
- Remove side arrows
- Widen the canvas
Fix this bug in wifi-wrap.py:
WiFi scan error message:
wlan0 Interface doesn't support scanning : Network is down
Number of APs detected:
0