I am attempting to ssh into a server by using a bash script from MacOSX.


spawn ssh username@gatewayserver
expect "password"
send "Mypassword\r"

spawn ssh username@storageserver
expect "password"
send "Mypassword\r"
cd /path

spawn scp -r retrievedfolder username@gatewayserver:/path/
expect "password"
send "Mypassword\r"


spawn scp -r username@gatewayserver:/path/retrievedfolder .
expect "password"
send "Mypassword\r"

spawn ssh username@gatewayserver
expect "password"
send "Mypassword\r"
rm -r retrievedfolder/

mv -nv -- "$retrievedfolder" "$retrievedfolder.$(date +%Y%m%d)"
mv /retrievedfolder /backup

If you can follow the probably redundant code, the goal was to log into the gateway server to get to the storage server. cd to the appropriate folder. secure copy the folder i want to a directory on the gateway server, and then copy the file from the gateway server to the desktop. Then I want to remove the retrieved folder from the gateway server (storage limitations) and move the copied folder to a folder on the desktop with the current date appended to the end of the file name. I think that most of what I have written should work if I were running linux but bash on MacOS isn't recognizing spawn send or interact and it is looking for a file or directory after expect.

After some help from Stephen Kitt and webKnjaZ, my code looks more like this:


ssh username@gatewayserver 'ssh username@storageserver; cd /path/; scp -r retrievedfolder username@gatewayserver:/path/'

scp -r username@gatewayserver:/path/retrievedfolder .

ssh username@gatewayserver 'rm -r retrievedfolder/'

mv -nv -- "retrievedfolder" "retrievedfolder.$(date +%Y%m%d)"
mv retrievedfolder.$(date +%Y%m%d)/ backup/

After correctly setting up passwordless SSH it almost works correctly. Without ssh -tt it gives me an error message: "Pseudo-terminal will not be allocated because stdin is not a terminal". With ssh -tt it stops at the command prompt once logged into the second server, and when I use ssh -T it hangs. (probably in the same spot, but just not visible)

  • that's expected behavior. when you try to run 'ssh under ssh' the remote one doesn't get access to your TTY. But the point is to avoid binding to TTY. See my answer updated. – webKnjaZ Feb 19 '15 at 15:43

I think you're just trying to copy files from the server behind NAT via gatewayserver.

I'd suggest you more simple solution.

  1. Set up password-less authentication (put private key from your desktop/Mac to both mentioned servers)
  2. Use something like

    ssh -MNf -L 60022:storageserver:22 username@gatewayserver

    to set up ssh tunnel via gatewayserver. Now your Mac's port 60002 has direct connection to port 22 of storageserver.

  3. At this point you may copy files directly via this tunnel:

    scp -P 60022 -R username@localhost:/path/to/folder .

Put these commands into your bash script and you will achieve what you want.


Putting it all together, here's complete script:


# set up tunnel
ssh -MNf -L 60022:storageserver:22 gatewayserver_username@gatewayserver || true

# copy files __directly__ into correct backup folder
scp -P 60022 -R "storageserver_username@localhost:/remote/path/to/retrievedfolder/at/storageserver" "/local/path/to/backup/retrievedfolder.`date +%Y%m%d`"

Prerequisites (run just one time on your Mac):

# Create SSH keys @ local machine

# Put your local SSH key to the gateway server
ssh-copy-id gatewayserver_username@gatewayserver

# Enable tunnel (will not ask your password if previous steps are correct)
ssh -MNf -L 60022:storageserver:22 gatewayserver_username@gatewayserver

# Put your local SSH key to the storageserver server
ssh-copy-id -p 60022 storageserver_username@localhost
| improve this answer | |
  • In addition to this, you might consider using ssh keys so that you are not constantly required to enter your password manually. Then the only thing your script would need to wait on is the completion of the copy action. – Klaatu von Schlacker Feb 18 '15 at 2:42
  • I made passwordless ssh work and now my script is a lot shorter and tidier, but it still isn't quite working. I've edited the question to reflect the changes. – Charles Butler Feb 18 '15 at 23:02
  • Klaatu von Schlacker, that's exactly what I meant :) – webKnjaZ Feb 19 '15 at 15:07
  • Charles Butler, there's no need to run scp on remote machine. All commands I wrote are meant to be ran from localhost – webKnjaZ Feb 19 '15 at 15:09
  • I've updated my answer, hope it became more clear. – webKnjaZ Feb 19 '15 at 15:35

If you look at Shell Script for logging into a ssh server you'll notice that the shebang line is


This is an expect script, not a bash script. You'll need to install Expect if you don't already have it.

| improve this answer | |
  • Fantastic! I did not catch that. I suppose I should search some more on how to do it in bash then. – Charles Butler Feb 17 '15 at 23:34
  • Why not just try Expect? It may even already be installed on your Mac... – Stephen Kitt Feb 17 '15 at 23:35
  • Stephen, expect is installed. I am attempting to write it in bash because I already know at least rudimentary bash scripting and would rather get better at one language than learn the basics of another. – Charles Butler Feb 17 '15 at 23:40
  • @CharlesButler If you don't have password-less authentication set up, trying to enter passwords will lead you to expect sooner or later. – muru Feb 17 '15 at 23:43
  • @muru After a bit of searching I haven't found an easy way of doing what I want solely in bash. Now I am attempting to call expect within the bash script – Charles Butler Feb 17 '15 at 23:58

To simplify even further:

Firstly, you should use keys rather than passwords (which you're now doing).

Secondly, add the following to your ~/.ssh/config

Host storageserver
    ProxyCommand ssh gatewayserver nc %h %p

Thirdly, use rsync so your script becomes a single command:

rsync -havz storageserver:/path/retrievedfolder/ backup/retrievedfolder.$(date +%Y%m%d)

Which is a fair bit shorter than the starting point!

| improve this answer | |

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.