Search code examples
sshexpectsshpass

sshpass doesn't work with SSH ProxyCommand option


I am trying to automate SSH login for a SSH tunnel using a proxy:

  1. I do not want to use the ssh-copy-id solution
  2. sshpass works properly when I set the ssh tunnel without ProxyCommand option but it doesn't work with the option set (Write failed. Broken pipe). The ssh tunnel itself works good with that option, asking me for password. Also tried setting the option in ~/.ssh/ssh_config with no solution. Here is the oneliner:

    sshpass -p $mypass ssh -fN -o StrictHostKeyChecking=no \
      -o ProxyCommand="nc -x localhost:8888 %h %p" -R \
      *:$rport:$localhostname:$nport $username@$hostname
    
  3. I tried an Expect script as described here with no success. I am new to expect and could not figure out the correct quote escapation because I couldn't find a complex spawn example. Here is what I tried with no luck (and some other variations of quotes):

    spawn ssh -fN -o StrictHostKeyChecking=no \
      "-o ProxyCommand=\"nc -x localhost:8888 %h %p\"" \
      -R *:$rport:$localhostname:$nport $username@$hostname
    

Can somenone help me? Thank you.


Solution

  • To use expect:

    spawn ssh -fN -o StrictHostKeyChecking=no \
        -o "ProxyCommand=nc -x localhost:8888 %h %p" \
        -R *:$rport:$localhostname:$nport \
        $username@$hostname