How do I get Python to know what Wifi the user is connected to?
Asked Answered
L

6

12

I'm 14, pardon my Python knowlege. I'm trying to make this program that will only run while I'm at school (on the school's Wifi) using an if/else statement like this:

if ontheschoolwifi:
     Keep running the program
else:
     close the program because im not at school and wont need it

I'd like to know how to let python know how to get what wifi it is connected to. Thank you, in advance, for your help :)

Loment answered 20/10, 2015 at 2:51 Comment(3)
Which operating system? Programming languages are just languages, but wifi is a hardware interface which is generally handled by the operating system and very loosely speaking, the OS is responsible for exposing an application programming interface (API) which programs, written in a language (say Python) can use to query/interact with.Swoon
@Swoon Im running Windows 10.Loment
Take a look at this thread and its linked threads: Associating my Windows computer to a wifi AP with PythonSwoon
M
13
import subprocess

if "SchoolWifiName" in subprocess.check_output("netsh wlan show interfaces"):
    print "I am on school wifi!"
Montagnard answered 20/10, 2015 at 8:10 Comment(0)
T
10

here some code that actually works, the other answers did not work for me on Windows...

import subprocess

wifi = subprocess.check_output(['netsh', 'WLAN', 'show', 'interfaces'])
data = wifi.decode('utf-8')
if "school_wifi_name" in data:
    print("connected to speccific wifi")
else:
    print("not connected")
Tindle answered 12/5, 2021 at 12:52 Comment(0)
V
6

For Mac OS query the airport using os module. "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I" Then, look the name assigned to SSID by your school. It should be something similar for the other operating systems.

Vend answered 20/10, 2015 at 3:2 Comment(0)
G
2

This will help you out to get the network name.

import subprocess

subprocess_result = subprocess.Popen('iwgetid',shell=True,stdout=subprocess.PIPE)
subprocess_output = subprocess_result.communicate()[0],subprocess_result.returncode
network_name = subprocess_output[0].decode('utf-8')
Godwin answered 29/12, 2020 at 11:37 Comment(0)
D
2

This should be the best solution:

import subprocess

if 'SchoolWifiName' in subprocess.check_output("netsh wlan show interfaces").decode('utf-8'):
    print('I am on school wifi!')
Deguzman answered 12/8, 2022 at 13:14 Comment(2)
This is the same, essentially, as the current top answer, except adapted for Python 3.Tasha
@DominikStańczak0 That's right we live in 2022.Deguzman
L
1
import subprocess

print(str(subprocess.check_output(['iwgetid -r'], shell=True)).split('\'')[1][:-2])

will print the current Wi-Fi SSID

To check if the SSID has the desired name:

import subprocess

if 'School_wifi_name' in str(subprocess.check_output(['iwgetid -r'], shell=True)).split('\'')[1][:-2]:
  print('I am on school Wi-Fi!')
Laktasic answered 31/1, 2023 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.