aboutsummaryrefslogtreecommitdiff
path: root/autoDHCP.sh
blob: 484dd33052c2fa5ba76cc183d2536deabb618e1d (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
#!/bin/env bash

interface="";
server="dhcpd"
ip="10.5.6.11"
range="24"
interface_internet=""

# For Wifi teth
ssid=🦄🌈
passphrase=chocolat


# Todo : List interface ✓
#        Select first ✓
#        Select dhcpd or dnsmasq ✓
#        Select ip range ✓
#        Select ip ✓
#        Help ✓
#        Install from Makefile ✓
#        Add support for tethapp
#        dnsmasq config overwrite

# Default interface is the first non-wireless interfaces (sorted alpha-num)

function guess_wifi {
    for i in `ls /sys/class/net/`; do
        if [ -d "/sys/class/net/$i/wireless" ] ; then
            _interface="$i"
        fi
    done
    echo "${_interface}"
}
function guess_nowifi {
    for i in `ls /sys/class/net/`; do
        if [ ! -d "/sys/class/net/$i/wireless" ] ; then 
            if [ "$interface" ] ; then 
                if [[ "$i" < "$înterface" ]] ; then
                    interface="$i"
                fi
            else
                interface="$i"
            fi
        fi
    done
}
function guess_internet {
    echo $(ip route show | grep 'default' |  \
                    sed 's/.*dev/dev/' | sed 's/scope//' | \
                    sed 's/src \([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}//g' | \
                    sed 's/link//' |
                    tr -s ' ' |
                    cut -d' ' -f 2)
}

if [ "$1" == "dns" ] ; then
    server="dnsmasq"
    shift
elif [ "$1" == "teth" ] ; then
  interface_internet=$(guess_internet)
  shift
elif [ "$1" == "teth_dns" ] || [ "$1" == "teth_dnsmasq" ] ; then
  interface_internet=$(guess_internet)
  server="dnsmasq"
  shift
elif [ "$1" == "teth_wifi_dnsmasq" ] ; then
  interface_internet=$(guess_internet)
  interface=$(guess_wifi)
  server="dnsmasq"
  hotspot="yes"
  shift
elif [ "$1" == "quit" ] || [ "$1" == "close" ] ; then
  sudo pkill dnsmasq
  exit
elif [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
    echo 'Usage : autoDHCP [interface] [ip/range] [dns]'
    echo ''
    echo 'You also put the server type at the start of the command'
    echo 'Ex :'
    echo '> autoDHCP wlp3s0 10.5.5.11/24'
    echo '> autoDHCP eth0 192.168.0.1 dns'
    echo '> autoDHCP dns eth0 192.168.0.1/30'
    exit
fi


if [ "$1" ] ; then
    interface="$1"
    echo "Interface set to ${interface}"
fi

if [ "$2" ] ; then
    if [[ "$2" =~ .*/.* ]] ; then
        ip=$(echo $2 | cut -d'/' -f1)
        range="`echo $2 | cut -d'/' -f2 `"
        echo "Range set to ${range}"
    else
        ip="$2"
    fi
    echo "Ip set to ${ip}"
fi

if [ "$3" == "dns" ] ; then
    server="dnsmasq"
fi

IFS='.' read -r -a ipList <<< "$ip"
mask=$(( 4294967295 >> 32-${range} << 32-${range} ))
ipRaw=$(( ipList[0]*2**24 + ipList[1]*2**16 + ipList[2]*2**8 + ipList[3] ))
ipNetRaw=$(( ipRaw & mask ))

ipNet="$(( ipNetRaw >> 24 )).$(( ipNetRaw >> 16 & 255 )).$(( ipNetRaw >> 8 & 255)).$(( ipNetRaw & 255))"

if [ -z "$interface" ] ; then
  guess_nowifi
fi


#########################################################################
######################## ACTION #########################################
#########################################################################


sudo ip l set "$interface" up
sudo ip a r "${ip}/${range}" dev "$interface"
#sudo ip r a "${ip}/${range}" dev "$interface" src "${ip}"

if [ "$server" == "dhcpd" ] ; then
    sudo /usr/bin/dhcpd -4 -q -pf /run/dhcpd4.pid "$interface"
elif [ "$server" == "dnsmasq" ] ; then

    echo 'Configure dnsmasq'
    pidFile=$(mktemp)

    ip_start=$(echo "${ip}" | sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)[0-9]\{1,3\}/\1/')
    ip_start_tmp=$(echo "${ip}" | sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)//')
    ip_start_tmp=$((ip_start_tmp+1))

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

    cat <<< $(
    echo "
        interface=${interface}
        listen-address=${ip}
        dhcp-range=${dhcp_range}"
    ) > /tmp/dnsmasq.conf
    if sudo dnsmasq --conf-file=/tmp/dnsmasq.conf --pid-file="${pidFile}"; then
      echo 'Ok dnsmasq prêt'
      echo "PID : ${pidFile}"
    else
      echo 'Erreur : Impossible de démarrer dnsmasq'
      echo 'Abandon'
      exit 1
    fi
else
    echo "Server name ${server} unknow"
fi


if [ -n "$interface_internet" ] ; then
  echo "Routage des connections internet (${interface_internet} => ${interface})"
  sudo sysctl net.ipv4.ip_forward=1
  sudo iptables -t nat -A POSTROUTING -o "${interface_internet}" -j MASQUERADE
  sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
  sudo iptables -A FORWARD -i "${interface}" -o "${interface_internet}" -j ACCEPT
fi

if [ "${hotspot}" == "yes" ] ; then
  echo "Configuration de hostapd"

  cat <<< $(
  echo "
interface=${interface}
ssid=${ssid}

wpa=1
wpa_passphrase=${passphrase}

channel=11
hw_mode=g

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"
  ) > /tmp/hostapd.conf

  echo "Lancement du hostpot Wifi"
  if sudo hostapd /tmp/hostapd.conf ; then
      echo 'Yeah, i wish you enjoyed it'
  else
      echo 'Error: there is a problem with hostapd'
  fi
else
  echo 'Should be ready ;)'
  sleep 100d
fi

echo 'Clean des iptables'
sudo iptables --flush
sudo iptables -t nat --flush

echo 'Clean config'

rm /tmp/dnsmasq_hotspot.conf /tmp/hostapd.conf /tmp/dnsmasq.conf 2> /dev/null

if [ "${server}" == "dnsmasq" ] ; then
  echo 'Switch off dnsmasq'
  sudo kill $(cat $pidFile)
fi

echo 'Bye 💋'
echo 'Have a nice day <3'