0

I am trying to allow a user to input the number of ip addresses that they want to interface with, then enter each ip address and have it assigned to a variable. The script will ultimately write and execute a second script. The reason for the second script is that I am ssh-ing into an AP in order to cluster x number of AP's together, and once SSH occurs, bash/python variables are no longer passed through(the AP has it's own language), so they must be translated to plain text before the ssh script is run. The code below functions but allows only 2 ip addresses(I couldn't figure out how to use the $cvar to create multiple variables), and does not allow me to decide how many ip addresses to enter:

#!/bin/bash
echo -e "How many AP's do you want to Cluster?:"
#this is the variable to define how many ips to ask for
read cvar 

echo -e "Enter last 2 digits of AP #1:"
read ip1
echo -e "Enter last 2 digits of AP #2:"
read ip2
#I want this to continue for x number of ip addresses(defined by $cvar)

echo -e "Enter a name for your Cluster:"
read cname
#code below is executed within the AP terminal(commands are unique to that shell)
    echo "#!/bin/bash
ssh -T admin@192.168.0.$ip1 <<'eof'
configure
cluster
add $cname
add-member 192.168.0.$ip1 user ***** password ********
save
add-member 192.168.0.$ip2 user ***** password ********
save
exit
operate $cname
save
exit
" > ./2ndScript.sh
    chmod a+x ./2ndScript.sh
    /bin/bash ./2ndScript.sh
  • You need an [array](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html) for this. – Azeem Jul 23 '17 at 04:44
  • 2
    You've got two answers suggesting that you use an array, which should be enough to let you improve your script yourself, so I won't add another. Instead, I'll refer you to the bash function at the bottom of [this answer](https://stackoverflow.com/a/13778973/1072112) to help with your IP address validation. :-) – ghoti Jul 23 '17 at 05:05

2 Answers2

0

Without rewriting the entire script, here is a snippet.

#!/bin/bash

# IP is an array
IP=()

# Read number of IP Addresses to be read in
read -p "How many AP's do you want to Cluster?: " cvar

loop=1
while [ $loop -le $cvar ]
do
    read -p "Enter last 2 digits of AP #${loop}: " IP[$loop]
    loop=$((loop+1))
done
Deathgrip
  • 398
  • 4
  • 10
0

Arrays are your friend here. Take the following;

echo -e "Enter last 2 digits of AP #1:"
read ip1
echo -e "Enter last 2 digits of AP #2:"
read ip2
#I want this to continue for x number of ip addresses(defined by $cvar)

We can make a for loop, and then add an element to an array for each address. In this for loop, $i will tell us which cycle we're on, starting with 0. Since it auto-increments, we can just use it to specify what index of the array to update.

for (( i=0; i<$cvar; i++ )); do
    echo -e "Enter last 2 digits of AP #$((i+1)):"
    read #With no arguments, read assigns the output to $REPLY
    #optional; this allows the user to enter "done" to end prematurely
    #if [[ $REPLY == "done" ]]; then break; fi
    ip[$i]=$REPLY #ip is the name of the array, and $i points to the index

done

If you use that optional code snipit, you don't even have to ask how many addresses the user wants. You could instead replace the for loop with while true; do, and just instruct the user to enter "done" (or any other exit command) to end address collection (though you'll need to define i=0 somewhere and then increment it at the end of the loop if you swap to while).

Now you have a list of values ordered from ${ip[0]} to ${ip[n]} of all the address that the user entered. You can extract them using another for loop later;

for ((i=0;i<${#ip[*]};i++)); do
    #Insert code here. For example:
    #echo "${ip[$i]} #echos the currently selected value of the array
    #echo "${ip[$i]}" >> file.txt #appends file.txt with the current value
done
Deathgrip
  • 398
  • 4
  • 10
Guest
  • 124
  • 7
  • Thanks for the great answer. I still have the issue of injecting them into the second script. The variables collected need to be written to specific places in the ssh script(./2ndScript.sh). How do I write an "add-member..." line for each ip[$i] entered by the user? – Brock Taylor Jul 23 '17 at 05:47
  • @Brock You could use a loop such as I've shown in my second `for loop` example. I'm making an edit with more information, but I'm confused about this line; `ssh -T admin@192.168.0.$ip1 <<'eof'`. You have the first IP listed here. Does this whole section need to be done for each IP, or is the first IP also assumed to be the connection for the second script? – Guest Jul 23 '17 at 06:42
  • 1
    It took some sloppy work, but I figured it out: for ((i=0;i<${#ip[*]};i++)); do echo "add-member 192.168.0.${ip[$i]} user ***** password ******** save" >> tempfile.txt . THEN: cat tempfile.txt >> ./2ndScript.sh rm tempfile.txt – Brock Taylor Jul 23 '17 at 08:37
  • the ssh line is used to interface with the first AP. the $ip1 is the ip address to 'dial'. the <<'eof' generates a heredoc that just passes text to the command line of the AP. All of the other ip addresses have to be contained in that heredoc. Once the ssh has started, no bash commands or variables can be passed, so they have to be written before the ssh session is opened(which is why I had to use the 1st script to generate and run a 2nd one). – Brock Taylor Jul 23 '17 at 08:50