Skip to content

Instantly share code, notes, and snippets.

@fortran01
Created September 22, 2023 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fortran01/e1a10a5d432a464ec73ac047efb250f4 to your computer and use it in GitHub Desktop.
Save fortran01/e1a10a5d432a464ec73ac047efb250f4 to your computer and use it in GitHub Desktop.
Updates the IP in Ubuntu based on netplan
#!/bin/bash
echo "Available network interfaces:"
ip link show | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2;getline}'
read -p "Choose the network interface to modify (e.g., enp0s3): " INTERFACE
echo "Available netplan configuration files:"
ls /etc/netplan
read -p "Choose the netplan configuration file to modify (e.g., 01-netcfg.yaml): " NETPLAN_FILE
read -p "Enter the IP address and subnet mask in CIDR notation (e.g., 192.168.1.2/24): " IP_ADDRESS
read -p "Enter the gateway IP address (e.g., 192.168.1.1): " GATEWAY_ADDRESS
read -p "Enter the primary DNS IP address (e.g., 8.8.8.8): " PRIMARY_DNS_ADDRESS
read -p "Enter the secondary DNS IP address (e.g., 8.8.4.4): " SECONDARY_DNS_ADDRESS
NETPLAN_CONFIG=$(cat << EOF
network:
version: 2
ethernets:
$INTERFACE:
dhcp4: no
addresses:
- $IP_ADDRESS
gateway4: $GATEWAY_ADDRESS
nameservers:
addresses: [$PRIMARY_DNS_ADDRESS, $SECONDARY_DNS_ADDRESS]
EOF
)
echo "Updating netplan configuration file..."
echo "$NETPLAN_CONFIG" | sudo tee /etc/netplan/$NETPLAN_FILE > /dev/null
read -p "Do you want to apply the changes with 'netplan apply' or 'netplan try'? (apply/try): " NETPLAN_ACTION
if [ "$NETPLAN_ACTION" == "apply" ]; then
echo "Applying changes with 'netplan apply'..."
sudo netplan apply
elif [ "$NETPLAN_ACTION" == "try" ]; then
echo "Applying changes with 'netplan try'..."
sudo netplan try
else
echo "Invalid input. Exiting without applying changes."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment