Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #!/bin/python
  2.  
  3. #
  4.  
  5. # Connects to a Wifi network
  6.  
  7. #
  8.  
  9.  
  10.  
  11. import os
  12.  
  13.  
  14.  
  15. def commandExists(command):
  16.  
  17. def canExecute(file):
  18.  
  19. return os.path.isfile(file) and os.access(file, os.X_OK)
  20.  
  21. for path in os.environ["PATH"].split(os.pathsep):
  22.  
  23. file = os.path.join(path, command)
  24.  
  25. if canExecute(file):
  26.  
  27. return True
  28.  
  29. return False
  30.  
  31.  
  32.  
  33. print ("Checking for root access...")
  34.  
  35. user = os.popen("whoami").read()
  36.  
  37. if 'root' not in user:
  38.  
  39. print ('You need to be the root user to run this program and you are running as '+user+' Try sudo python <ScriptName>.py')
  40.  
  41. print ('Exiting...')
  42.  
  43. quit()
  44.  
  45. else:
  46.  
  47. print( 'Good job, you\'re root.')
  48.  
  49.  
  50.  
  51. print ("Trying to activate the default wlan0 network...")
  52.  
  53. if not commandExists("ifconfig"):
  54.  
  55. print ("You will need the command 'ifconfig' to continue.")
  56.  
  57. quit()
  58.  
  59. status = os.popen("ifconfig wlan0 up").read()
  60.  
  61. if 'No such device' in status:
  62.  
  63. print( "It seems your wireless device is not named wlan0, so you're going to need to enter it manually.")
  64.  
  65. winame = raw_input('Wireless Device Name: ')
  66.  
  67. else:
  68.  
  69. winame = "wlan0"
  70.  
  71. print ("Wireless device enabled!")
  72.  
  73. print ("Checking for available wireless networks...")
  74.  
  75. stream = os.popen("iwlist " + winame + " scan")
  76.  
  77. print ("Available Networks:")
  78.  
  79. networksfound = 0
  80.  
  81. for line in stream:
  82.  
  83. if "ESSID" in line:
  84.  
  85. networksfound += 1
  86.  
  87. print (" " + line.split('ESSID:"', 1)[1].split('"', 1)[0])
  88.  
  89. if networksfound == 0:
  90.  
  91. print( "Looks like we didn't find any networks in your area. )Exiting..."
  92. else:
  93.  
  94. quit()
  95.  
  96. network = raw_input("Please enter your network: ")
  97.  
  98. tpass = raw_input("Please enter the network's pass (Blank for none): ")
  99.  
  100. if tpass == '':
  101.  
  102. os.popen("iwconfig " + winame + " essid " + network)
  103.  
  104. else:
  105.  
  106. connectstatus = os.popen("iwconfig " + winame + " essid " + network + " key s:" + tpass)
  107.  
  108. print ("Connecting...")
  109.  
  110. if not commandExists("dhclient"):
  111.  
  112. print ("Looks like there isn't a dhclient program on this computer. Trying dhcpd (Used with Arch)")
  113.  
  114. con2 = os.popen("dhcpcd " + winame).read()
  115.  
  116. print (con2)
  117.  
  118. if not commandExists("dhcpcd"):
  119.  
  120. print ("Well, I'm out of options. Try installing dhcpd or dhclient.")
  121.  
  122. quit()
  123.  
  124. else:
  125.  
  126. os.popen("dhclient " + winame)
  127.  
  128. ontest = os.popen("ping -c 1 google.com").read()
  129.  
  130. if ontest == '':
  131.  
  132. print ("Connection failed. (Bad pass?)")
  133.  
  134. quit()
  135.  
  136. print ("Connected successfully!")
  137.  
  138. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement