A script to facilitate the changing of network settings for wired ethernet
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.0KB

  1. #!/bin/bash
  2. # Version 0.2
  3. # Author/Copyright: Brandon Nolet
  4. # Contact: nolet.brandon@gmail.com
  5. # Description: This is a basic script that makes use
  6. # of the nmcli command, setting either a static or
  7. # dynamic IPv4 address. The script will take one or
  8. # no arguments. Should no arguments be passed to the
  9. # script, then the user will be prompted for the mode
  10. # to set the interface to. Otherwise the syntax is as follows
  11. #
  12. # Syntax:
  13. # ./nmcli-basic.sh [dynamic|static]
  14. #
  15. # The script will also take automatic, auto, and dhcp
  16. # as valid dynamic address inputs.
  17. function setstatic() {
  18. #Getting Static IP info
  19. read -p "Enter the desired IPv4 address: " IPAdd
  20. echo
  21. read -p "Enter the IPv4 prefix length: " PrefLen
  22. echo
  23. read -p "Enter the default router/gateway IP: " GatewayIP
  24. echo
  25. read -p "Enter the DNS server address(es): " DNSAdd
  26. echo
  27. #Setting Static IP info
  28. echo "Configuring..."
  29. nmcli con mod ${IntID} ipv4.addresses ${IPAdd}/${PrefLen}
  30. nmcli con mod ${IntID} ipv4.gateway ${GatewayIP}
  31. nmcli con mod ${IntID} ipv4.dns "${DNSAdd}" #Surrounded in quotes for multiple DNS servers
  32. nmcli con mod ${IntID} ipv4.method manual
  33. apply
  34. }
  35. function setdynamic() {
  36. echo "Configuring..."
  37. nmcli con mod ${IntID} ipv4.method auto
  38. apply
  39. }
  40. function apply() {
  41. #Applying the changes
  42. echo "Applying changes..."
  43. nmcli con down ${IntID} > /dev/null
  44. nmcli con up ${IntID} > /dev/null
  45. echo "Done!"
  46. }
  47. function conshow() {
  48. nmcli con show
  49. echo
  50. read -p "Which interface to configure? " IntID
  51. echo
  52. }
  53. function usage() {
  54. echo "
  55. Description: This is a basic script that makes use
  56. of the nmcli command, setting either a static or
  57. dynamic IPv4 address. The script will take one or
  58. no arguments. Should no arguments be passed to the
  59. script, then the user will be prompted for the mode
  60. to set the interface to. Otherwise the syntax is as follows
  61. Syntax:
  62. ./nmcli-basic.sh [dynamic|static]
  63. The script will also take automatic, auto, and dhcp
  64. as valid inputs for dynamic addressing.
  65. "
  66. }
  67. case "$1" in
  68. [dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
  69. conshow
  70. setdynamic
  71. ;;
  72. [sS]tatic | [mM]anual)
  73. conshow
  74. setstatic
  75. ;;
  76. [uU]sage)
  77. usage
  78. ;;
  79. "")
  80. read -p "Static or DHCP? " CHOICE
  81. case ${CHOICE} in
  82. [dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
  83. setdynamic
  84. ;;
  85. [sS]tatic | [mM]anual)
  86. setstatic
  87. ;;
  88. esac
  89. esac
  90. exit 0
  91. # This program is free software: you can redistribute it and/or modify
  92. # it under the terms of the GNU General Public License as published by
  93. # the Free Software Foundation, either version 3 of the License, or
  94. # (at your option) any later version.
  95. #
  96. # This program is distributed in the hope that it will be useful,
  97. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  98. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  99. # GNU General Public License for more details.
  100. #
  101. # You should have received a copy of the GNU General Public License
  102. # along with this program. If not, see <http://www.gnu.org/licenses/>