Add static/dhcp ip helpers for NetworkManager
All checks were successful
ci/woodpecker/manual/lint Pipeline was successful
ci/woodpecker/manual/test Pipeline was successful
ci/woodpecker/manual/z.ntfy Pipeline was successful

This commit is contained in:
2023-10-18 16:58:24 +13:00
parent a3ee65b213
commit 682404a819
2 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
#!/bin/bash
# This script will reset the IP Address back to default of DHCP
# helpful for a pending restore
/bin/nmcli c m "System eth0" ipv4.method auto
/bin/nmcli c m "System eth0" ipv4.address "" ipv4.gateway ""
/bin/nmcli connection up "System eth0"

View File

@@ -0,0 +1,48 @@
#!/bin/bash
# get subnet
subnet=$(ip a | grep "inet " | tail -1 | awk '{print $2}')
# get router/gateway
router=$(ip route show | head -1 | awk '{print $3}')
# get size of network portion of address in bytes
sz=$(echo $subnet | awk -F / '{print $2}')
bytes=$(("$sz" / 8))
prefix=$(echo "$subnet" | cut -d. -f1-$bytes) # e.g., 192.168.0
# get IP address to be set
IP=$(hostname -I | awk '{print $1}') # current IP
echo -n "Keep IP address?—$IP [yn]> "
read -r ans
if [ "$ans" == "n" ]; then
echo -n "Enter new IP address: "
read -r IP
# check if specified IP is properly formatted
if [[ ! $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo Invalid IP
fi
# check if specified IP works for local network
if [[ ! $IP =~ ^$prefix ]]; then
echo "ERROR: Specified IP not usable for local network"
exit
fi
fi
# check if specified IP is properly formatted
if [[ ! $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo Invalid IP
fi
# fetch the UUID
UUID=$(nmcli connection show | tail -1 | awk '{print $4}')
if [[ "$UUID" == "ethernet" ]]; then
# This is the other format of nmcli connection show
UUID=$(nmcli connection show | head -2 | tail -1 | awk '{print $3}')
fi
# run commands to set up the permanent IP address
nmcli connection modify "$UUID" IPv4.address "$IP"/"$sz"
nmcli connection modify "$UUID" IPv4.gateway "$router"
nmcli connection modify "$UUID" IPv4.method manual
nmcli connection up "$UUID"