Advertisement
DeaD_EyE

wifi-switch

Feb 23rd, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import subprocess
  4. import shlex
  5. import time
  6. from RPi import GPIO
  7.  
  8. CLIENT_MODE = 19
  9. AP_MODE = 26
  10.  
  11. def run(program):
  12.     proc = subprocess.Popen(shlex.split(program), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  13.     return proc.communicate()[0].decode()
  14.    
  15. def is_connected(dev):
  16.     data = run('/bin/ip link show').splitlines()
  17.     for line in data:
  18.         if '{}:'.format(dev) in line:
  19.             return True
  20.     else:
  21.         return False
  22.  
  23. def remove_ip(dev):
  24.     run('/bin/ip addr flush dev {}'.format(dev))  
  25.  
  26. def set_ip(dev, ip):
  27.     run('/bin/ip add add dev {dev} {ip}/24'.format(dev=dev, ip=ip))
  28.  
  29. def kill_wpa():
  30.     run('/usr/bin/pkill -9 wpa_supplicant')
  31.  
  32. def start_dhclient(dev):
  33.     run('/sbin/dhclient -nw {}'.format(dev))
  34.  
  35. def kill_dhclient():
  36.     run('pkill -9 dhclient')
  37.  
  38. def kill_hostapd():
  39.     run('/bin/systemctl stop hostapd')
  40.  
  41. def dnsmasq(action):
  42.     run('/bin/systemctl {} dnsmasq'.format(action))
  43.  
  44. def kill(dev):
  45.     dnsmasq('stop')
  46.     kill_dhclient()
  47.     kill_wpa()
  48.     kill_hostapd()
  49.     remove_ip(dev)
  50.  
  51.  
  52. def client(dev, conf):
  53.     if is_connected(dev):
  54.         kill(dev)
  55.         time.sleep(1)
  56.         cmd = '/sbin/wpa_supplicant -Dnl80211 -i{dev} -c {conf} -B'.format(dev=dev, conf=conf)
  57.         run(cmd)
  58.         start_dhclient(dev)
  59.  
  60. def ap(dev):
  61.     if is_connected(dev):
  62.         kill(dev)
  63.         #cmd = '/usr/sbin/hostapd /etc/hostapd/hostapd.conf -B'
  64.         cmd = '/bin/systemctl start hostapd'
  65.         run(cmd)
  66.         set_ip(dev, '192.168.0.254')
  67.         dnsmasq('start')
  68.  
  69. def status(ap_mode, client_mode):
  70.     if ap_mode:
  71.         print("Activating Access Point Mode")
  72.         ap('wlan0')
  73.     elif client_mode:
  74.         print("Activating Client Mode")
  75.         client('wlan0', '/home/server/China2.conf')
  76.     else:
  77.         print('Switch is not connected')
  78.         print('Starting Access Point Mode')
  79.         ap('wlan0')
  80.  
  81. class Switch:
  82.     def __init__(self, callback):
  83.         GPIO.setmode(GPIO.BCM)
  84.         GPIO.setup(AP_MODE, GPIO.IN, GPIO.PUD_UP)
  85.         GPIO.setup(CLIENT_MODE, GPIO.IN, GPIO.PUD_UP)
  86.         self.ap_mode = GPIO.input(AP_MODE)
  87.         self.client_mode = GPIO.input(CLIENT_MODE)
  88.         self.callback = callback
  89.         self.callback(self.ap_mode, self.client_mode)
  90.  
  91.     def update(self):
  92.         ap_mode = GPIO.input(AP_MODE)
  93.         client_mode = GPIO.input(CLIENT_MODE)
  94.         #print('AP: {}\tCLIENT: {}'.format(ap_mode, client_mode))
  95.         if ap_mode != client_mode:
  96.             if ap_mode != self.ap_mode or client_mode != self.client_mode:
  97.                 self.ap_mode = ap_mode
  98.                 self.client_mode = client_mode
  99.                 self.callback(ap_mode, client_mode)
  100.                
  101.  
  102.     def run(self):
  103.         while True:
  104.             self.update()
  105.             time.sleep(2)
  106.  
  107.  
  108. if __name__ == '__main__':
  109.     switch = Switch(status)
  110.     switch.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement