Browse Source

Misc 0.6 stuff

* Rearranged some lines
* Cleaned up tee stuff
* Actually used `NMCLI_BIN` var
* Expanded `con` and `mod` words for legibility of the raw script
* Removed known bug line
* Fixed "try usage for more info"
* Added `field` option to conshow to allow usage on debian-based
machines (not yet tested)
* Added "Reading this script" section
master
Brandon Nolet 2 years ago
parent
commit
79e91d75e0
1 changed files with 29 additions and 19 deletions
  1. +29
    -19
      nin.sh

+ 29
- 19
nin.sh View File

@@ -4,6 +4,11 @@
# Author: Brandon Nolet
# Contact: nolet.brandon@gmail.com

# Reading this script:
# Global variables use ALL CAPS
# Local variables use UpperCamelCase
# Might add more to this later

function usage() {
echo "
Description: This is a basic script that makes use
@@ -22,6 +27,7 @@ function usage() {
NMCLI_BIN=$(which nmcli)
TIMESTAMP=$(date +%H:%M:%S)
LOGFILE=nin.log
TEELOG=$(tee --append ${LOGFILE})

# `setstatic` works as a state machine, running through
# each case statement in order of operation. This is Done
@@ -70,13 +76,13 @@ case $1 in #case to enable returnability
6)
#Setting Static IP info
echo "Configuring..."
nmcli con mod "${IntID}" ipv4.addresses ${IPAdd}/${PrefLen}
echo "${TIMESTAMP}: IPv4 address of ${IntID} set to ${IPAdd}/${PrefLen}." >> ${LOGFILE}
nmcli con mod "${IntID}" ipv4.gateway ${GatewayIP}
${NMCLI_BIN} connection modify "${IntID}" ipv4.addresses ${IPAdd}/${PrefLen}
${NMCLI_BIN} connection modify "${IntID}" ipv4.gateway ${GatewayIP}
${NMCLI_BIN} connection modify "${IntID}" ipv4.dns "${DNSAdd}" #Surrounded in quotes for multiple DNS servers
${NMCLI_BIN} connection modify "${IntID}" ipv4.method manual
echo "${TIMESTAMP}: IPv4 address of ${IntID} set to ${IPAdd}/${PrefLen}." >> ${LOGFILE}
echo "${TIMESTAMP}: ${IntID} default gateway set to ${GatewayIP}." >> ${LOGFILE}
nmcli con mod "${IntID}" ipv4.dns "${DNSAdd}" #Surrounded in quotes for multiple DNS servers
echo "${TIMESTAMP}: ${IntID}'s DNS servers are now ${DNSAdd}." >> ${LOGFILE}
nmcli con mod "${IntID}" ipv4.method manual
echo "${TIMESTAMP}: ${IntID}'s resulting addressing method is \"Static\"" >> ${LOGFILE}

reloadint
@@ -89,7 +95,9 @@ function setdynamic() {

echo
echo "Configuring..."
nmcli con mod "${IntID}" ipv4.method auto
#This is all that's really needed to set the interface to
#DHCP addressing
${NMCLI_BIN} connection modify "${IntID}" ipv4.method auto

reloadint
}
@@ -100,31 +108,32 @@ function reloadint() {
#to apply the changes

echo "Applying changes..."
nmcli con down "${IntID}" | sed "s/^/${TIMESTAMP}: /" >> ${LOGFILE}
nmcli con up "${IntID}" | sed "s/^/${TIMESTAMP}: /" >> ${LOGFILE}
${NMCLI_BIN} connection down "${IntID}" | sed "s/^/${TIMESTAMP}: /" >> ${LOGFILE}
sleep 1 #for effect
${NMCLI_BIN} connection up "${IntID}" | sed "s/^/${TIMESTAMP}: /" >> ${LOGFILE}
echo "Done!"
}

function conshow() {

echo
${NMCLI_BIN} con show | tee --append ${LOGFILE}
${NMCLI_BIN} -t -f NAME,UUID,TYPE,DEVICE connection show | ${TEELOG}

echo
read -p "Which interface to configure? " IntID
read -p "Which interface to configure? " IntID | ${TEELOG}

if [[ "${IntID}" != '' ]]; then #empty strings make grep have a fit
nmcli -t con show | cut -d : -f 1 | grep "${IntID}" &> /dev/null
${NMCLI_BIN} -t connection 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." | ${TEELOG}
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)
IntID=$(${NMCLI_BIN} -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
@@ -142,11 +151,11 @@ function conshow() {
# `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
# VVV it's ugly...but it works
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]|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})):){5})(?:(?:(?:(?:(?:[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-9a-fA-F]{1,4})):){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,1}(?:(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?:(?:(?:(?:[0-9a-fA-F]{1,4})):){0,6}(?:(?:[0-9a-fA-F]{1,4})))?::)))))$" > /dev/null
n=n+$? #if ANY of the passed IPs are invalid, this increments
done
@@ -154,7 +163,8 @@ function validip { #made specifically for the setstatic state machine
setstatic $2 #if all is good, move on to next state
else
echo
echo "Sorry, that's not a valid IP. Try again."
echo $3 >> ${LOGFILE}
echo "Sorry, that's not a valid IP. Try again." | ${TEELOG}
echo
setstatic $1 #return to previous state
fi
@@ -164,7 +174,7 @@ function validip { #made specifically for the setstatic state machine
function getchoice() {

if [[ $1 == '' ]]; then
read -p "Static or DHCP? " CHOICE
read -p "Do you want to configure static or DHCP addressing? " CHOICE
CHOICE=`echo ${CHOICE}|tr '[:upper:]' '[:lower:]'`
else
CHOICE=`echo $1|tr '[:upper:]' '[:lower:]'`
@@ -182,7 +192,7 @@ function getchoice() {
*)
echo
echo "Sorry, that's not a valid option."
echo "Try ./nin usage for more information."
echo "Try ./nin.sh usage for more information."
echo
sleep 1
getchoice
@@ -190,9 +200,9 @@ function getchoice() {
esac
}

echo "Start of script run: `date +%H:%M:%S`" >> $LOGFILE
echo "Start of script run: ${TIMESTAMP}" >> $LOGFILE
getchoice $1
echo "End of script run: `date +%H:%M:%S`" >> $LOGFILE
echo "End of script run: ${TIMESTAMP}" >> $LOGFILE

exit 0



Loading…
Cancel
Save