DeaD_EyE

WLAN-Autoconnect-Hack

Feb 2nd, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import time
  4. from subprocess import Popen, PIPE, call
  5.  
  6. def scan_wlan(dev, ap):
  7.     proc = Popen(['/sbin/iwlist', dev, 'scanning'], stdout=PIPE, stderr=PIPE)
  8.     out, _ = proc.communicate()
  9.     out = out.decode()
  10.     for line in out.splitlines():
  11.         if "ESSID:" in line:
  12.             _, essid = line.split(':')
  13.             essid = essid.replace('"', '')
  14.             if essid == ap:
  15.                 return True
  16.     else:
  17.         return False
  18.  
  19. def ping(ip):
  20.     cmd = ['ping', '-c1', '-W1', ip]
  21.     returncode = Popen(cmd, stdout=PIPE, stderr=PIPE).wait()
  22.     if returncode == 0:
  23.         return True
  24.     else:
  25.         return False
  26.  
  27. def reconnect(dev):
  28.     call(['/sbin/ifdown', dev], stdout=PIPE, stderr=PIPE)
  29.     time.sleep(1)
  30.     call(['/sbin/ifup', dev], stdout=PIPE, stderr=PIPE)
  31.  
  32.  
  33. def main(dev, ap, ip):
  34.     while True:
  35.         if not ping(ip):
  36.             if scan_wlan(dev, ap):
  37.                 reconnect(dev)
  38.         time.sleep(5)
  39.  
  40. if __name__ == '__main__':
  41.     main('wlan0', 'Mikrowelle 3', '192.168.43.1')
Add Comment
Please, Sign In to add comment