I have an array, $HOSTS
, which contains a set of hostnames.
I have a database table that has a set of IP addresses. I want to go through each IP address in that table and send a command (hostname -s
) to that address, and add the output to $HOSTS
. The remote servers have no PKI configured and the script must run automatically without human interaction. It's a closed network and the root password is stored as variables in the script ($SERVERPASSWORD
). And before anyone says it I realize it's not a good idea to have the root password in a variable like this, but in this case it's more important for there to not be any human interaction than for the root password to be protected.
Here's the relevant part of my current (sanitized) script:
for IP in $(sql query that returns a list of IP addresses);do
HOSTS=($(expect -c 'spawn ssh root@$IP "hostname -s"; expect "assword:";send ""$SERVERPASSWORD"\r";interact' ${HOSTS[@]}))
done
The result of this is a failure, saying:
Can't read "IP": no such variable while executing "spawn ssh root@$IP "hostname -s""
I can put an echo $IP
in place of that and it works as expected, so I know the IP variable is being filled in the loop.
After some Googling, it seems from what I understand that expect
needs to have its own variable set and can't use Bash variables, but all the solutions I found were for actual Expect scripts, and not for using expect within a Bash script. I tried modifying my command to be expect set IP $IP -c 'spawn ....'
but that didn't seem to take.
What can I do to make expect
read the value of $IP
in this For loop?
$IP
is single-quoted, so the shell does not expand it. So is$SERVERPASSWORD
, I think. Can you take it from here? I don't want to touch the code because I don't get the point ofHOSTS=
orinteract
("it's more important for there to not be any human interaction" +interact
= ???). Can't you usesshpass
?