Browse Source

Merge branch 'dev-0.6' into 'master'

0.6 Release

See merge request Brandonnolet/nmcli-basic!4
master
Brandon Nolet 1 year ago
parent
commit
1dce82dd77
3 changed files with 65 additions and 38 deletions
  1. 0
    1
      LICENSE.md
  2. 28
    14
      README.md
  3. 37
    23
      nin.sh

COPYING → LICENSE.md View File

@@ -672,4 +672,3 @@ may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.


+ 28
- 14
README.md View File

@@ -1,29 +1,43 @@
# nmcli-basic.sh

`nmcli-basic` is a script designed to make setting an interface's addressing parameters interactive and simple to do.
# `nin.sh`
`nin.sh` is a script designed to make setting an interface's addressing parameters interactive and simple to do.

## Installation
## Prerequisites

### Requirements
* Linux
* nmcli
* `bash`
* Linux platform
* `nmcli`

## Installing
Download nmcli-basic.sh and give it execution permissions by running `chmod +x nmcli-basic.sh`

###### or

Copy and paste this into your own text editor and save it with a .sh extension, then give it execution permissions.

## Usage
## Deployment

Add additional notes about how to deploy this on a live system

```bash
./nmcli-basic.sh [usage|static|dhcp]
```
## Built With

* [Bash](https://www.gnu.org/software/bash/)
* [Atom](https://atom.io/)

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.
Please make sure to test as appropriate.

## Authors

* **Brandon Nolet** - [Brandonnolet (Gitlab)](https://gitlab.com/brandonnolet)

See also the list of [contributors](https://gitlab.com/Brandonnolet/nmcli-basic/graphs/master) who participated in this project.

## License
[GPLv3](http://www.gnu.org/licenses/)

This project is licensed under the GPLv3 copyleft license. That means that this code and any derivative works are perpetually free - see the [LICENSE.md](LICENSE.md) file for details

## Why Should I Use This?

With basic static/DHCP IPv4 addressing, it's much faster than actually typing out the nmcli commands, especially when you have multiple interfaces to address.

nmcli-basic.sh → nin.sh View File

@@ -1,9 +1,14 @@
#!/bin/bash

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

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

function usage() {
echo "
Description: This is a basic script that makes use
@@ -14,14 +19,15 @@ function usage() {
to set the interface to. Otherwise the syntax is as follows

Syntax:
./nmcli-basic.sh [dynamic|static|usage]
./nin.sh [dynamic|static|usage]
"
}

#Global Vars
NMCLI_BIN=$(which nmcli)
TIMESTAMP=$(date +%H:%M:%S)
LOGFILE=nmcli-basic.log
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
@@ -39,6 +45,7 @@ case $1 in #case to enable returnability
setstatic 2
;;
2)
echo
read -p "Enter the desired IPv4 address: " IPAdd
echo
validip 2 3 ${IPAdd}
@@ -55,6 +62,7 @@ case $1 in #case to enable returnability
echo
;;
4)
echo
read -p "Enter the default router/gateway IP: " GatewayIP
echo
validip 4 5 ${GatewayIP}
@@ -68,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
@@ -85,8 +93,11 @@ esac
function setdynamic() {
conshow

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
}
@@ -97,30 +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() {

${NMCLI_BIN} con show | tee --append ${LOGFILE}
echo
${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
if [[ "${IntID}" != '' ]]; then #empty strings make grep have a fit
${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
@@ -138,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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-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
n=n+$? #if ANY of the passed IPs are invalid, this increments
done
@@ -150,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
@@ -160,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:]'`
@@ -178,7 +192,7 @@ function getchoice() {
*)
echo
echo "Sorry, that's not a valid option."
echo "Try ./nmcli-basic usage for more information."
echo "Try ./nin.sh usage for more information."
echo
sleep 1
getchoice
@@ -186,9 +200,9 @@ function getchoice() {
esac
}

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

exit 0


Loading…
Cancel
Save