I found a command to directly scp my file to a host (C) via another host (B) from my computer. The command is in this form:

scp -oProxyCommand="ssh -W %h:%p B" thefile C:destination

I used it and it works good in command line. but when I want to write an expect script using this command, I got some errors. the script I've written is in this form:


set arg1 [lindex $argv 0]

spawn scp -oProxyCommand="ssh -W %h:%p B" ./$arg1 C:destination 


and the error is:

unknown option -- W 

W option works in command line but not in expect script. Can anybody tell me why?


Much easier would be to configure this in the ~/.ssh/config, where you don't need to care about the syntax of the expect scripts and its issues with arguments. Create ~/.ssh/config with this content:

Host C
  ProxyCommand ssh -W %h:%p B

and then connect just using

spawn scp ./$arg1 C:destination

Or just use the simplified version using ProxyJump option:

spawn scp -oProxyJump=B ./$arg1 C:destination 
  • Thank you for your answer. But there are some strange problems. I made the file you mentioned (~/.ssh/config) as you said. When I execute the command "scp -oProxyCommand="ssh -W %h:%p B" myfile C:destination" in terminal it works very well, But when I execute this one: scp myfile C:destination (after creating ~/.ssh/config) I get the following error: channel 0: open failed: administratively prohibited: open failed stdio forwarding failed ssh_exchange_identification: Connection closed by remote host lost connection – Mostafa Tavakoli Jun 10 '17 at 22:38
  • I searched for this error in forums and they say the intermediate host (B) doesn't allow this process. But how is it possible when I can execute the command "scp -oProxyCommand="ssh -W %h:%p B" myfile C:destination" directly?!!! – Mostafa Tavakoli Jun 10 '17 at 22:38
  • That is weird. Are you sute you put the hostnames correctly? – Jakuje Jun 11 '17 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.