2

I am attempting to send a password to a remote server using a bash script. This worked fine until we were required to change the passwords to more complex passwords and one of the new passwords included a $ character (i.e. P@$sw0rd).

I have included the snippet that is no longer working:

password="'P@$sw0rd'"

spawn ssh "$deviceType@$device"
expect {
  "*no)?" {send "yes\r"}
  "*assword:" {send "$password\r"}
  "*assword:" {send "$password\r"}
}

Every time it sends the password, the remote system attempts to find the variable $sw0rd.

I know the problem is with the $ character in the password itself, but is there a way to do this without changing this one password on all of our remote servers to one that does not include the $ character?

I have tried different ways to store the $ character:

password='P@$sw0rd'
password=P@$sw0rd
password="'P@\$sw0rd'"
password="P@\$sw0rd"
password='P@\$sw0rd'
password=P@\$sw0rd

None of these have worked.

Please, help.

codeforester
  • 39,467
  • 16
  • 112
  • 140
PCnetMD
  • 167
  • 4
  • Sounds like it might be a problem of the **remote system** – Stefan Hegny Aug 02 '17 at 19:32
  • 1
    Can you include more of the code? If your code is actually `password="whatever"; expect << eof; spawn ssh "$deviceType@$device"; ...; eof` then that makes a huge difference. Currently the code appears to be partially Bash and partially TCL, and therefore not runnable or representative of the problem. – that other guy Aug 02 '17 at 20:29

2 Answers2

4

As you use spawn command there is a chance that you actually have expect script and not bash script. This should work in expect:

set password P@\$sw0rd

Of course you also have to use expect to run that script, either by adding #!/usr/bin/env expect at the top or using expect directly in the terminal: expect <SCRIPT>

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
0

@ArkadiuszDrabczyk has your answer.

You need to use exp_continue to get your login to work though:

spawn ssh "$deviceType@$device"
expect {
  "*no)?" {send "yes\r"; exp_continue}
  "*assword:" {send "$password\r"; exp_continue}
  "*your prompt pattern here"
}
# ... carry on with the script

exp_continue allows you to stay in the current expect command and continue to look for other patterns. You do need something else to expect to allow you to break out.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352