0

I wrote a bash script that get a password from an API and use "send" to set this password

un=admin
pw=$(get_json_value_from_file "$rsm_res_file" "password")
echo $pw
export HISTIGNORE="expect*";
expect -c "
    spawn ssh -o UserKnownHostsFile=$ap_known_hosts_file -p $PORT  $un@localhost
    expect "*ogin*"
    send \"$un\r\"
    expect "*assword*"
    send \"$pw\r\"
    interact"

and it retuned the folowing exception:

  missing close-bracket
while executing "send \"f40T[2[6g%^TsMLv\r\"
    interact"

I think the problem with the special characters.

pynexj
  • 19,215
  • 5
  • 38
  • 56
Daoud Shaheen
  • 364
  • 4
  • 16
  • Is this not an escaping issue, there are nested double quotes not being escaped by the look of it? – asimovwasright Oct 12 '16 at 09:20
  • 1
    You can see by the syntax highlighter that the expect statements are outside of quotes. – 123 Oct 12 '16 at 09:26
  • It could be easier to run the expect as a separate script, sending it arguments as needed: http://stackoverflow.com/questions/15422757/how-to-pass-variables-from-shell-script-to-expect-script – asimovwasright Oct 12 '16 at 09:27
  • When possible (and it usually is), it is much simpler to set up public key authentication than to use `expect` to send a password to `ssh`. – chepner Oct 12 '16 at 11:44

1 Answers1

1

Your password includes a [ char which has special meaning. The [...] syntax in Tcl/Expect is command substitutin just like `...` or $(...) in Bash. To get a literal [ you have to use \[. For your scenario the easiest solution is to pass the password from Bash to Expect by using an env var. E.g.:

PASSWORD=$pw expect -c "
    spawn ssh -o UserKnownHostsFile=$ap_known_hosts_file -p $PORT  $un@localhost
    expect "*ogin*"
    send \"$un\r\"
    expect "*assword*"
    send \$env(PASSWORD)\r
    interact"

or

export PASSWORD=$pw
expect -c "
    spawn ssh -o UserKnownHostsFile=$ap_known_hosts_file -p $PORT  $un@localhost
    expect "*ogin*"
    send \"$un\r\"
    expect "*assword*"
    send \$env(PASSWORD)\r
    interact"
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • 1
    Now that we're passing the password through the environment, we can do the same with the other variables, and then we can use single quotes to surround the expect script body and stop escaping everything. Particularly, the lack of quotes for the expect commands look problematic. – glenn jackman Oct 12 '16 at 13:35