blob: 7b39de3305e20b90b021ca63b17fc969bc376c4a (
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
|
#!/bin/env bash
interface="";
server="dhcpd"
ip="10.5.5.11"
range="24"
interface_internet=""
# 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_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" ] ; then
interface_internet=$(guess_internet)
server="dnsmasq"
shift
fi
if [ "$1" == "help" -o "$1" == "-h" -o "$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'
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
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'
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}
bind-interfaces
dhcp-range=${dhcp_range}"
) > /tmp/dnsmasq.conf
sudo dnsmasq --conf-file=/tmp/dnsmasq.conf
else
echo "Server name ${server} unknow"
fi
if [ -n "$interface_internet" ] ; then
echo "Routage des connections internet (${interface_internet} => ${interface})"
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
echo 'Should be ready ;)'
|