SHARE
TWEET

Untitled

a guest Dec 13th, 2016 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/python
  2. #
  3. # Connects to a Wifi network
  4. #
  5.  
  6. import os
  7.  
  8. def commandExists(command):
  9.     def canExecute(file):
  10.         return os.path.isfile(file) and os.access(file, os.X_OK)
  11.     for path in os.environ["PATH"].split(os.pathsep):
  12.         file = os.path.join(path, command)
  13.         if canExecute(file):
  14.             return True
  15.     return False
  16.  
  17. print "Checking for root access..."
  18. user = os.popen("whoami").read()
  19. if 'root' not in user:
  20.     print 'You need to be the root user to run this program and you are running as '+user+'  Try sudo python <ScriptName>.py'
  21.     print 'Exiting...'
  22.     quit()
  23. else:
  24.     print 'Good job, you\'re root.'
  25.  
  26. print "Trying to activate the default wlan0 network..."
  27. if not commandExists("ifconfig"):
  28.     print "You will need the command 'ifconfig' to continue."
  29.     quit()
  30. status = os.popen("ifconfig wlan0 up").read()
  31. if 'No such device' in status:
  32.     print "It seems your wireless device is not named wlan0, so you're going to need to enter it manually."
  33.     winame = raw_input('Wireless Device Name: ')
  34. else:
  35.     winame = "wlan0"
  36. print "Wireless device enabled!"
  37. print "Checking for available wireless networks..."
  38. stream = os.popen("iwlist " + winame + " scan")
  39. print "Available Networks:"
  40. networksfound = 0
  41. for line in stream:
  42.     if "ESSID" in line:
  43.         networksfound += 1
  44.         print " " + line.split('ESSID:"', 1)[1].split('"', 1)[0]
  45. if networksfound == 0:
  46.     print "Looks like we didn't find any networks in your area. Exiting..."
  47.     quit()
  48. network = raw_input("Please enter your network: ")
  49. tpass = raw_input("Please enter the network's pass (Blank for none): ")
  50. if tpass == '':
  51.     os.popen("iwconfig " + winame + " essid " + network)
  52. else:
  53.     connectstatus = os.popen("iwconfig " + winame + " essid " + network + " key s:" + tpass)
  54. print "Connecting..."
  55. if not commandExists("dhclient"):
  56.     print "Looks like there isn't a dhclient program on this computer. Trying dhcpd (Used with Arch)"
  57.     con2 = os.popen("dhcpcd " + winame).read()
  58.     print con2
  59.     if not commandExists("dhcpcd"):
  60.         print "Well, I'm out of options. Try installing dhcpd or dhclient."
  61.         quit()    
  62. else:
  63.     os.popen("dhclient " + winame)
  64. ontest = os.popen("ping -c 1 google.com").read()
  65. if ontest == '':
  66.     print "Connection failed. (Bad pass?)"
  67.     quit()
  68. print "Connected successfully!"
  69. quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top