Using EXPECT script with a $ in the password

0

I'm trying to set a variable in a Linux bash script which includes a $ symbol, i.e. password is Gl@d1us$123 so PASS="Gl@d1us\$123". I can preceed the $ with a \ and it works fine, but when it calls the EXPECT script the $ symbol causes it to fail. I can enter the password directly into the EXPECT script using the \ as before and it works fine, so it's just the process of passing the password variable between scripts that is causing me grief.

Main script:

echo  
echo  
USER=kendalla  
PASS="Gl@d1us\$123"  
DIR=/home/kendalla/scripts/Audit  

./get_hardware.expect $HOST $USER $PASS > $DIR/$HOST

get_hardware.expect script:

#!/usr/bin/expect -f
log_user 1  
log_file  /home/kendalla/scripts/Audit/HOST  
set HOST [lrange $argv 0 0]  
set USER [lrange $argv 1 1]  
set PASS [lrange $argv 2 2]  
spawn ssh -q -l $USER $HOST  
expect "$USER@rebehc01's password:"  
send "Gl@d1us\$123\r"    ;#(This works)  
send $PASS               ;#(This doesn't work)  

All help appreciated.

Thanks

Andy.

Andy Kendall

Posted 2014-05-14T11:39:09.567

Reputation: 1

1Show some code. It can be readily solved. – glenn jackman – 2014-05-14T16:42:03.197

Code appended as requested. – Andy Kendall – 2014-05-15T07:57:06.113

Answers

0

This is the problem:

./get_hardware.expect $HOST $USER $PASS > $DIR/$HOST .

In shell scripting you always want to quote your variables, unless you understand precisely when to not quote them.

./get_hardware.expect "$HOST" "$USER" "$PASS" > "$DIR/$HOST" .

glenn jackman

Posted 2014-05-14T11:39:09.567

Reputation: 18 546

It still doesn't work. ./get_hardware.expect "$HOST" "$USER" "$PASS" > "$DIR/$HOST" and expect "$USER@$HOST's password:" send "$PASS\r" expect ">" – Andy Kendall – 2014-05-15T13:52:00.863

print out the value of $PASS in the expect script. What is it there? – glenn jackman – 2014-05-15T16:22:52.060

I changed the expect script lrange to lindex and it works fine as I originally wrote it. thanks for your help anyway. – Andy Kendall – 2014-05-20T10:30:02.003