Month: May 2011

Not quite a multidimensional array (hack)

So the following doesn’t do anything except illustrate how I worked around BASH not support multidimensional arrays.

In my production code (not the below example) I used the below loop to call an expect script. The expect script does the work of logging into the router(s). The output from expect is then piped to AWK, which handles the rest of the formatting.

I’m using a cron job to call up a bash script, which calls up an expect script, which hands the output data to AWK, which pipes it to a file on disk.

bash –> expect –> awk –> stout

#!/bin/bash
#
# Not quite a multidimensional array
#
A=("192.168.0.3:::Router-1" "192.168.0.4:::Router-2")

echo '--------------------'
for ROW in ${A[*]}
do
 echo "processing row: $ROW"
 echo "HostAddress: ${ROW%:::*}"
 echo "Host Prompt: ${ROW#*:::}"
 echo
done

(Edited 1/5/2012)
The purpose for needing to associate a host name along with the IP address is to due to Expect.  When Expect logs into a device it is looking to match a prompt.  Not every device will have the exact same prompt so including a name (string) with the IP address helps in allowing a general/generic Expect script for all devices.