Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save a740122/f37c990284e67dbd138b079447d5d748 to your computer and use it in GitHub Desktop.
Save a740122/f37c990284e67dbd138b079447d5d748 to your computer and use it in GitHub Desktop.
Easy Wifi Connection via Python
#!/bin/python
#
# Connects to a Wifi network
#
import os
def commandExists(command):
def canExecute(file):
return os.path.isfile(file) and os.access(file, os.X_OK)
for path in os.environ["PATH"].split(os.pathsep):
file = os.path.join(path, command)
if canExecute(file):
return True
return False
print "Checking for root access..."
user = os.popen("whoami").read()
if 'root' not in user:
print 'You need to be the root user to run this program and you are running as '+user+' Try sudo python <ScriptName>.py'
print 'Exiting...'
quit()
else:
print 'Good job, you\'re root.'
print "Trying to activate the default wlan0 network..."
if not commandExists("ifconfig"):
print "You will need the command 'ifconfig' to continue."
quit()
status = os.popen("ifconfig wlan0 up").read()
if 'No such device' in status:
print "It seems your wireless device is not named wlan0, so you're going to need to enter it manually."
winame = raw_input('Wireless Device Name: ')
else:
winame = "wlan0"
print "Wireless device enabled!"
print "Checking for available wireless networks..."
stream = os.popen("iwlist " + winame + " scan")
print "Available Networks:"
networksfound = 0
for line in stream:
if "ESSID" in line:
networksfound += 1
print " " + line.split('ESSID:"', 1)[1].split('"', 1)[0]
if networksfound == 0:
print "Looks like we didn't find any networks in your area. Exiting..."
quit()
network = raw_input("Please enter your network: ")
tpass = raw_input("Please enter the network's pass (Blank for none): ")
if tpass == '':
os.popen("iwconfig " + winame + " essid " + network)
else:
connectstatus = os.popen("iwconfig " + winame + " essid " + network + " key s:" + tpass)
print "Connecting..."
if not commandExists("dhclient"):
print "Looks like there isn't a dhclient program on this computer. Trying dhcpd (Used with Arch)"
con2 = os.popen("dhcpcd " + winame).read()
print con2
if not commandExists("dhcpcd"):
print "Well, I'm out of options. Try installing dhcpd or dhclient."
quit()
else:
os.popen("dhclient " + winame)
ontest = os.popen("ping -c 1 google.com").read()
if ontest == '':
print "Connection failed. (Bad pass?)"
quit()
print "Connected successfully!"
quit()
@abdullahnazir123
Copy link

Hi there,
Thanks for sharing it. It can help us lot of people but i'm facing a Little bit problem whenever i try to connect i get the following error : "Error for wireless request "Set Encode" (8B2A) :
SET failed on device wlan0 ; Invalid argument."
Can you tell how it can be resolved?

@ouail-amar-ramzi
Copy link

me too i get this error

@fany0r
Copy link

fany0r commented Jul 10, 2019

我也得到这个错误

You can try to delete [s:] after the 53rd line key.

@juro1012cqq
Copy link

Hi pro,
This is very interesting code.
If, I want to connecting wifi with python code into windows operating system, Can you help me?

@abdulwahabmalak
Copy link

I have tried deleting s: in front of key at 53rd line. But it is yet giving "/bin/sh: 1: iwconfigwlan0essidvivo-s1keyallahakbar: not found" error. Moreover, it also gives "Error for wireless request "Set ESSID" (8B1A) :". Kindly reply how to solve it

@danagarcia
Copy link

@abdulwahabmalak, @nguyentranphucbao - The issue is likely that you aren't trying to connect to a WEP network you're attempting to connect to a WPA network. This script using iwlist and iwconfig which only support WEP. You can modify this script to work by doing something like this on line 53:

    os.popen("wpa_passphrase '" + network + "' '" + tpass + "' > wpa.conf")
    connectstatus = os.popen("wpa_supplicant -iwlan0 -c/root/wpa.conf -B")

This will require that you install WPA supplicant using:
sudo apt install wpasupplicant

You should be able to leave line 51 seeing as this example is if there isn't a passphrase (i.e. open network)

Hope this helps!
Cheers,
Dana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment