netplan-tui.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. dhcp=""
  3. filename="config.yaml"
  4. #filename="50-cloud-init.yaml"
  5. # Assign values to the variables
  6. ip=""
  7. gateway=""
  8. dns=""
  9. device=""
  10. devices=$(ls /sys/class/net/)
  11. DIALOG${DIALOG=dialog}
  12. $DIALOG --title "Netplan TUI" --clear \
  13. --defaultno --yesno "Do you want to use DHCP? (Usually no)" 10 45
  14. case $? in
  15. 0)
  16. device=$(dialog --title "Netplan TUI" --backtitle "Netplan TUI" \
  17. --inputbox "Device: " 8 60 \
  18. 3>&1 1>&2 2>&3 3>&-)
  19. echo -e "network:\n\t version: 2\n\t renderer: networkd\n\t ethernets:\n\t\t $device:\n\t\t dhcp4: true" > /etc/netplan/$filename && netplan apply && clear && echo "Sucessfully changed Network Settings!" && exit;;
  20. 1)
  21. dhcp="no";;
  22. esac
  23. #open fd
  24. exec 3>&1
  25. # Store data to $VALUES variable
  26. VALUES=$(dialog --ok-label "Submit" \
  27. --backtitle "Netplan TUI" \
  28. --title "Netplan TUI" \
  29. --form "Netplan TUI" \
  30. 15 50 0 \
  31. "IP/CIDR:" 1 1 "$ip" 1 10 40 0 \
  32. "Gateway" 2 1 "$gateway" 2 10 40 0 \
  33. "DNS:" 3 1 "$dns" 3 10 40 0 \
  34. "Device:" 4 1 "$device" 4 10 40 0 \
  35. 2>&1 1>&3)
  36. #close fd
  37. exec 3>&-
  38. clear
  39. ipaddr=`echo $VALUES | cut -d ' ' -f 1`
  40. dgateway=`echo $VALUES | cut -d ' ' -f 2`
  41. ddns=`echo $VALUES | cut -d ' ' -f 3`
  42. ddevice=`echo $VALUES | cut -d ' ' -f 4`
  43. # Write to file
  44. echo -e "network:\n\t version: 2\n\t renderer: networkd\n\t ethernets:\n\t\t $ddevice:\n\t\t dhcp4: $dhcp\n\t\t addresses:\n\t\t\t - $ipaddr\n\t\t gateway4: $dgateway\n\t\t nameservers:\n\t\t\t addresses: [$ddns]" > /etc/netplan/$filename
  45. netplan apply
  46. echo "Sucessfully changed Network Config!"