Qiang Xu
Qiang Xu

Reputation: 4803

expect adding curly brackets to password containing special characters

I wanted to write up a small expect script and another bash script to save the effort of typing password in ssh connection.

Here goes the scripts:

// ssh.exp, the real workhorse
#!/usr/bin/expect -f
# usage: ./ssh.exp host user pass

set host [lrange $argv 0 0]
set user [lrange $argv 1 1]
set pass [lrange $argv 2 2]

spawn ssh $user@$host
match_max 100000
expect "*?assword:*"
send -- "$pass\r"
send -- "\r"
interact

// the bash script to call it
#!/bin/bash

host='my.host.com'
user='someuser'
pass='Qwerty389$'

./ssh.exp $host $user $pass

However, when the test scripts run, the ssh server always complains that the password is incorrect.

I tried escaping the dollar sign, like pass='Qwerty389\$', but to no avail.

Put the debug statement exp_internal 1 into the expect script, and it shows that the password sent is:

send: sending "{Qwerty389$}\r" to { exp6 }  // without escaping $
send: sending "{Qwerty389\$}\r" to { exp6 } // escaping $ in password

Not sure why expect put the curly brackets around the password passed to it. I verified that if there is no dollar sign in the password, there would not be the brackets.

Any help?

Upvotes: 0

Views: 1881

Answers (1)

glenn jackman
glenn jackman

Reputation: 246799

The shell code needs to quote variables:

./ssh.exp "$host" "$user" "$pass"

The expect code should not treat lists like plain strings. Extract the arguments with

lassign $argv host user pass

or if your expect is too old to have lassign, do

foreach {host user pass} $argv break

or (less DRY)

set host [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]

Upvotes: 2

Related Questions