0

Ubuntu 16.04 GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

We have to add ips to servers a few times a day. I find myself creating different variations of the same file according to the ip block.

#-- 100.100.100.160/29 - Sun 04/22/18 02-38-pm
#-- -----------------------------------------
auto eth0:0
iface eth0:0 inet static
    address 100.100.100.160
    netmask 255.255.255.255

auto eth0:1
iface eth0:1 inet static
    address 100.100.100.161
    netmask 255.255.255.255

auto eth0:2
iface eth0:2 inet static
    address 100.100.100.162
    netmask 255.255.255.255

auto eth0:3
iface eth0:3 inet static
    address 100.100.100.163
    netmask 255.255.255.255

auto eth0:4
iface eth0:4 inet static
    address 100.100.100.164
    netmask 255.255.255.255

auto eth0:5
iface eth0:5 inet static
    address 100.100.100.165
    netmask 255.255.255.255

auto eth0:6
iface eth0:6 inet static
    address 100.100.100.166
    netmask 255.255.255.255

auto eth0:7
iface eth0:7 inet static
    address 99.100.100.167
    netmask 255.255.255.255

So I figured to create a script to get me as close to that file as possible so I wrote a script and here is a portion of it ...

#!/bin/bash
#
#-- bash add_ips.sh "eth0" "100.100.100.160" "255.255.255.255" "29"

wDir="/scripts/tools/ips"
ifaceFile="/etc/network/interfaces"
ipFile="${wDir}/ipFile.txt"
nic="${1}"
address="${2}"
netmask="${3}"
block="${4}"
timeStamp="$(date '+%a %D %I-%M-%P')"

if [ ! -n "$4" ]; then
   echo 'add_ips.sh nic address mask block ...';
   exit 1;
fi

#-- echo the address block on a header line
{
   echo "";
   echo "#-- IP Block ${address}/${block} - ${timeStamp}";
   echo "#-- ----------------------------------------------";
} > "$ipFile"

#-- If a 30/block
if [[ "$block" == "30" ]]; then
   start="0"
   end="3"
   for ipnum in $(seq "$start" "$end"); do
      {
         echo "auto ${nic}:${ipnum}";
         echo "iface ${nic}:${ipnum} inet static";
         echo -e "\t address ${address}";
         echo -e "\t netmask ${netmask}";
         echo "";
      } >> "$ipFile"
   done

The output was close to what I was looking for and the only thing left to do was change the last number of the ip address.

#-- 100.100.100.160/29 - Sun 04/22/18 02-38-pm
#-- -----------------------------------------
auto eth0:0
iface eth0:0 inet static
    address 100.100.100.160
    netmask 255.255.255.255

auto eth0:1
iface eth0:1 inet static
    address 100.100.100.160
    netmask 255.255.255.255

auto eth0:2
iface eth0:2 inet static
    address 100.100.100.160
    netmask 255.255.255.255

How can I change the last octet of the ip address while adding the $seq value of the present loop on that line?

I tried this

#-- If a 30/block
if [[ "$block" == "30" ]]; then
   start="0"
   end="3"
   for ipnum in $(seq "$start" "$end"); do
      ipaddress=$(expr "$address" + "$ipnum")
      {
         echo "auto ${nic}:${ipnum}";
         echo "iface ${nic}:${ipnum} inet static";
         echo -e "\t address ${ipaddress}";
         echo -e "\t netmask ${netmask}";
         echo "";
      } >> "$ipFile"
   done
fi

The address is not an integer so it fails.

2

I think the smallest change to make it work would be:

ipaddress=${address%.*}.$((${address##*.} + ipnum))

This sets ipaddress to:

  • the address stripped of the trailing dot followed by anything (the last octet)
  • a dot
  • the mathematical expression $(( ... )) that adds:
    • the first octets of address (stripping as many characters as possible through the last dot it finds)
    • ipnum
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.