Browse Source

Most of IP checking done

Something's wrong with the way the argument variables are being used.
master
Brandon Nolet 1 year ago
parent
commit
214b624f54
1 changed files with 67 additions and 22 deletions
  1. 67
    22
      nmcli-basic.sh

+ 67
- 22
nmcli-basic.sh View File

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

NMCLI_BIN=$(which nmcli)



function setstatic() {
conshow
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
1)
conshow
setstatic 2
;;
2)
read -p "Enter the desired IPv4 address: " IPAdd
echo
validip ${IPAdd} 2 3
;;
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 ${GatewayIP} 4 5
;;
5)
read -p "Enter the DNS server address(es): " DNSAdd
echo
validip ${DNSAdd} 5 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
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
apply
;;
esac
}

function setdynamic() {
@@ -78,7 +102,7 @@ function conshow() {
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 "Sorry, that's not the name of a valid interface. Try again."
echo
sleep 1
@@ -99,6 +123,27 @@ function conshow() {

}

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

}

function getchoice() {
read -p "Static or DHCP? " CHOICE
case ${CHOICE} in
@@ -106,7 +151,7 @@ function getchoice() {
setdynamic
;;
[sS]tatic | [mM]anual)
setstatic
setstatic 1
;;
*)
echo
@@ -123,7 +168,7 @@ case "$1" in
setdynamic
;;
[sS]tatic | [mM]anual)
setstatic
setstatic 1
;;

[uU]sage)

Loading…
Cancel
Save