1

Say I have a terminal open on a Mac (or any unix-ish box)

fattie$

I can of course do this:

fattie$ scp -i key.pem [email protected]:file.js .

and it will copy file.js from my AWS server, to, the Mac. All good so far.

Another day, I do this

fattie$ ssh -i key.pem [email protected]

I'm now in a shell on the Mac, but, I'm logged in to a shell on the aws machine

ubuntu@ip-111-22-3-444:~$ 

All good.

If I want to transfer the file file.js back to the Mac, of course you can do it along the lines of:

ubuntu@ip-111-22-3-444:~$ scp file.js [email protected]

No problem.

But.

I'm already "in" the Mac, if you see what I mean.

The parent shell (is there such a concept??) of the ubuntu shell is indeed the Mac in question. I'm even in the correct directory I want on the Mac.

In fact, is there some way to tell scp - or some other command? - to copy from

  • the shell you are in (i.e., the aws shell in the example) to

  • "where you are locally", the "parent" shell, the Mac in the example?

Perhaps something like ...

ubuntu@ip-111-22-3-444:~$ cat file.js > "the parent machine"/done.js

or even just

ubuntu@ip-111-22-3-444:~$ cp file.js "the parent machine"

... if you see what I mean?

Is there a way to get to the "parent" shell ??

(I guess, you could overkill using nc but you'd have to set up the local end first!)

It would often be very convenient to do this.

2
  • You can always 'exit' back to the Mac, but I assume that you want to keep the AWS session going. You can always open another tab to perform the local 'cp' without disrupting the AWS session. If you're trying to do this from inside a script, you're out of luck, it would have to be scripted outside of the context of the AWS session. EDIT : I mean, you probably COULD expose your Mac to the general Internet, put it in your DMZ or something similar, and then cause commands to be issued to it from the AWS session, but that's insecure and a lot of work. May 6, 2019 at 22:45
  • hi @ChristopherHostage - sorry if the question was unclear; it's unrelated to AWS.
    – Fattie
    May 6, 2019 at 23:31

2 Answers 2

2

Yes and no. Shells (both local and SSH connections) do stack, but the remote shell started via SSH has no access to the locally running client. There is no back-tunnel for file transfer (there is one for ssh-agent but that's all).

You may be able to scp/sftp from the remote server back into the local computer, but it would act as a fully independent connection (yet another new layer in the stack) – it would know nothing about what "current folder" you were in.

So because the only method is a new SSH connection, obviously this needs your local computer to have an SSH server (sshd) running to begin with. (Fortunately, unlike nc, the sshd only needs to be set up once and left running forever.)

If your computer has a public IP address, the next step is to just SSH back from the Ubuntu system to your own IP address (as can be found in $SSH_CONNECTION). If both the client and the server have IPv6, that can be used.

Most PCs don't have a public IP address, so the alternative is to use SSH's -R "remote TCP forwarding" feature to establish a back-channel – note that in this case it's still a brand new SSH login into your computer, it still doesn't know anything about the "parent" shell:

MyMac$  pwd
/Users/Fred/Documents/This/That
MyMac$  ssh -R 12345:localhost:22 ubuntu.example.com
  ubuntu$  ssh -p 12345 localhost
    MyMac$  pwd
    /Users/Fred
    MyMac$  exit
  ubuntu$  scp -o Port=12345 file.json localhost:Documents/

In this example 12345 is a random port number that'll start accepting connections on the server, which will be carried to sshd on your local computer's port 22.

Note: In this example the -R option is specified before connecting, but if you happen to need it retroactively, you can just open a second connection (this time with -R) and the tunnel will be available globally. It is not limited to that particular shell/connection.

(Alternatively the keys Enter ~ C should pop up an interactive prompt which accepts "-R 123..." as input and establishes the forwarding.)


Personally, I would recommend just SFTP'ing directly to the remote machine. Then you can navigate to the file using somewhat regular 'cd' and 'ls', then retrieve files using 'get'.

The default OpenSSH sftp client isn't great but works fine; lftp is one of the better alternatives.


the lame way to do what I say is, just cat the file on screen. Then you effectively have it on the local machine: copy and paste to a file :) ( It occurs to me, one could write a custom version of a Terminal app that has this feature: it would understand when you type cat f.txt > LOCAL/f.tx it would actually just capture the result and save it to a text file!!! Heh! ) – Fattie 6 hours ago

Yes, some terminals support this – it's actually a feature that comes from the dial-up system and BBS days, and is called ZMODEM or XMODEM.

To receive a file via ZMODEM, you first make sure the local terminal has the file transfer feature enabled (I think it's present at least in the GNU Screen multiplexer, although very rarely in modern terminals), then run sz <file> on the remote server and you should see the file pop up locally.

It seems someone has written integration scripts for Zmodem in iTerm2, too.

4
  • grawity, thanks for the answer - it pretty much sounds like the answer to my question is "In fact NO, there's absolutely definitely NO way to 'access' the 'parent' shell..." ........ does that sound about right?! Thanks again, awesome!
    – Fattie
    May 6, 2019 at 22:17
  • BTW the lame way to do what I say is, just cat the file on screen. Then you effectively have it on the local machine: copy and paste to a file :) ( It occurs to me, one could write a custom version of a Terminal app that has this feature: it would understand when you type cat f.txt > LOCAL/f.tx it would actually just capture the result and save it to a text file!!! Heh! )
    – Fattie
    May 6, 2019 at 22:20
  • @Fattie: Well, if you're using iTerm2, check out Zmodem grack.com/blog/2011/10/26/… – it's one of the oldest ways of transferring files through a terminal connection (along with simple 'cat' of course). May 7, 2019 at 4:31
  • Ah! thanks for that, grawity ! :) Really that's exactly what I meant, I guess! Cheers...
    – Fattie
    May 7, 2019 at 14:15
1

@grawity is correct (and upvoted) for the problem as stated.

However, if you are willing to create an SSH server instance on your local machine, and your local machine's SSH server is accessible from the host blah, you can use this command, which works even without knowing what your local machine's hostname or IP is. You do have to know the username, though, and possibly the port number, if you're running SSH on a non-standard port:

$ scp file.js fattie@${SSH_CLIENT%% *}:done.js

where fattie is your username on the local Mac. That command line also assumes you're running bash for your shell on blah.

But again, as grawity points out, you'd need to be running SSH on the Mac, and if your Mac is on a private LAN behind a firewall, then the firewall would have to be configured to pass incoming TCP port 22 through to your Mac. This command is just a shortcut to perform the copy without you having to know what your public IP is, or having some sort of dynamic DNS in place.

1
  • Right, sure I know how to do this (thank God! :) ) ... I'm sure it will be a helpful tutorial for folks. Jim, it looks like you also have confirmed there's just no way to get to the 'parent' shell!! Thanks man!
    – Fattie
    May 6, 2019 at 23:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .