-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupbox.sh
executable file
·105 lines (91 loc) · 2.76 KB
/
setupbox.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
#!/bin/bash
# sudo ./setupbox.sh --interface eth0 --mode set --ipaddress 172.19.1.235 --gateway 172.19.0.1 --dns 8.8.8.8 --type static --subnetmask 16
# sudo ./setupbox.sh --interface eth0 --mode set --type dynamic
# sudo ./setupbox.sh --interface eth0 --mode get
[[ -n ${SUDO_USER} ]] && SUDO="sudo "
#
# edit ip address within network manager
#$2 ipaddress $3 subnetmasK $4 gateway $5 dns
function nm_ip_editor ()
{
if [[ $? = 0 ]]; then
localuuid=$(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $1 | awk '{print $1}')
nmcli con mod $localuuid ipv4.method manual ipv4.addresses "$2/$3" >/dev/null 2>&1
nmcli con mod $localuuid ipv4.method manual ipv4.gateway "$4" >/dev/null 2>&1
nmcli con mod $localuuid ipv4.dns "$5,$4" >/dev/null 2>&1
nmcli con down $localuuid >/dev/null 2>&1
sleep 2
nmcli con up $localuuid >/dev/null 2>&1
fi
}
while [ -n "$1" ]; do
case "$1" in
--interface) export INTERFACE="$2"; shift;;
--mode) export MODE="$2"; shift;;
--type) export TYPE="$2"; shift;;
--ipaddress) export IPADDRESS="$2"; shift;;
--subnetmask) export SUBNETMASK="$2"; shift;;
--gateway) export GATEWAY="$2"; shift;;
--dns) export DNS="$2"; shift;;
-*)
echo "Invalid option: $1"
exit 1
;;
*) break;;
esac
shift;
done
if [ -z "$MODE" ]
then
echo "Missing mode"
exit 1
fi
if [ -z "$INTERFACE" ]
then
echo "Missing interface"
exit 1
fi
DEFAULT_ADAPTER="$INTERFACE"
if [ "$MODE" = "set" ] # mode set ip address
then
if [ -z "$TYPE" ]
then
echo "Missing type"
exit 1
fi
if [ "$TYPE" = "static" ]
then
if [ -z "$IPADDRESS" ]
then
echo "Missing ip address"
exit 1
fi
if [ -z "$GATEWAY" ]
then
echo "Missing default gateway"
exit 1
fi
if [ -z "$SUBNETMASK" ]
then
echo "Missing subnet mask"
exit 1
fi
if [ -z "$DNS" ]
then
echo "Missing dns"
exit 1
fi
nm_ip_editor "$DEFAULT_ADAPTER" "$IPADDRESS" "$SUBNETMASK" "$GATEWAY" "$DNS"
else
nmcli connection delete uuid $(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $DEFAULT_ADAPTER | awk '{print $1}') >/dev/null 2>&1
nmcli con add con-name "Armbian ethernet" type ethernet ifname $DEFAULT_ADAPTER >/dev/null 2>&1
nmcli con up "Armbian ethernet" >/dev/null 2>&1
fi
else # mode get ip address
localuuid=$(LC_ALL=C nmcli -f UUID,DEVICE connection show | grep $DEFAULT_ADAPTER | awk '{print $1}')
address=$(ip -4 addr show dev $DEFAULT_ADAPTER | awk '/inet/ {print $2}' | cut -d'/' -f1)
netmask=$(ip -4 addr show dev $DEFAULT_ADAPTER | awk '/inet/ {print $2}' | cut -d'/' -f2)
gateway=$(sudo route -n | grep $DEFAULT_ADAPTER | grep 'UG[ \t]' | awk '{print $2}')
dns=$(nmcli conn show $localuuid | grep "IP4.DNS\[1\]:" | awk '{print $2}')
echo {\"address\": \"$address\", \"netmask\": $netmask, \"gateway\": \"$gateway\", \"dns\": \"$dns\"}
fi