Browse Source

More comments + rename

clarifies some lines in the validip function and in the state machine

renamed apply() to reloadint(). Just seems like a more apt name.
master
Brandon Nolet 1 year ago
parent
commit
470bb28a3d
1 changed files with 24 additions and 11 deletions
  1. 24
    11
      nmcli-basic.sh

+ 24
- 11
nmcli-basic.sh View File

@@ -20,6 +20,12 @@ function usage() {

NMCLI_BIN=$(which nmcli)

# `setstatic` works as a state machine, running through
# each case statement in order of operation. This is Done
# by the use of the setstatic function couples with the first
# argument corresponding to the case statement desiring to
# be returned to

function setstatic {

#Getting Static IP info
@@ -36,7 +42,7 @@ case $1 in #case to enable returnability
;;
3)
read -p "Enter the IPv4 prefix length: " PrefLen
if [[ ${PrefLen} -gt 0 ]] && [[ ${PrefLen} -lt 32 ]]; then
if [[ ${PrefLen} -gt 0 ]] && [[ ${PrefLen} -lt 32 ]]; then #IPs only have 32 bits
setstatic 4
else
echo "Not a valid prefix length."
@@ -64,7 +70,7 @@ case $1 in #case to enable returnability
nmcli con mod ${IntID} ipv4.dns "${DNSAdd}" #Surrounded in quotes for multiple DNS servers
nmcli con mod ${IntID} ipv4.method manual

apply
reloadint
;;
esac
}
@@ -75,12 +81,13 @@ function setdynamic() {
echo "Configuring..."
nmcli con mod ${IntID} ipv4.method auto

apply
reloadint
}

function apply() {
function reloadint() {

#Applying the changes
#Cycles the state of the selected interface
#to apply the changes

echo "Applying changes..."
nmcli con down ${IntID} > /dev/null
@@ -120,19 +127,25 @@ function conshow() {

}

function validip {
declare -i n=0
for i in $3; do
# `valid IP` is meant to check for proper IP address formatting
# `validip` accepts 3 arguments. The first is the state (an integer)
# to return to on incorrect format detection. The second is the state
# to move to on success
#Know bug: will permit moving on with IPs greater than 255.255.255.255

function validip { #made specifically for the setstatic state machine
declare -i n=0 #Need some math up in here!
for i in $3; do #with DNS, multiple IPs need to be parsed for format
echo $i | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" > /dev/null
n=n+$?
n=n+$? #if ANY of the passed IPs are invalid, this increments
done
if [ ${n} == 0 ]; then
setstatic $2
setstatic $2 #if all is good, move on to next state
else
echo
echo "Sorry, that's not a valid IP. Try again."
echo
setstatic $1
setstatic $1 #return to previous state
fi

}

Loading…
Cancel
Save