-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnet-setup
222 lines (200 loc) · 8.47 KB
/
net-setup
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
#!/bin/bash
# net-setup
##################################################################
# *** P25Link v2.30.-2 ***
# Released: November 09, 2020. Created September 04, 2019.
# Created by: Juan Carlos Pérez De Castro (Wodie) KM4NNO, XE1F.
# www.wodielite.com
# wodielite at mac.com
##################################################################
# See LICENSE file for copyright and license details
# Based on ASL modification by mod4whip N8THN
# Network interface setup script
#
#
DHCPCDPATH=/etc/dhcpcd.conf
INTPATH=/etc/network/interfaces
IFUPDPATH=/etc/network/if-up.d/touchfile
RESOLVPATH=/etc/resolv.conf
#HOSTN=$(cat /etc/hostname)
#FQDN=$(hostname -f)
#DOMN=$(hostname -d)
#HOSTNAME=`echo $FQDN |awk -F. '{ print $1 }'`
DPKG_QUERY=$(which dpkg-query)
ETHS=$(ip -o link show | awk -F': ' '{print $2}' | grep ^en)
MULTI=$(ip -o link show | awk -F': ' '{print $2}' | grep -c ^en)
if [ $MULTI -gt 1 ]; then
MULTIETH="YES"
else
MULTIETH="NO"
fi
ETH=$(ip -o link show | awk -F': ' '{print $2}' | grep ^en)
if [ $? = "1" ]; then
ETH=eth0
fi
#################################################################################
# root/sudo test ################################################################
#################################################################################
check_if_root() {
echo "check_if_root" >> $logfile
if [ `whoami` != root ]; then
whiptail --msgbox "This script must be run as root or with sudo" 20 60 2
exit 1
fi
}
#################################################################################
# Functions #####################################################################
#################################################################################
mask2cidr() {
nbits=0
IFS=.
for dec in $1 ; do
case $dec in
255) let nbits+=8;;
254) let nbits+=7 ; break ;;
252) let nbits+=6 ; break ;;
248) let nbits+=5 ; break ;;
240) let nbits+=4 ; break ;;
224) let nbits+=3 ; break ;;
192) let nbits+=2 ; break ;;
128) let nbits+=1 ; break ;;
0) let nbits+=0 ; break ;;
*) whiptail --msgbox "Error: parsing netmask $dec is not recognised" 20 60 1; exit 1
esac
done
}
do_dhcpcd_yn() {
systemctl status dhcpcd 2> /dev/null 1> /dev/null
if [ $? = 0 ]; then
DHCPCD_YN=YES
else
DHCPCD_YN=NO
fi
}
do_static_dhcp_menu() {
while true; do
FUNIP=$(whiptail --title "DHCP or Static" --menu "Network addressing options" 20 80 5 --cancel-button Back --ok-button Select \
"S Static" "Set Static IP addressing" \
"D DHCP" "Set DHCP addressing" \
3>&1 1>&2 2>&3)
RETIP=$?
if [ $RETIP -eq 1 ]; then
return
elif [ $RETIP -eq 0 ]; then
case "$FUNIP" in
S\ *) do_set_static ;;
D\ *) do_set_dhcp ;;
*) whiptail --msgbox "Whoooops, script error: unrecognized option" 20 60 1 ;;
esac || whiptail --msgbox "There was an error running option $FUNA" 20 60 1
fi
done
return
}
do_set_static() {
if [ $MULTIETH = "YES" ]; then
ETH_IFACE=$(whiptail --inputbox "Enter Ethernet interface to use:\n$ETHS \n" 20 78 3>&1 1>&2 2>&3)
ETH=$ETH_IFACE
fi
IPADDR=$(whiptail --inputbox "Enter the IP address for interface $ETH \nin the form XXX.XXX.XXX.XXX: " 8 78 3>&1 1>&2 2>&3)
NETMASK=$(whiptail --inputbox "Enter the netmask in the form XXX.XXX.XXX.XXX: " 8 78 3>&1 1>&2 2>&3)
mask2cidr $NETMASK
numbits=$nbits
DGW=$(whiptail --inputbox "Enter the default gateway in the form XXX.XXX.XXX.XXX: " 8 78 3>&1 1>&2 2>&3)
PDNS=$(whiptail --inputbox "Enter the address of the primary nameserver in the form XXX.XXX.XXX.XXX: " 8 78 3>&1 1>&2 2>&3)
SDNS=$(whiptail --inputbox "Enter the address of the secondary nameserver in the form XXX.XXX.XXX.XXX: " 8 78 3>&1 1>&2 2>&3)
if (whiptail --yesno "So we have: \n\nIP Address/CIDR : $IPADDR/$numbits\nNetmask : $NETMASK\nDefault Gateway : $DGW\nPrimary DNS : $PDNS\nSecondary DNS : $SDNS\n\nSelect yes to continue or no to start over." 30 80); then
# yes, write it
do_static_write
else
# no, re-enter
do_set_static
fi
}
do_static_write() {
if [ "$DHCPCD_YN" = "YES" ]; then
# write dhcpd.conf
cp -r /etc/dhcpcd.conf /etc/dhcpcd.conf.orig
whiptail --msgbox "Removing any previous static entries" 20 60 2
sed -i '/^profile static/d' /etc/dhcpcd.conf
sed -i '/^interface/d' /etc/dhcpcd.conf
sed -i '/^static/d' /etc/dhcpcd.conf
whiptail --msgbox "Writing static settings in /etc/dhcpcd.conf" 20 60 2
echo "# define static profile for eth0" >> /etc/dhcpcd.conf
echo "profile static_$ETH" >> /etc/dhcpcd.conf
echo "interface $ETH" >> /etc/dhcpcd.conf
echo "static ip_address=$IPADDR/$numbits" >> /etc/dhcpcd.conf
echo "static routers=$DGW" >> /etc/dhcpcd.conf
echo "static domain_name_servers=$PDNS, $SDNS" >> /etc/dhcpcd.conf
ip link show | grep wlan > /dev/null
if [ $? = 0 ]; then
whiptail --msgbox "Wireless interface found Writing settings for wlan0 also" 20 60 1
echo "# define static profile for wlan " >> /etc/dhcpcd.conf
echo "profile static_wlan0" >> /etc/dhcpcd.conf
echo "interface wlan0" >> /etc/dhcpcd.conf
echo "static ip_address=$IPADDR/$numbits" >> /etc/dhcpcd.conf
echo "static routers=$DGW" >> /etc/dhcpcd.conf
echo "static domain_name_servers=$PDNS, $SDNS" >> /etc/dhcpcd.conf
fi
whiptail --msgbox "Finished writing DHCPCD settings for static IP addressing\nChanges will take effect after reboot" 10 78
else
# rewrite /etc/network/interfaces
whiptail --msgbox "DHCPCD not used, writing settings to /etc/network/interfaces" 20 60 2
sed -i '/address/d' /etc/network/interfaces
sed -i '/netmask/d' /etc/network/interfaces
sed -i '/gateway/d' /etc/network/interfaces
sed -i "/iface $ETH inet/c\iface $ETH inet static" /etc/network/interfaces
sed -i "/iface $ETH inet static/a address $IPADDR \nnetmask $NETMASK \ngateway $DGW \ndns-nameservers $PDNS $SDNS" /etc/network/interfaces
ip link show | grep wlan > /dev/null
if [ $? = 0 ] ;then
sed -i "/iface wlan0 inet/c\iface $ETH inet static" /etc/network/interfaces
sed -i "/iface wlan0 inet static/a address $IPADDR \nnetmask $NETMASK \ngateway $DGW \ndns-nameservers $PDNS $SDNS" /etc/network/interfaces
fi
whiptail --msgbox "Finished writing settings in interfaces file for static IP addressing\nChanges will take effect after reboot" 10 78
fi
}
do_set_dhcp() {
if [ "$DHCPCD_YN" = "YES" ]; then
# read /etc/dhcpcd.conf and comment out any static settings.
cp -f /etc/dhcpcd.conf /etc/dhcpcd.conf.static
# profile static_eth0
sed -i 's/^profile static*/#&/' /etc/dhcpcd.conf
# interface eth0
sed -i "s/^interface $ETH/#&/" /etc/dhcpcd.conf
# static ip_address=192.168.1.23/24
sed -i 's/^static ip_add*/#&/' /etc/dhcpcd.conf
# static routers=192.168.1.1
sed -i 's/^static router*/#&/' /etc/dhcpcd.conf
# static domain_name_servers=192.168.1.1
sed -i 's/^static domain_nam*/#&/' /etc/dhcpcd.conf
whiptail --msgbox "Commented out any static IP settings in /etc/dhcpcd.conf \nreturning to DHCP IP addressing\nChanges will take effect after reboot" 10 78
else
# rewrite /etc/network/interfaces
sed -i "/iface $ETH inet/c\iface $ETH inet dhcp" /etc/network/interfaces
ip link show | grep wlan > /dev/null
if [ $? =0 }; then
sed -i "/iface wlan0 inet/c\iface wlan0 inet dhcp" /etc/network/interfaces
fi
sed -i '/address/d' /etc/network/interfaces
sed -i '/netmask/d' /etc/network/interfaces
sed -i '/gateway/d' /etc/network/interfaces
sed -i '/dns-nameservers/d' /etc/network/interfaces
whiptail --msgbox "Finished writing settings in interfaces for DHCP addressing\nChanges will take effect after reboot" 10 78
fi
}
#################################################################################
# Main body here ################################################################
#################################################################################
check_if_root
if (whiptail --title "Network setup" --yesno "Network DHCP or Static setup.\n\nDo you want to do this now?" 15 78) then
ANSWER=$?
else
ANSWER=$?
fi
if [ $ANSWER = 1 ]
then
whiptail --msgbox "You may set up your IP address settings later" 8 78
exit 0
else
do_dhcpcd_yn
do_static_dhcp_menu
fi