0

Suppose I am working on LocalA (username userA). I use ssh to tunnel to RemoteB (username userB), then to RemoteC (username userC), then finally to RemoteDestinationD (username userD).

It would look something like the following:

userA@LocalA$ ssh userB@RemoteB
userB@RemoteB$ ssh userC@RemoteC
userC@RemoteC$ ssh userD@RemoteDestinationD
userD@RemoteDestinationD$ 

Suppose I forget which series of connections I used to connect from LocalA to RemoteDestinationD. It could have been one jump (e.g. userA@LocalA$ ssh userD@RemoteDestinationD) or three jumps (as in my example above). How could I programmatically determine what my series of jumps was? ("Scroll through your history, dummy!" doesn't count....)

I'd like a BASH script that would output something like the following:

userA@LocalA$ --> userB@RemoteB
userB@RemoteB$ --> userC@RemoteC
userC@RemoteC$ --> userD@RemoteDestinationD

Any ideas?

Thanks in advance!

jvriesem
  • 1,859
  • 3
  • 18
  • 40

1 Answers1

0

There is no good way to do this, I suspect. There are a bunch of true hacks you could do if you were willing, such as turning on X11 port forwarding and using different ports for counting.

Or, more obnoxiously:

sshcount=0
ssh userA@RemoteA "echo sshcount=$(($sshcount+1)) > .bashrc; bash"

And from there do something similar to get to B, so sshcount keeps incrementing.

Clearly this should be done via overwriting a .sshcountrc file or something, and source that from the .bashrc. And then, you might need to make the file name dependent on some munging of $SSH_TTY if you want to support doing parallel versions of this.

And the whole thing is really a hack so maybe you shouldn't anyway and you should solve the problem another way. Or remove the need for counting in the first place.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69