How to send password with $ character in Expect Script

I expect the script that I need to log in to the remote system and execute the commands. This script works except for providing a root account password. The root password contains a dollar sign, which I can't seem to work. Here is the code

#!/usr/bin/expect set timeout 3 set username "root" set password "Pas$word" set hostname [lindex $argv 0] log_user 0 send_user "\n#####\n# $hostname\n#####\n" spawn ssh -q -o StrictHostKeyChecking=no $username@ $hostname expect { timeout { send_user "\nFailed to get password prompt\n"; exit 1 } eof { send_user "\nSSH failure for $hostname\n"; exit 1 } "*assword" } send "$password\r" expect { timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1} "*\$ " } send_user "\nPassword is correct\n" expect "$ " { send "ls" } 

I confirmed that this works when providing credentials whose passwords do not contain a dollar sign, but I cannot get it to work with the root account. It always throws a Login failed. Password incorrect timeout error Login failed. Password incorrect Login failed. Password incorrect . Changing a password is not an option. I tried to provide the escape character \ in the password definition like this:

 set password "Pas\$word" 

And I get the same results ... any ideas on what I am doing wrong?

thanks

EDIT Like I said. I already tried to escape the $ character. But for clarification, I added a print statement for the password when the script runs to check that the variable contains the password correctly ... Here is the change:

 set password "Pas\$word" ... send_user "\n#####\n# $hostname\n#####\n" send_user "Using password: $password\n" ... 

Here is the console output:

 njozwiak@ubuntu :~$ ./ssh_ls.sh 192.168.5.93 ##### # 192.168.5.93 ##### Using password: Pas$word Login failed. Password incorrect. 
+6
source share
8 answers

I was not able to get this job, just avoiding the dollar sign. I got lucky using \ x to specify the hexadecimal value for the dollar sign.

set password "Pas \ x24word"

+3
source

Try performing a backslash, i.e.

 Pas$word 

becomes

 Pas\\\$word 

This is a common scripting problem (even with PHP, Python), where a line gets an uninsulated file twice or more!

A full-blown example of how I use expect with SFTP inside a Bash script:

 #!/bin/bash host=mysftp.dmz port=22 username=john /usr/bin/expect <<EOF set timeout -1 spawn sftp -C -oPort=$port $username@ $host expect "password:" send "Pas\\\$word\r" expect "sftp>" send "get data.txt\r" expect "sftp>" send "bye\r" EOF 
+2
source

I found a workaround for this. I am sure this problem has been solved long ago, just mentioning how I solved mine:

Actual password:
Pas$word

Assign a password to a variable:
set password {Pas\$word}

In TCL (or expect in the code), grouping words in double brackets {} disables the substitution in curly brackets, so your password will be saved as: Pas\$word
Now, at the end, when you send the password through the wait, Pas\$word will be translated as Pas$word , which is the actual password.

But the problem is that the password is unknown, for example, the password is in an encrypted file or should be accepted as user input. I was looking for this type of case where I do not know where and how much $ sings in the password.

+2
source

I found this question because I had the same problem, I know this is an old question, but for those who are still looking for a quick solution:

 set CorrectedPassword [ string map -nocase { "\$" "\\\$" } $MyPassword ] 

Then $ CorrectedPassword is fixed using the login procedure. -nocase is not required, but I find it safer to include by default.

0
source

I had a similar problem when I wanted to pass the filename with "$" while waiting for the script and after working for me

 filePath=$(echo -n $filePath | sed -e 's/\$/\\\$/g') 
0
source

I used below and it worked for me:

send \"Pas\\\$word\"

0
source

This will definitely help -

 set username root set password Pas\$word 

Get rid of quotes.

Hope this helps.

-1
source

I recently had a similar problem. This worked for me:

$ PASS = "pa \ $ \ $ word"; somecommand --user uname --password $ PASS

Note the double quotes and semicolon.

-1
source

Source: https://habr.com/ru/post/950555/


All Articles