|
| 1 | +import socket |
| 2 | +import subprocess |
| 3 | +import shutil |
| 4 | +import sys |
| 5 | + |
| 6 | +class WiFi(): |
| 7 | + |
| 8 | + adapters = ["RT5370", "RTL8188CUS"] |
| 9 | + hostapds = {"RT5370": "hostapd.RT5370", "RTL8188CUS": "hostapd.RTL8188"} |
| 10 | + web_url = "http://coderbotsrv.appspot.com/register_ip" |
| 11 | + wifi_client_conf_file = "/etc/wpa_supplicant/wpa_supplicant.conf" |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def get_adapter_type(cls): |
| 15 | + lsusb_out = subprocess.check_output("lsusb") |
| 16 | + for a in cls.adapters: |
| 17 | + if a in lsusb_out: |
| 18 | + return a |
| 19 | + return None |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def start_hostapd(cls): |
| 23 | + adapter = cls.get_adapter_type() |
| 24 | + hostapd_type = cls.hostapds.get(adapter) |
| 25 | + print hostapd_type |
| 26 | + try: |
| 27 | + #out = subprocess.check_output(["start-stop-daemon", "--start", "--oknodo", "--quiet", "--exec", "/usr/sbin/" + hostapd_type, "--", "/etc/hostapd/" + hostapd_type]) |
| 28 | + out = subprocess.check_output(["/usr/sbin/" + hostapd_type, "/etc/hostapd/" + hostapd_type]) |
| 29 | + |
| 30 | + except subprocess.CalledProcessError as e: |
| 31 | + print e.output |
| 32 | + raise |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def stop_hostapd(cls): |
| 36 | + try: |
| 37 | + out = subprocess.check_output(["pkill", "-9", "hostapd"]) |
| 38 | + print out |
| 39 | + except: |
| 40 | + pass |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def get_ipaddr(cls): |
| 44 | + ipaddr = socket.gethostbyname(socket.gethostname()) |
| 45 | + return ipaddr |
| 46 | + |
| 47 | + @classmethod |
| 48 | + def register_ipaddr(cls, ipaddr, botname): |
| 49 | + urllib.request.urlopen(cls.web_url + "?name=" + botname + "&ipaddr=" + ipaddr) |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def get_wlans(cls): |
| 53 | + out = subprocess.check_output(["iwlist", "wlan0", "scan"]) |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def set_client_params(cls, wssid, wpsk): |
| 57 | + f = open (cls.wifi_client_conf_file, "w+") |
| 58 | + f.write("""ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev |
| 59 | +update_config=1 |
| 60 | +network={\n""") |
| 61 | + f.write(" ssid=\""+wssid+"\"\n") |
| 62 | + f.write(" psk=\""+wpsk+"\"\n") |
| 63 | + f.write("}") |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def start_as_client(cls): |
| 67 | + cls.stop_hostapd() |
| 68 | + out = subprocess.check_output(["ifdown", "wlan0"]) |
| 69 | + shutil.copy("/etc/network/interfaces_cli", "/etc/network/interfaces") |
| 70 | + out = subprocess.check_output(["ifup", "wlan0"]) |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def start_as_ap(cls): |
| 74 | + out = subprocess.check_output(["ifdown", "wlan0"]) |
| 75 | + shutil.copy("/etc/network/interfaces_ap", "/etc/network/interfaces") |
| 76 | + out = subprocess.check_output(["ifup", "wlan0"]) |
| 77 | + cls.start_hostapd() |
| 78 | + |
| 79 | +def main(): |
| 80 | + w = WiFi() |
| 81 | + |
| 82 | + |
| 83 | +main() |
| 84 | + |
0 commit comments