0

I had this idea for long time and tried several ways to implement, with no luck. Tried to google many times and always finding not quite what is needed.
How to do:

$ ssh hostA

|--------------------------------|
|                                |
|                                |
|                                |
|                                |
|                                |
|                                |
|                                |
| root@hostA $ ssh hostB         |
|--------------------------------|

|--------------------------------|
|                                |
|                                |
|                                |
|                                |
|                                |
|                                |
| root@hostB $                   |
| root@hostA $                   |
|--------------------------------|

The closest solution I came up with is with gnu screen status bar, displaying ssh from hostA to hostB (the trick is to use $SSH_... variables). Would be nice to have a visual stack of all connections (not just 2).

5
  • That's not practical. Imagine you run in both sessions top, how is it supposed to be displayed? The best solution is tmux or if you have a X-Window-System up and don't mind some Gnome-Dependencies, Terminator.
    – Bobby
    Jun 12, 2012 at 9:34
  • So you are just looking to have the prompt reflect the host you sshed from as well as the one you are connected to? Couldn't you just update the prompt in a bash profile, using SSH_ vars?
    – Paul
    Jun 13, 2012 at 4:27
  • @Bobby , actually how this is displayed is not important - the key is to know the path of connections. e.g. ssh from hostA to hostB and then to hostC could be shown like a stack or as a single line: "hostA -> hostB -> root@hostC $"
    – Alex
    Jun 14, 2012 at 2:10
  • @Paul , as in my question text I did manage to get it to work with $SSH_ vars but this works max for now+previous hosts. What if I want to see a chain/tunnel of 4 connections?
    – Alex
    Jun 14, 2012 at 2:12
  • Oh wait, did you just mean to display the path? Like root@hostB_via_hostA, root@hostC_via_hostB_via_hostA? In that case scratch my comment.
    – Bobby
    Jun 14, 2012 at 6:54

2 Answers 2

1

In ~/.ssh/environment you can set environment variables that you wish to pass through to remote hosts. This requires the PermitUserEnvironment setting enabled in sshd_config.

So what you could do is in .bashrc or whichever script you prefer, set an environment variable to pass through:

echo "HOSTPATH=$HOSTPATH-$USER@$HOSTNAME" >> ~/.ssh/environment

For your first hop, HOSTPATH will be empty, so ~/.ssh/environment would contain HOSTNAME=-user@host1.

As you log into the second host, the HOSTNAME env var would be passed through showing where you came from. As the bashrc is run again the HOSTPATH variable would be written to .ssh/enviroment with the current host appended to the first, ready for the next hop:

HOSTNAME=-user@host1-user@host2    

Each hope would result in HOSTPATH being appended with the current host and ready to pass through via the ssh environment to the next hop.

You can adapt this for your own asthetics. If you exit from a host, and jump to another, it should automatically delete the host you came from from the path, as it will re-use the currently set .ssh/environment setting. It might even deal with loops, I'll leave that as an exercise for the reader :)

1

Have you tried/looked at screen?

It's not exactly what you want, but you can issue screen -la whenever you like to get the listing you desire.

1
  • Hi Nada, for me, running screen and screen -la seem to produce the same result
    – Alex
    Jun 14, 2012 at 2:14

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.