-
Notifications
You must be signed in to change notification settings - Fork 57
/
setup-network
executable file
·60 lines (48 loc) · 1.89 KB
/
setup-network
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
#!/run/current-system/sw/bin/bash
set -u
## Inspect what we can
echo "Inspecting eth0 and eth1..."
interfaces=/etc/network/interfaces
gateway=$(grep gateway "$interfaces" | awk '{print $2}')
ns1=$(grep "dns-nameservers" "$interfaces" | awk '{print $2}')
ns2=$(grep "dns-nameservers" "$interfaces" | awk '{print $3}')
eth0_enabled=$(grep eth0 "$interfaces")
eth0_address=$(grep address "$interfaces" | awk '{print $2}' | head -1)
eth0_prefix_length=$(grep -Eo "/..?" "$interfaces" | sed 's_/__' | head -1)
eth1_enabled=$(grep eth1 "$interfaces")
eth1_address=$(grep address "$interfaces" | awk '{print $2}' | tail -1)
eth1_prefix_length=$(grep -Eo "/..?" "$interfaces" | sed 's_/__' | tail -1)
## Takes 1) interface name; 2) address; 3) prefix length
function enable_interface
{
echo "Enabling $1..."
ip link set "$1" up
ip addr add "$2/$3" dev "$1"
}
## Takes 1) nameserver 1; 2) nameserver 2; 3) gateway
function enable_dns
{
echo "Enabling DNS..."
resolvconf -m 1 -a static <<EOF
nameserver $1
nameserver $2
EOF
ip route add default via "$3" || true
}
[ -n "$eth0_enabled" ] && enable_interface eth0 "$eth0_address" "$eth0_prefix_length"
[ -n "$eth1_enabled" ] && enable_interface eth1 "$eth1_address" "$eth1_prefix_length"
# NixOS 16.03 uses newer naming
[ -n "$eth0_enabled" ] && enable_interface enp0s3 "$eth0_address" "$eth0_prefix_length"
[ -n "$eth1_enabled" ] && enable_interface enp0s4 "$eth1_address" "$eth1_prefix_length"
# NixOS 17.09 uses newer naming
[ -n "$eth0_enabled" ] && enable_interface ens3 "$eth0_address" "$eth0_prefix_length"
[ -n "$eth1_enabled" ] && enable_interface ens8 "$eth1_address" "$eth1_prefix_length"
enable_dns "$ns1" "$ns2" "$gateway"
echo "All set!"
## Example data:
#iface eth0 inet static
# address 188.166.222.72
# netmask 255.255.240.0
# gateway 188.166.208.1
# up ip addr add 10.15.0.7/16 dev eth0
# dns-nameservers 8.8.8.8 8.8.4.4