I have a simple bash script with an associative array called servers. The server name is the key and the IP address is the value. I use this when I want to run some one off script on all my servers.
for server in ${!servers[@]}; do
echo ""
echo -e "\e[93mConnecting to $server...\e[0m"
ssh $user@${servers[$server]} 'sudo /bin/bash -s' < $1 | while read line; do echo -e "\e[92m[$server]\e[0m $line"; done
done
This works fine when I run it interactively and read from stdout but now I would like to run it automatically from cron and have the script that runs on the server conditionally send data to hipchat using curl.
Unfortunately many of the servers do not have meaningful hostnames set and I cant easily change the hostnames since there are decades old php websites on some of them that use the hostname to determine what environment they are running in and set db credentials ect... I do not want to audit dozens or even hundred+ websites for this.
Can I send the value of $server to the spawned bash shell on the server as an environment variable the script can use when sending messages to hipchat or email? I tried just doing SERVER=$server before /bin/bash but that did not seem to work.