-
Notifications
You must be signed in to change notification settings - Fork 0
/
openair-ap.py
84 lines (70 loc) · 2.49 KB
/
openair-ap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
import sys
from subprocess import Popen, PIPE
_VIRTUAL_IFACE_ = "ap0"
_IFACE_ = "wlan0"
_DNSMASQ_CONF_ = "/etc/openair-dnsmasq.conf"
_HOSTAPD_CONF_ = "/etc/openair-hostapd.conf"
class BashManager:
_runner = None
_pipe = None
def __init__(self):
self._runner = Popen
self._pipe = PIPE
def run(self, command):
process = self._runner(
command,
shell=True,
stdout=self._pipe,
stderr=self._pipe
)
return process.communicate()
def _exists_iface(bash_manager, interface):
bash_command = "ip link | grep %s" % (interface)
(result, error) = bash_manager.run(bash_command)
return result and len(result) > 0
def _create_virtual_iface(bash_manager, iface, virtual_iface):
bash_command = "brctl addbr %s" % (virtual_iface)
bash_manager.run(bash_command)
bash_command = "iw dev %s set 4addr on" % (iface)
bash_manager.run(bash_command)
bash_command = "brctl addif %s %s" % (virtual_iface, iface)
bash_manager.run(bash_command)
def _enable_iface(bash_manager, iface):
bash_command = "ip link set %s up" % (iface)
bash_manager.run(bash_command)
def _run_ap_daemon(bash_manager, dns_conf, apd_conf):
bash_command = "dnsmasq -C %s" % dns_conf
bash_manager.run(bash_command)
bash_command = "hostapd %s" % apd_conf
bash_manager.run(bash_command)
def _stop_ap_daemon(bash_manager, virtual_iface):
bash_command = "ip link set %s down" % (virtual_iface)
bash_manager.run(bash_command)
if _exists_iface(bash_manager, virtual_iface):
bash_command = "brctl delbr %s" % (virtual_iface)
bash_manager.run(bash_command)
bash_command = "killall dnsmasq"
bash_manager.run(bash_command)
bash_command = "killall hostapd"
bash_manager.run(bash_command)
def start_ap(bash_manager):
if not _exists_iface(bash_manager, _VIRTUAL_IFACE_):
_create_virtual_iface(bash_manager, _IFACE_, _VIRTUAL_IFACE_)
_enable_iface(bash_manager, _VIRTUAL_IFACE_)
_run_ap_daemon(bash_manager, _DNSMASQ_CONF_, _HOSTAPD_CONF_)
def stop_ap(bash_manager):
_stop_ap_daemon(bash_manager, _VIRTUAL_IFACE_)
def print_usage():
print("Usage: ")
print(" openair-ap start|stop")
if __name__ == "__main__":
bash_manager = BashManager()
if len(sys.argv) < 2:
print_usage()
elif sys.argv[1] == "start":
start_ap(bash_manager)
elif sys.argv[1] == "stop":
stop_ap(bash_manager)
else:
print_usage()