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.

nmcli-basic.sh 3.0KB

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