-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle-static-ip.sh
More file actions
executable file
·53 lines (49 loc) · 1.44 KB
/
Copy pathtoggle-static-ip.sh
File metadata and controls
executable file
·53 lines (49 loc) · 1.44 KB
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
#!/bin/bash
set -eou pipefail
con=$(nmcli --terse con show | grep ethernet | cut -d: -f1)
address=192.168.42.2/24
gateway=192.168.42.1
dns=127.0.0.1
static-ip() {
method=$(nmcli --terse con show "$con" | grep 'ipv4.method')
case "${1:-''}" in
apply)
if [[ "$method" = ipv4.method:manual ]]; then
echo "static ip already setup"
return
fi
echo "setting up static ip"
echo address=$address
echo gateway=$gateway
echo dns=$dns
sudo nmcli con mod "$con" \
ipv4.address "$address" \
ipv4.gateway "$gateway" \
ipv4.dns "$dns" \
ipv4.dns-search "lan" \
ipv4.method "manual"
sudo nmcli con down "$con"
sudo nmcli con up "$con"
;;
revert)
if [[ "$method" = ipv4.method:auto ]]; then
echo "dynamic ip already setup"
return
fi
echo "enabling dhcp"
sudo nmcli con mod "$con" \
ipv4.address "" \
ipv4.gateway "" \
ipv4.dns "" \
ipv4.dns-search "" \
ipv4.method "auto"
sudo nmcli con down "$con"
sudo nmcli con up "$con"
;;
*)
echo "usage $0 apply|revert"
return 1
;;
esac
}
static-ip "$@"