I have a bash script which read an input file and using a heredoc to ssh to a server using expect, but am having issue escaping some special character in the input file. Here is what I have..
I have a file call input.txt which contain:
1.2.3.4:abcdefg
2.3.4.5:abc$def
I have a bash script which look like this. It works fine if the password doesn't contain the character '$' but blow up when the password contain '$' as it treat the part after $ as a variable.
#!/bin/bash
if [ -e "input.txt" ]; then
while read i; do
/usr/bin/expect <(cat << EOD
set timeout 15
spawn ssh "user@$(echo $i | cut -d: -f 1)"
#######################
expect "yes/no" {
send "yes\r"
expect "Password:" { send "$(echo $i | cut -d: -f 2)\r" }
} "Password:" { send "$(echo $i | cut -d: -f 2)\r" }
expect -re "day: $" { send "\r" }
expect ":" { send "\r" }
expect -re "# $" { send "date\r" }
expect -re "# $" { send "exit\r" }
EOD
)
done < input.txt
fi
I got the following error when I run this and hit the second set of IP.
spawn ssh [email protected]
Unauthorized use of this system is prohibited and may be prosecuted
to the fullest extent of the law. By using this system, you implicitly
agree to monitoring by system management and law enforcement authorities.
If you do not agree with these terms, DISCONNECT NOW.
Password: can't read "def": no such variable
while executing
"send "abc$def\r" "
invoked from within
"expect "yes/no" {
send "yes\r"
expect "Password:" { send "abc$def\r" }
} "Password:" { send "abc$def\r" }"
(file "/dev/fd/63" line 5)
Anyone have any idea? I tried single quote inside double quote and everything I could think of but still can't get it to work. Thanks in advance