Bash scripts examples : Add persistent route from command line

Introduction

Here is a new chapter of the “Bash script examples” series : this one is a script that allow one to add a persistent route to a Red Hat based distribution from command line.

Under a Red Hat Linux you are supposed to edit a /etc/sysconfig/network-scripts/route-eth* file, which is quiet painful !

 

More “bash scripts examples” posts

 

 

1 Description

This script is basically taking the arguments you gave and format + append them to a route-eth* file.

If no route-eth* is given as argument then the defaultĀ route-eth0 (defined in the $default variable) file is used.

Once you have this script you just need to put it somewhere in your $PATH to make it available from command line. I personally added it to my /bin directory.

Note : If called without any arguments the usage is printed.

1.1 Limitations

This script requires your route_eth* files to be well construct, by this i mean that they need to have the first route at the top of the file (not at the bottom), as :

GATEWAY0=10.10.0.1
NETMASK0=255.0.0.0
ADDRESS0=10.0.0.0
GATEWAY1=192.168.10.10
NETMASK1=255.255.255.0
ADDRESS1=192.168.33.0

 

 

2 The code

#!/bin/bash

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Company name - Division name - Project name
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# HallOfTips - 15/09/2012 - routeAdd - 1.0
# ---------------------------------
# This script add persistent route to Red Hat based distros.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#####################################
##### VARIABLES
#####################################

addr=$1
gw=$2
nm=$3
route_file=$4
default="/etc/sysconfig/network-scripts/route-eth0"
E_ARGERROR="3"

#####################################
##### USAGE
#####################################

# usage & examples
Usage() {
  cat <<- EOF
    Usage: $(basename $0) [destination] [gateway] [netmask] [route_file]

    Where:
    [destination] : must be either a network or a host address
    [gateway] : is the gateway (!)
    [netmask] : must be given as a "dotted decimal"
    [route_file] : optional, if not given default ($default) is used, if given (using absolute path) target file must be writeable.

    Examples:
    - Add a route to the 192.168.33.0 network which network mask is 255.255.255.0 and which is reachable through 192.168.12.254
    $(basename $0) 192.168.33.0 192.168.12.254 255.255.255.0

EOF
  exit $E_ARGERROR
}

#####################################
##### FUNCTIONS
#####################################

FindGreater() {
  local numbers=( "${@}" )              # Initialize the array with given arguments
  local first="${numbers[0]}"           # Get the first elem from array
  local lenght="${#numbers[@]}"         # Get the number of elem in array
  local num="0"
  while (( num < "${lenght}" )) ; do

    if [[ "${numbers[$num]}" -ge "${greater:-0}" ]] ; then
      local greater="${numbers[$num]}"
    fi

    ((num++))
  done
  echo "${greater}"
}

IndexArray() {
 while IFS="=" read string _ ; do
   echo $string |
   grep -Eo '[[:digit:]]+'
 done < route_eth0 | uniq
}

AddToRouteFile() {

 cat >> ${route_file:=${default}} <<-EOF
        GATEWAY${next_route_index}=${gw}
        NETMASK${next_route_index}=${nm}
        ADDRESS${next_route_index}=${addr}
        EOF

}
#####################################
##### MAIN
#####################################

if [[ "$#" -lt "3" ]] ; then
  echo -e "Error $0 : missing parameters\n" && Usage
fi

if [[ ! -w "${route_file:=${default}}" ]] ; then
  echo -e "Error ${route_file:=${default}} : permission denied\n" && Usage
fi

index_array="$(IndexArray)"

next_route_index=$(( $(FindGreater ${index_array[@]}+1) ))

AddToRouteFile && exit "0"

 

 

3 Example

The following example was applied to a fake route-eth0 file.

[pier@machine test_dir]$ more route_eth0   # Here is the original route file
GATEWAY0=10.10.0.1
NETMASK0=255.0.0.0
ADDRESS0=10.0.0.0
[pier@machine test_dir]$ routeAdd 192.168.99.0 192.168.55.33 255.255.255.0 route_eth0  # I use the script to add the route
[pier@machine test_dir]$ more route_eth0   # Here is the modified route file
GATEWAY0=10.10.0.1
NETMASK0=255.0.0.0
ADDRESS0=10.0.0.0
GATEWAY1=192.168.55.33
NETMASK1=255.255.255.0
ADDRESS1=192.168.99.0
[pier@machine test_dir]$

Leave a Reply

Your email address will not be published. Required fields are marked *

This site supports SyntaxHighlighter via WP SyntaxHighlighter. It can highlight your code.
How to highlight your code: Paste your code in the comment form, select it and then click the language link button below. This will wrap your code in a <pre> tag and format it when submitted.