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.

nin.sh 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/bin/bash
  2. # Version 0.6
  3. # Author: Brandon Nolet
  4. # Contact: nolet.brandon@gmail.com
  5. # Reading this script:
  6. # Global variables use ALL CAPS
  7. # Non-global variables use UpperCamelCase
  8. # Might add more to this later
  9. function usage() {
  10. echo "
  11. Description: This is a basic script that makes use
  12. of the nmcli command, setting either a static or
  13. dynamic IPv4 address. The script will take one or
  14. no arguments. Should no arguments be passed to the
  15. script, then the user will be prompted for the mode
  16. to set the interface to. Otherwise the syntax is as follows
  17. Syntax:
  18. ./nin.sh [dynamic|static|usage]
  19. "
  20. }
  21. #Global Vars
  22. NMCLI_BIN=$(which nmcli)
  23. TIMESTAMP=$(date +%H:%M:%S)
  24. LOGFILE=nin.log
  25. TEELOG="tee --append ${LOGFILE}"
  26. # `setstatic` works as a state machine, running through
  27. # each case statement in order of operation. This is Done
  28. # by the use of the setstatic function couples with the first
  29. # argument corresponding to the case statement desiring to
  30. # be returned to
  31. function setstatic {
  32. #Getting Static IP info
  33. case $1 in #case to enable returnability
  34. 1)
  35. conshow
  36. setstatic 2
  37. ;;
  38. 2)
  39. echo
  40. read -p "Enter the desired IPv4 address: " IPAdd
  41. echo
  42. validip 2 3 ${IPAdd}
  43. ;;
  44. 3)
  45. read -p "Enter the IPv4 prefix length: " PrefLen
  46. if [[ ${PrefLen} -gt 0 ]] && [[ ${PrefLen} -lt 32 ]]; then #IPs only have 32 bits
  47. setstatic 4
  48. else
  49. echo "Not a valid prefix length."
  50. echo
  51. setstatic 3
  52. fi
  53. echo
  54. ;;
  55. 4)
  56. echo
  57. read -p "Enter the default router/gateway IP: " GatewayIP
  58. echo
  59. validip 4 5 ${GatewayIP}
  60. ;;
  61. 5)
  62. read -p "Enter the DNS server address(es): " DNSAdd
  63. echo
  64. validip 5 6 "${DNSAdd}"
  65. ;;
  66. 6)
  67. #Setting Static IP info
  68. echo "Configuring..."
  69. ${NMCLI_BIN} connection modify "${IntID}" ipv4.addresses ${IPAdd}/${PrefLen}
  70. ${NMCLI_BIN} connection modify "${IntID}" ipv4.gateway ${GatewayIP}
  71. ${NMCLI_BIN} connection modify "${IntID}" ipv4.dns "${DNSAdd}" #Surrounded in quotes for multiple DNS servers
  72. ${NMCLI_BIN} connection modify "${IntID}" ipv4.method manual
  73. echo "${TIMESTAMP}: IPv4 address of ${IntID} set to ${IPAdd}/${PrefLen}." >> ${LOGFILE}
  74. echo "${TIMESTAMP}: ${IntID} default gateway set to ${GatewayIP}." >> ${LOGFILE}
  75. echo "${TIMESTAMP}: ${IntID}'s DNS servers are now ${DNSAdd}." >> ${LOGFILE}
  76. echo "${TIMESTAMP}: ${IntID}'s resulting addressing method is \"Static\"" >> ${LOGFILE}
  77. reloadint
  78. ;;
  79. esac
  80. }
  81. function setdynamic() {
  82. conshow
  83. echo
  84. echo "Configuring..."
  85. #This is all that's really needed to set the interface to
  86. #DHCP addressing
  87. ${NMCLI_BIN} connection modify "${IntID}" ipv4.method auto
  88. reloadint
  89. }
  90. function reloadint() {
  91. #Cycles the state of the selected interface
  92. #to apply the changes
  93. echo "The network interface will be momentarily disconnected."
  94. sleep 1 #cosmetic
  95. read -s -p "Please pause your downloads and whatnot, then press enter when ready."; echo
  96. echo "Applying changes..."
  97. ${NMCLI_BIN} connection down "${IntID}" | sed "s/^/${TIMESTAMP}: /" >> ${LOGFILE}
  98. sleep 1 #for effect
  99. ${NMCLI_BIN} connection up "${IntID}" | sed "s/^/${TIMESTAMP}: /" >> ${LOGFILE}
  100. echo "Done!"
  101. }
  102. function conshow() {
  103. echo
  104. ${NMCLI_BIN} connection show --active | ${TEELOG}
  105. echo
  106. read -p "Type the name of the interface you wish to configure: " IntID
  107. echo "User input: IntID=${IntID}" >> ${LOGFILE}
  108. if [[ "${IntID}" != '' ]]; then #empty strings make grep have a fit
  109. ${NMCLI_BIN} -t connection show | cut -d : -f 1 | grep "${IntID}" &> /dev/null
  110. if [[ $? != 0 ]]; then #checking for valid interface name being input
  111. echo
  112. echo "Sorry, that's not the name of a valid interface. Try again." | ${TEELOG}
  113. echo
  114. sleep 1
  115. conshow #would love to have a non-inception like way to do this.
  116. else
  117. #incomplete entry of interface results in completion by grep ;) Interesting I suppose
  118. IntID=$(${NMCLI_BIN} -t con show | cut -d : -f 1 | grep "${IntID}" | head -1)
  119. return 0 #If the input matches the name of an interface, move on
  120. fi
  121. else #empty string is empty, prompt again
  122. echo
  123. echo "Sorry, that's not the name of a valid interface, try again"
  124. echo
  125. sleep 1
  126. conshow
  127. fi
  128. echo
  129. }
  130. # `validip` is meant to check for proper IP address formatting
  131. # `validip` accepts 3 arguments. The first is the state (an integer)
  132. # to return to on incorrect format detection. The second is the state
  133. # to move to on success
  134. function validip { #made specifically for the setstatic state machine
  135. declare -i n=0 #Need some math up in here!
  136. for i in $3; do #with DNS, multiple IPs need to be parsed for format
  137. # VVV it's ugly...but it works
  138. echo $i | egrep -o "^((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-fA-F]|[a-fA-F][a-fA-F0-9\-]*[a-fA-F0-9])\.)*([A-Fa-f]|[A-Fa-f][A-Fa-f0-9\-]*[A-Fa-f0-9])$|^(?:(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9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,3}(?:(?:[0-9a-fA-F]{1,4})))?::(?:(?:[0-9a-fA-F]{1,4})):)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,4}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,5}(?:(?:[0-9a-fA-F]{1,4})))?::)(?:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,6}(?:(?:[0-9a-fA-F]{1,4})))?::)))))$" > /dev/null
  139. n=n+$? #if ANY of the passed IPs are invalid, this increments
  140. done
  141. if [ ${n} == 0 ]; then
  142. setstatic $2 #if all is good, move on to next state
  143. else
  144. echo
  145. echo $3 >> ${LOGFILE}
  146. echo "Sorry, that's not a valid IP. Try again." | ${TEELOG}
  147. echo
  148. setstatic $1 #return to previous state
  149. fi
  150. }
  151. function getchoice() {
  152. if [[ $1 == '' ]]; then
  153. read -p "Do you want to configure static or DHCP addressing? (static/dhcp) " CHOICE
  154. CHOICE=`echo ${CHOICE}|tr '[:upper:]' '[:lower:]'`
  155. else
  156. CHOICE=`echo $1|tr '[:upper:]' '[:lower:]'`
  157. fi
  158. case ${CHOICE} in
  159. dhcp)
  160. setdynamic
  161. ;;
  162. static)
  163. setstatic 1
  164. ;;
  165. usage)
  166. usage
  167. ;;
  168. *)
  169. echo
  170. echo "Sorry, accepted values are 'dhcp' or 'static' without quotes."
  171. # echo "Try ./nin.sh usage for more information." Will put back when more options are available.
  172. echo
  173. sleep 1
  174. getchoice
  175. ;;
  176. esac
  177. }
  178. echo "Start of script run: ${TIMESTAMP}" >> $LOGFILE
  179. getchoice $1
  180. echo "End of script run: ${TIMESTAMP}" >> $LOGFILE
  181. exit 0
  182. # This program is free software: you can redistribute it and/or modify
  183. # it under the terms of the GNU General Public License as published by
  184. # the Free Software Foundation, either version 3 of the License, or
  185. # (at your option) any later version.
  186. #
  187. # This program is distributed in the hope that it will be useful,
  188. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  189. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  190. # GNU General Public License for more details.
  191. #
  192. # You should have received a copy of the GNU General Public License
  193. # along with this program. If not, see <http://www.gnu.org/licenses/>