Browse Source

Merge branch 'dev-0.4' into 'master'

Dev 0.4

See merge request Brandonnolet/nmcli-basic!2
master
Brandon Nolet 1 year ago
parent
commit
8fab552a5c
1 changed files with 111 additions and 31 deletions
  1. 111
    31
      nmcli-basic.sh

+ 111
- 31
nmcli-basic.sh View File

@@ -1,6 +1,6 @@
#!/bin/bash

# Version 0.3
# Version 0.4
# Author/Copyright: Brandon Nolet
# Contact: nolet.brandon@gmail.com

@@ -23,29 +23,58 @@ function usage() {

NMCLI_BIN=$(which nmcli)

function setstatic() {
function setstatic {

#Getting Static IP info
read -p "Enter the desired IPv4 address: " IPAdd
echo
read -p "Enter the IPv4 prefix length: " PrefLen
echo
read -p "Enter the default router/gateway IP: " GatewayIP
echo
read -p "Enter the DNS server address(es): " DNSAdd
echo
case $1 in #case to enable returnability

#Setting Static IP info
echo "Configuring..."
nmcli con mod ${IntID} ipv4.addresses ${IPAdd}/${PrefLen}
nmcli con mod ${IntID} ipv4.gateway ${GatewayIP}
nmcli con mod ${IntID} ipv4.dns "${DNSAdd}" #Surrounded in quotes for multiple DNS servers
nmcli con mod ${IntID} ipv4.method manual
1)
conshow
setstatic 2
;;
2)
read -p "Enter the desired IPv4 address: " IPAdd
echo
validip 2 3 ${IPAdd}
;;
3)
read -p "Enter the IPv4 prefix length: " PrefLen
if [[ ${PrefLen} -gt 0 ]] && [[ ${PrefLen} -lt 32 ]]; then
setstatic 4
else
echo "Not a valid prefix length."
echo
setstatic 3
fi
echo
;;
4)
read -p "Enter the default router/gateway IP: " GatewayIP
echo
validip 4 5 ${GatewayIP}
;;
5)
read -p "Enter the DNS server address(es): " DNSAdd
echo
validip 5 6 "${DNSAdd}"
;;

apply
6)
#Setting Static IP info
echo "Configuring..."
nmcli con mod ${IntID} ipv4.addresses ${IPAdd}/${PrefLen}
nmcli con mod ${IntID} ipv4.gateway ${GatewayIP}
nmcli con mod ${IntID} ipv4.dns "${DNSAdd}" #Surrounded in quotes for multiple DNS servers
nmcli con mod ${IntID} ipv4.method manual

apply
;;
esac
}

function setdynamic() {
conshow

echo "Configuring..."
nmcli con mod ${IntID} ipv4.method auto

@@ -55,7 +84,7 @@ function setdynamic() {
function apply() {

#Applying the changes
echo "Applying changes..."
nmcli con down ${IntID} > /dev/null
nmcli con up ${IntID} > /dev/null
@@ -63,35 +92,86 @@ function apply() {
}

function conshow() {

${NMCLI_BIN} con show

echo
read -p "Which interface to configure? " IntID

if [[ ${IntID} != '' ]]; then #empty strings make grep have a fit
nmcli -t con show | cut -d : -f 1 | grep ${IntID} &> /dev/null
if [[ $? != 0 ]]; then #checking for valid interface name being input
echo
echo "Sorry, that's not the name of a valid interface. Try again."
echo
sleep 1

conshow #would love to have a non-inception like way to do this.
else
#incomplete entry of interface results in completion by grep ;) Interesting I suppose
IntID=$(nmcli -t con show | cut -d : -f 1 | grep ${IntID} | head -1)
return 0 #If the input matches the name of an interface, move on
fi
else #empty string is empty, prompt again
echo
echo "Sorry, that's not the name of a valid interface, try again"
echo
sleep 1
conshow
fi
echo

}

function validip {
declare -i n=0
for i in $3; do
echo $i | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" > /dev/null
n=n+$?
done
if [ ${n} == 0 ]; then
setstatic $2
else
echo
echo "Sorry, that's not a valid IP. Try again."
echo
setstatic $1
fi

}

function getchoice() {
read -p "Static or DHCP? " CHOICE
case ${CHOICE} in
[dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
setdynamic
;;
[sS]tatic | [mM]anual)
setstatic 1
;;
*)
echo
echo "Sorry, that's not a valid choice"
echo
sleep 1
getchoice
;;
esac
}

case "$1" in
[dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
conshow
setdynamic
;;
[sS]tatic | [mM]anual)
conshow
setstatic
setstatic 1
;;

[uU]sage)
usage
;;
"")
read -p "Static or DHCP? " CHOICE
case ${CHOICE} in
[dD]ynamic | [aA]uto | [aA]utomatic | [dD][hH][cC][pP])
setdynamic
;;
[sS]tatic | [mM]anual)
setstatic
;;
esac
getchoice
;;
*)
echo "Sorry, argument '${1}' not understood."
@@ -113,4 +193,4 @@ exit 0
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
# along with this program. If not, see <http://www.gnu.org/licenses/>

Loading…
Cancel
Save