Skip to content

Instantly share code, notes, and snippets.

@hitesh83
Created November 14, 2025 03:52
Show Gist options
  • Select an option

  • Save hitesh83/c140678288bd55600f0f94e33b2f5ca3 to your computer and use it in GitHub Desktop.

Select an option

Save hitesh83/c140678288bd55600f0f94e33b2f5ca3 to your computer and use it in GitHub Desktop.
netplan
#!/bin/bash
# interactive_netplan.sh
NETPLAN_FILE="/etc/netplan/01-netcfg.yaml"
BACKUP_FILE="/etc/netplan/01-netcfg.yaml.bak"
if [ -f "$NETPLAN_FILE" ]; then
cp "$NETPLAN_FILE" "$BACKUP_FILE"
echo "Backup created at $BACKUP_FILE"
fi
read -p "Enter interface name (e.g. eth0): " IFACE
read -p "Use DHCP? (yes/no): " DHCP
if [ "$DHCP" == "yes" ]; then
cat <<EOF > $NETPLAN_FILE
network:
version: 2
ethernets:
$IFACE:
dhcp4: true
EOF
else
read -p "Enter static IP (e.g. 192.168.1.100): " IPADDR
read -p "Enter subnet prefix length (e.g. 24): " SUBNET
read -p "Enter gateway (e.g. 192.168.1.1): " GATEWAY
read -p "Enter nameservers (comma separated): " DNS
cat <<EOF > $NETPLAN_FILE
network:
version: 2
ethernets:
$IFACE:
addresses: [$IPADDR/$SUBNET]
gateway4: $GATEWAY
nameservers:
addresses: [${DNS// /}]
EOF
fi
netplan apply
echo "Netplan applied successfully for $IFACE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment