-
Notifications
You must be signed in to change notification settings - Fork 0
/
s_network.sh
308 lines (239 loc) · 6.79 KB
/
s_network.sh
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
WIZARD=$1
trap bashtrap INT
bashtrap()
{
bash ~/setup/setup.sh
}
cd ~/setup/
OLDINTERFACES=/etc/network/interfaces
NEWINTERFACES=/etc/network/interfaces
INTERFACE=`/sbin/ifconfig | grep ^eth | sed "s/ .*//" | head -n 1`
function network {
echo ""
CURRENT_HOST=$(hostname)
read -e -p "HOST NAME: " -i "$CURRENT_HOST" CHANGE_HOST
if [ "$CURRENT_HOST" != "$CHANGE_HOST" ]; then
echo $CHANGE_HOST >/etc/hostname
cat /etc/hosts | sed "s/^127\.0\.1\.1[ ].*$/127.0.1.1 $CHANGE_HOST/" >/etc/hosts.new; sync; mv /etc/hosts.new /etc/hosts
echo "host saved"
fi
echo ""
echo "------"
echo ""
readNetwork
}
function readNetwork {
while :
do
choice=""
while [ -z "$choice" ]
do
echo "Please choose a network type [1 or 2]?"
echo "1) DHCP"
echo "2) Static"
read -e -p "Netork Type: " -i "" choice
done
if [ $choice = 1 ] ; then
readNetworkDHCP
else
if [ $choice = 2 ] ; then
readNetworkIP
else
echo "Please make a choice between 1-2!"
readNetwork
fi
fi
done
}
function readNetworkDHCP {
echo ""
echo "using DHCP"
cat <<EOF >$NEWINTERFACES
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto $INTERFACE
iface $INTERFACE inet dhcp
EOF
proxyfn
}
function readNetworkIP {
CUR_IFCONFIG=`/sbin/ifconfig $INTERFACE`
if grep -q "^iface $INTERFACE"".*static" $OLDINTERFACES
then
CURRENT_IP=`grep "^ address " $OLDINTERFACES | sed "s/^ address //"`
CURRENT_SUB=`grep "^ netmask " $OLDINTERFACES | sed "s/^ netmask //"`
CURRENT_GATEWAY=`grep "^ gateway " $OLDINTERFACES | sed "s/^ gateway //"`
CURRENT_DNS1=`grep "^ dns-nameservers " $OLDINTERFACES | sed "s/^ dns-nameservers \([^ ]*\).*$/\1/"`
CURRENT_DNS2=`grep "^ dns-nameservers [^ ]* " $OLDINTERFACES | sed "s/^ dns-nameservers [^ ]* \([^ ]*\).*$/\1/"`
MUSTSAVE="f"
else
CURRENT_IP=""
CURRENT_SUB=""
CURRENT_GATEWAY=""
CURRENT_DNS1=""
CURRENT_DNS2=""
MUSTSAVE="t"
fi
if [ -z "$CURRENT_IP" ]; then
CURRENT_IP=`echo $CUR_IFCONFIG | sed "s/.*inet addr:\([0-9\.]*\).*/\1/"`
fi
if [ -z "$CURRENT_SUB" ]; then
CURRENT_SUB=`echo $CUR_IFCONFIG | sed "s/.*Mask:\([0-9\.]*\).*/\1/"`
fi
NUMNAMESERVERS=`grep nameserver /etc/resolv.conf | wc -l`
NS1=""
NS2=""
if [ $NUMNAMESERVERS -ge 1 ]
then
NS1=`grep nameserver /etc/resolv.conf | head -n 1 | sed "s/.*nameserver //"`
fi
if [ $NUMNAMESERVERS -ge 2 ]
then
NS2=`grep nameserver /etc/resolv.conf | head -n 2 | tail -n 1 | sed "s/.*nameserver //"`
fi
if [ -z "$CURRENT_GATEWAY" ]; then
CURRENT_GATEWAY=`/sbin/route -n | grep "^0\.0\.0\.0" | grep $INTERFACE | sed "s/[^ ]*[ ]*\([^ ]*\).*/\1/"`
fi
if [ -z "$CURRENT_DNS1" ]; then
CURRENT_DNS1=$NS1
fi
if [ -z "$CURRENT_DNS2" ]; then
CURRENT_DNS2=$NS2
fi
echo "current"
echo " IP: $CURRENT_IP"
echo " SUBNET: $CURRENT_SUB"
echo " GATEWAY: $CURRENT_GATEWAY"
echo " DNS1: $CURRENT_DNS1"
echo " DNS2: $CURRENT_DNS2"
echo ""
if [ "$MUSTSAVE" = "t" ]; then
read -e -p "You need to save these settings. Save now? (y to save | n to edit): " -i "y" saveNetowrk
if [ "$saveNetowrk" = "y" ]; then
CHANGE_IP=$CURRENT_IP
CHANGE_SUB=$CURRENT_SUB
CHANGE_GATEWAY=$CURRENT_GATEWAY
CHANGE_DNS1=$CURRENT_DNS1
CHANGE_DNS2=$CURRENT_DNS2
saveNetworkIP
else
changeNetworkIP
fi
else
read -e -p "Do you want to change these?: " -i "n" changeNetowrk
if [ "$changeNetowrk" = "y" ]; then
changeNetworkIP
else
proxyfn
fi
fi
}
function changeNetworkIP {
echo ""
echo "Change: "
read -e -p "IP: " -i "$CURRENT_IP" CHANGE_IP
read -e -p "SUBNET: " -i "$CURRENT_SUB" CHANGE_SUB
read -e -p "GATEWAY: " -i "$CURRENT_GATEWAY" CHANGE_GATEWAY
read -e -p "DNS1: " -i "$CURRENT_DNS1" CHANGE_DNS1
read -e -p "DNS2: " -i "$CURRENT_DNS2" CHANGE_DNS2
echo ""
read -e -p "Save?: " -i "y" NETWORK_SAVE
if [ $NETWORK_SAVE = "y" ]; then
saveNetworkIP
else
readNetwork
fi
}
function saveNetworkIP {
cat <<EOF >$NEWINTERFACES
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto $INTERFACE
iface $INTERFACE inet static
address $CHANGE_IP
netmask $CHANGE_SUB
#network $CHANGE_NETWORK
#broadcast $CHANGE_BCAST
gateway $CHANGE_GATEWAY
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers $CHANGE_DNS1 $CHANGE_DNS2
EOF
echo "saved"
echo " IP: $CHANGE_IP, SUBNET: $CHANGE_SUB, GATEWAY: $CHANGE_GATEWAY, DNS1: $CHANGE_DNS1, DNS2: $CHANGE_DNS2"
proxyfn
}
function proxyfn {
CURRENT_PROXY=$http_proxy
echo ""
echo "--- Proxy Server ---"
echo "Current: $CURRENT_PROXY"
read -e -p "Do you want to change the proxy?: " -i "n" changeProxy
if [ "$changeProxy" = "y" ]; then
echo ""
echo "must be in format http://DOMAIN\USERNAME:PASSWORD@SERVER:PORT/ leave blank to disable the proxy"
read -e -p "Proxy: " -i "$CURRENT_PROXY" CHANGE_PROXY
if [ "$CHANGE_PROXY" != "$CURRENT_PROXY" ]; then
export http_proxy=$CHANGE_PROXY
export https_proxy=$CHANGE_PROXY
export HTTP_PROXY=$CHANGE_PROXY
export HTTPS_PROXY=$CHANGE_PROXY
git config --global http.proxy $CHANGE_PROXY
# TODO gsettings set org.gnome.system.proxy mode 'manual'
# TODO gsettings set org.gnome.system.proxy.http host 'myproxy.server.com'
# TODO gsettings set org.gnome.system.proxy.http port 8080
cat <<EOF > /etc/apt/apt.conf
Acquire::http::proxy "$CHANGE_PROXY";
Acquire::https::proxy "$CHANGE_PROXY";
EOF
PROXY_CONTENTS=""
if [ -n "$CHANGE_PROXY" ]; then
PROXY_CONTENTS="$CHANGE_PROXY"
fi
# writes just the proxy stuff to a file.. that my update script in php can read it and use it for git pulls. some stupid reason php shell_exec and git pull dont listen to the git config file (no $HOME folder for shell_exec)
cat <<EOF > /media/data/use_proxy
$PROXY_CONTENTS
EOF
fi
fi
finish
}
function finish {
echo ""
echo "Activating the configuration"
sleep 1
ifdown $INTERFACE
ifup $INTERFACE
hostname --file /etc/hostname
sudo service hostname restart
echo "--- Done ---"
endfn
}
function endfn {
echo ""
echo ""
read -p "Press [Enter] key to continue..."
if [ -n "$WIZARD" ]; then
bash ./s_folder.sh "$WIZARD"
else
bash ./setup.sh " - Netowrking Done"
fi
}
clear
echo "Networking"
echo "-----------------------------------------------"
echo ""
read -e -p "Continue?: " -i "y" goon
if [ $goon = "y" ]; then
network
else
endfn
fi