Thursday, February 04, 2016

BASH Script to Manipulate IP Addresses and Port Numbers

This post is just a place to store some bash script for reference. I don't work in bash often enough to remember how things work so I need to make little snippets and such for reference. I do the same thing for regex and sql.

Here are some of the things I do to manipulate ip addresses in bash scripts. Since I work a lot with Solr, specifying zookeeper machines and lists of servers for SolrCloud I am always manipulating IP addresses.

#!/bin/bash
#set -x
echo -----------------BEGIN------------------------
servers="10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080"
declare -a serversArray=("10.1.1.1:8080" "10.1.1.2:8080" "10.1.1.3:8080")

echo servers:"      "${servers}
echo serversArray:" "${serversArray[@]}

echo ----------------------------------------------

#use for loop to convert the serversArray to a comma seperated string
#Since arrays can be sparse, that is their indices do not have to be sequential
#get the indices as an array and iterate over the indices.
result1=
arrayIndices=(${!serversArray[*]})
for index in ${arrayIndices[*]}
{
   result1=${result1}${serversArray[$index]}
   if [ $index != ${arrayIndices[*]: -1} ]   #This gets the last element of an array
   then
      result1=$result1, #only append comma if this isn't the last entry
   fi
}
echo Array to comma seperated string using for loop:" "${result1}

#convert array to comma delimited string using character substitution
cds=${serversArray[@]};
cds=${cds// /,}
echo Array to comma seperated string using character substitution:"   "${cds}

#convert the comma delimited string into an array
atemp=($(IFS=','; x=($servers); echo ${x[@]}))
echo Comma delimited string to array:"   "${atemp[@]}
for a in "${atemp[@]}"
{
   echo []"${a}"
}
echo ----------------------------------------------

#remove the port from the servers, only works for port numbers of 4 digits
echo Remove Port Numbers from IP Addresses
servers2=${servers//:????/}
echo ${servers2}

#now use sed to remove port numbers from servers
echo ${servers} | sed -e "s/:[0-9]*//g"

echo ----------------------------------------------
#create an ip address with incrementing port numbers
echo Creat ip addresses with incrementing port numbers
echo 10.1.1.0:{10001,10002,10003,10004}
echo 10.2.2.2:{10001..10004}
echo 10.1.1.0:{10001,10002,10003,10004},
#capture echo into var and strip the trailing comma
ips=$(echo 10.1.1.0:{10001,10002,10003,10004},)
ips=${ips/%,/}
echo ${ips}







Here is the output:
-----------------BEGIN------------------------
servers:      10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080
serversArray: 10.1.1.1:8080 10.1.1.2:8080 10.1.1.3:8080
----------------------------------------------
Array to comma seperated string using for loop: 10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080
Array to comma seperated string using character substitution:   10.1.1.1:8080,10.1.1.2:8080,10.1.1.3:8080
Comma delimited string to array:   10.1.1.1:8080 10.1.1.2:8080 10.1.1.3:8080
[]10.1.1.1:8080
[]10.1.1.2:8080
[]10.1.1.3:8080
----------------------------------------------
Remove Port Numbers from IP Addresses
10.1.1.1,10.1.1.2,10.1.1.3
10.1.1.1,10.1.1.2,10.1.1.3
----------------------------------------------
Creat ip addresses with incrementing port numbers
10.1.1.0:10001 10.1.1.0:10002 10.1.1.0:10003 10.1.1.0:10004
10.2.2.2:10001 10.2.2.2:10002 10.2.2.2:10003 10.2.2.2:10004
10.1.1.0:10001, 10.1.1.0:10002, 10.1.1.0:10003, 10.1.1.0:10004,
10.1.1.0:10001, 10.1.1.0:10002, 10.1.1.0:10003, 10.1.1.0:10004

No comments: