Ram
Ram

Reputation: 1193

escaping hyphen and quotes in expect script

Hello I am using expect to automate a login task. but the script fails if password starts with a hyphen How can I escape that

I have a lot of trouble properly escaping ' " or other characters Is there a way I can encode all my characters and send expect the encoded string for it to decode before sending

This is my script

#!/usr/bin/expect -f
spawn ssh -o "PubkeyAuthentication no" -l user 10.10.10.10
expect "password: "
send "-cpass\'ok\r"
expect "$ "

Upvotes: 4

Views: 3568

Answers (2)

gregory
gregory

Reputation: 12915

From the man page:

The -- flag forces the next argument to be interpreted as a string rather than a flag. Any string can be preceded by "--" whether or not it actually looks like a flag. This provides a reliable mechanism to specify variable strings without being tripped up by those that accidentally look like flags. (All strings starting with "-" are reserved for future options.)

Which means you should write your send something like:

send -- "-cpass\'ok\r"

Note: you'll have problems trying to pass any string starting with a hyphen; this won't work: send "-cpass", if your string has a hyphen you need to use -- flag.

Upvotes: 7

daniel.971
daniel.971

Reputation: 64

You can try to save your password in a variable:

password=-cpass\'ok\r
send "$password"

You simply write a \ in front of every '," or spaces.

Upvotes: -2

Related Questions