aboutsummaryrefslogtreecommitdiff
path: root/autoDHCP.py
blob: cdada36795b78aa791ef5394a8f0643d80ca5f46 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/env python

import sys
import ipaddress
import argparse
import os
from os.path import exists
import subprocess
import tempfile


def guess_wifi() -> list[str]:
    return [interface for interface in os.listdir('/sys/class/net/') if
            exists(f'/sys/class/net/{interface}/wireless')]


def guess_nowifi() -> list[str]:
    return [interface for interface in os.listdir('/sys/class/net/') if
            not exists(f'/sys/class/net/{interface}/wireless')]


def guess_interface() -> list[str]:
    return [interface for interface in os.listdir('/sys/class/net/')]


def guess_internet() -> list[str]:
    com = subprocess.Popen(['ip', 'route', 'show'], stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT)
    stdout, stderr = com.communicate()
    if stderr:
        return []
    print(str(stdout).split('\n'))

    internet_i = [line for line in
                  [stripedLine(line) for line in str(stdout).split('\n') if
                   'default' in line]
                  if line != '']

    return list(set(internet_i))


def stripedLine(line) -> str:
    words = line.split(' ')
    if 'dev' in words and words.index('dev'):
        return words[words.index('dev') + 1]
    else:
        return ''


def menu(listEntry, strInput, refresh=None) -> int:
    if listEntry == []:
        print(f"Error menu: Empty list\n({strInput})", file=sys.stderr)
        sys.exit(1)

    c = -1
    if len(listEntry) == 1:
        c = 0
    while not (0 < c <= len(listEntry)):
        for i, ii in enumerate(listEntry):
            print(f" {i + 1}) {ii}")
        r = input(f'{strInput} '
                  f'(q to quit{ ", r to refresh" if refresh else ""}) :')
        if refresh and r == 'r':
            listEntry = refresh()
        elif r.lower() in ['q', 'quit']:
            exit(0)
        else:
            try:
                c = int(r)
            except:
                pass
    return c - 1


ip = '10.5.6.11'
netRange = '24'
ssid = '🦖'
password = 'chocoball'

hostapd_conf = '''
interface={interface}
ssid={ssid}
wpa_passphrase={passphrase}
wpa=1

channel=11
hw_mode=g

# ieee80211d=1
# country_code=FR
ieee80211n=1
# ieee80211ac=1

logger_syslog=-1
logger_syslog_level=2
logger_stdout=-1
logger_stdout_level=2

ctrl_interface=/var/run/hostapd
ctrl_interface_group=0

beacon_int=100
dtim_period=2
max_num_sta=255
rts_threshold=2347
fragm_threshold=2346
macaddr_acl=0
auth_algs=3
ignore_broadcast_ssid=0

wmm_enabled=1
wmm_ac_bk_cwmin=4
wmm_ac_bk_cwmax=10
wmm_ac_bk_aifs=7
wmm_ac_bk_txop_limit=0
wmm_ac_bk_acm=0
wmm_ac_be_aifs=3
wmm_ac_be_cwmin=4
wmm_ac_be_cwmax=10
wmm_ac_be_txop_limit=0
wmm_ac_be_acm=0
wmm_ac_vi_aifs=2
wmm_ac_vi_cwmin=3
wmm_ac_vi_cwmax=4
wmm_ac_vi_txop_limit=94
wmm_ac_vi_acm=0
wmm_ac_vo_aifs=2
wmm_ac_vo_cwmin=2
wmm_ac_vo_cwmax=3
wmm_ac_vo_txop_limit=47
wmm_ac_vo_acm=0

wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP
wpa=2

vht_oper_chwidth=3
eap_message=hello
eapol_key_index_workaround=0
eap_server=0
'''

dnsmasq_conf = '''
interface={interface}
listen-address={ip}
dhcp-range={dhcp_range}
'''

# dhcp_range=$(echo ${ip_start}{$ip_start_tmp\,,254}),12h

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Automaticaly set up a DHCP server')
    parser.add_argument('--ip', dest='ip', default=ip, help='IP on the DHCP network')
    parser.add_argument('-r', '--range', dest='range', default=netRange,
                        help='number of bit of the IP that are part of the '
                        'network address')
    parser.add_argument('-s', '--ssid', dest='ssid', default=ssid, help='Wifi\'s "name"')
    parser.add_argument('--password', dest='passwd', default=password,
                        help='the password of the Wifi network if any')
    parser.add_argument('--no-password', dest='nopass', action='store_true',
                        help='disable password')
    parser.add_argument('-t', '--tethering', dest='teth', action='store_true',
                        help='configure tethering on the DHCP network')
    parser.add_argument('-w', '--wifi', dest='wifi', action='store_true',
                        help='DHCP on a wifi interface')
    parser.add_argument('--no-wifi', dest='nowifi', action='store_true',
                        help='DHCP not on a wifi interface')
    parser.add_argument('-in', '--inet-iface', dest='internet_interface',
                        help='the internet interface to use when tethering')
    parser.add_argument('-i', '--iface', dest='interface',
                        help='DHCP network\'s interface')

    arg = parser.parse_args()

    if arg.wifi and arg.nowifi:
        print('Wifi or not Wifi, that is the question', file=sys.stderr)
        exit(1)
    ip = ipaddress.ip_address(arg.ip)

    inetInt = arg.internet_interface
    netInt = arg.interface

    if arg.teth:
        if not arg.internet_interface:
            inet_int = guess_internet()
            i = menu(inet_int, "Select the internet network interface", refresh=guess_internet)
            inetInt = inet_int[i]

    interfaces_list = []
    refresh = None

    if arg.wifi:
        refresh = guess_wifi
    elif arg.nowifi:
        refresh = guess_nowifi
    else:
        refresh = guess_interface

    interfaces_list = refresh()

    if not netInt:
        c = menu(interfaces_list, "Select the DHCP network's interface", refresh)
        netInt = interfaces_list[c]
    else:
        if netInt not in interfaces_list:
            if arg.wifi:
                print(f"{netInt} not a valid wifi interface", file=sys.stderr)
                exit(2)
            elif arg.nowifi:
                if netInt in guess_interface():
                    print(f'Warning: {netInt} is a wifi interface')
                else:
                    print(f"{netInt} not a valid interface", file=sys.stderr)
                    exit(2)
            else:
                print(f"{netInt} not a valid interface", file=sys.stderr)
                exit(2)

    os.system(f'ip link set {netInt} up')
    os.system(f'ip addr add {ip}/{netRange} dev {netInt}')

    fd, dnsConfPath = tempfile.mkstemp(dir="/tmp/")
    f_, pidPath = tempfile.mkstemp(dir="/tmp/")
    os.close(f_)

    with os.fdopen(fd, 'w') as tmp:
        network = ipaddress.ip_network(f'{ip}/{netRange}', strict=False)
        nextIp = ipaddress.ip_address(int(ip) + 1)
        lastIp = ipaddress.ip_address(int(network.broadcast_address) - 1)

        if nextIp >= lastIp:
            print('Error: Empty DHCP range', file=sys.stderr)
            exit(3)

        dhcp_range = f'{nextIp}, {lastIp}, 12h'
        tmp.write(dnsmasq_conf.format(interface=netInt, ip=ip,
                                      dhcp_range=dhcp_range))

    print("🔧 - Setting up dns server", f"Range : {dhcp_range}",
            f"Server IP: {ip}", sep='\n')
    ret = os.system(f'dnsmasq --conf-file={dnsConfPath} --pid-file={pidPath}')
    os.remove(dnsConfPath)
    pid = None
    with open(pidPath, "r") as f:
        pid = f.read().strip()
        os.remove(pidPath)

    if ret:
        print('Error: dnsmasq failed to launch', file=sys.stderr)
        exit(1)
    else:
        print(f'✅ dnsmasq launch with pid ({pid})')

    if arg.teth:
        os.system('sysctl net.ipv4.ip_forward=1')
        os.system('sysctl net.ipv6.conf.all.forwarding=1')
        os.system(f'iptables -t nat -A POSTROUTING -o {inetInt} -j MASQUERADE')
        os.system('iptables -A FORWARD -m conntrack --ctstate'
                  ' RELATED,ESTABLISHED -j ACCEPT')
        os.system(f'iptables -A FORWARD -i {netInt} -o {inetInt} -j ACCEPT')
        print("Tethering set")

    if arg.wifi:
        fd2, hostAPConfPath = tempfile.mkstemp(dir="/tmp/")

        with os.fdopen(fd2, 'w') as tmp:
            tmp.write(hostapd_conf.format(ssid=arg.ssid,
                                          interface=netInt,
                                          passphrase=arg.passwd))

        print("⚙️  - Setting up hostapd", f"SSID: {ssid}",
                f"Password: {arg.passwd}", f"Interface: {netInt}",
                sep='\n')
        ret = os.system(f'hostapd {hostAPConfPath}')
        if ret:
            print('Error: hostapd failed to launch', file=sys.stderr)
            if pid is not None:
                os.system(f'kill {pid}')
            exit(1)

        if pid is not None:
            print(" 🤖 - Terminating DHCP server")
            ret = os.system(f'kill {pid}')
            print("✅ Hasta la vista, baby")

    print("Thant you for using autoDHCP ! 💋 ")