diff --git a/files/helpers/set_dhcp_ip.sh b/files/helpers/set_dhcp_ip.sh new file mode 100644 index 0000000..3a2d5bd --- /dev/null +++ b/files/helpers/set_dhcp_ip.sh @@ -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" \ No newline at end of file diff --git a/files/helpers/set_static_ip.sh b/files/helpers/set_static_ip.sh new file mode 100644 index 0000000..bc6a409 --- /dev/null +++ b/files/helpers/set_static_ip.sh @@ -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" \ No newline at end of file