I am currently trying to simplify a script. Instead of manually entering a host name for each command, I’d like to be able to use variables? arrays? of some sort. Was wondering if you can help.
Example:
hostname1=192.168.1.1
hostname2=192.168.1.2
echo "What would you like to do?"
echo "(1)Ping Hosts"
echo "(2)mkdir /tmp/test"
echo "(3)write to test.txt"
read num_variable
if num_variable = 1
then
ping hostname1
ping hostname2
if num_variable = 2
ssh -n hostname1 "mkdir /tmp/test"
ssh -n hostname2 "mkdir /tmp/test"
if num_variable = 3
ssh -n hostname1 "echo 'hello' > /tmp/test/test.txt"
ssh -n hostname2 "echo 'hello' > /tmp/test/test.txt"
“Is there an easier way to write this?”
Example:
Hosts=hostname1,hostname2
Commands here?
I’d hate to have 20,000 lines when all i could do is list the hostnames on top and for each num variable execute all of the commands below for the hosts listed in that particular section.
Create an array:
You can split it to multiple lines:
Iterate over the elements:
Access a particular host:
Use the
selectcommand to create a menu: