Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import time
- from subprocess import Popen, PIPE, call
- def scan_wlan(dev, ap):
- proc = Popen(['/sbin/iwlist', dev, 'scanning'], stdout=PIPE, stderr=PIPE)
- out, _ = proc.communicate()
- out = out.decode()
- for line in out.splitlines():
- if "ESSID:" in line:
- _, essid = line.split(':')
- essid = essid.replace('"', '')
- if essid == ap:
- return True
- else:
- return False
- def ping(ip):
- cmd = ['ping', '-c1', '-W1', ip]
- returncode = Popen(cmd, stdout=PIPE, stderr=PIPE).wait()
- if returncode == 0:
- return True
- else:
- return False
- def reconnect(dev):
- call(['/sbin/ifdown', dev], stdout=PIPE, stderr=PIPE)
- time.sleep(1)
- call(['/sbin/ifup', dev], stdout=PIPE, stderr=PIPE)
- def main(dev, ap, ip):
- while True:
- if not ping(ip):
- if scan_wlan(dev, ap):
- reconnect(dev)
- time.sleep(5)
- if __name__ == '__main__':
- main('wlan0', 'Mikrowelle 3', '192.168.43.1')
Add Comment
Please, Sign In to add comment