Browse Source

Version 0.2

* Added dynamic option
* Can use dhcp/dynamic or static as first argument
* Moved static into its own function for legibility
* Extra comments for clarity

Coming up:

* Valid input checking w/ error msgs
master
Brandon Nolet 2 years ago
parent
commit
ed511fdbfa
1 changed files with 75 additions and 21 deletions
  1. +75
    -21
      nmcli-basic.sh

+ 75
- 21
nmcli-basic.sh View File

@@ -1,31 +1,85 @@
#!/bin/bash

#Version 0.1
#Author: Brandon Nolet
#Contact: nolet.brandon@gmail.com
# Version 0.2
# Author: Brandon Nolet
# Contact: nolet.brandon@gmail.com

# Description: This is a basic script that makes use
# of the nmcli command, setting either a static or
# dynamic IPv4 address. The script will take one or
# no arguments. Should no arguments be passed to the
# script, then the user will be prompted for the mode
# to set the interface to. Otherwise the syntax is as follows
#
# Syntax:
# ./nmcli-basic.sh dynamic|static
#
# The script will also take automatic, auto, and dhcp
# as valid dynamic address inputs.


nmcli con show



echo
read -p "Which interface to configure? " ID
echo
read -p "What's the ip? " IP
echo
read -p "And the prefix length? " PL
echo
read -p "What's the gateway IP? " GIP
echo
read -p "Lastly the DNS addresses? " DNS
read -p "Which interface to configure? " IntID
echo
echo "Configuring..."
nmcli con mod $ID ipv4.addresses $IP/$PL
nmcli con mod $ID ipv4.gateway $GIP
nmcli con mod $ID ipv4.dns "$DNS"
nmcli con mod $ID ipv4.method manual
echo "Restarting interface..."
nmcli con down $ID > /dev/null
nmcli con up $ID > /dev/null
echo "Done!"

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

#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
}

function setdynamic() {
echo "Configuring..."
nmcli con mod $IntID ipv4.method auto

apply
}

function apply() {

#Applying the changes
echo "Applying changes..."
nmcli con down $IntID > /dev/null
nmcli con up $IntID > /dev/null
echo "Done!"
}

if [[ $1 == "static" ]]; then
setstatic
fi

if [[ $1 == "dynamic" ]] || [[ $1 == "auto" ]] || [[ $1 == "automatic" ]] || [[ $1 == "dhcp" ]]; then
setdynamic
fi

if [[ $1 == "" ]]; then
read -p "Static or dynamic? " CHOICE
if [[ $CHOICE == "static" ]]; then
setstatic
fi
if [[ $CHOICE == "dynamic" ]]; then
setdynamic
fi
fi

exit

Loading…
Cancel
Save