From which hosts have I sshed

4

1

If I'm in a terminal and I ssh into a new host, then ssh to another host, then another etc., is there a way to determine this chain of hosts other than recursively exiting and seeing where I end up?

pythonic metaphor

Posted 2010-11-29T20:17:19.467

Reputation: 2 046

Answers

3

There are variables called SSH_CLIENT and SSH_CONNECTION that show in your current shell where you ssh'd from and which IP you came in on, but that only shows for the last connection. There are commands you can send to each ssh client connection (such as <RET><RET>~C to open the command line), but this is limited to just a few things. Maybe the most interested to you would be ~#, which shows connection info, but it doesn't show hosts.

If you can keep track of how many times you press ~ after <RET><RET>, you can background connection N in your chain starting from the end and in turn echo $SSH_CONNECTION each time to see where it goes. When you get back to your host, you'll need to foreground them all again with fg.

I guess my question for you would be, why do you need to know and do you need to know often?

deltaray

Posted 2010-11-29T20:17:19.467

Reputation: 1 665

I work in an environment with many hosts and I often ssh from machine to machine when I need to check something out on each host without bothering to exit. I tend to work out of the same few windows and after a while there are sometimes many jumps. This is fine except when it turns out one of the hosts is going down and I want to know which of my terminals is about to get killed. I won't be doing this often, so efficiency isn't too important. – pythonic metaphor – 2010-11-29T21:13:12.693

1

Run netstat -t -n|grep 22 (replace 22 with port number of your ssh-server if you changed it), at least you know the IP address of the host you are coming from. Or try who

Ken Ratanachai S.

Posted 2010-11-29T20:17:19.467

Reputation: 1 532

who gets me part of the way. You get output that looks like "user pts\3 Nov 30 09:09 (hostname)". And you can see what your current tty is with, well, tty. This at least establishes the first link. – pythonic metaphor – 2010-11-30T14:22:07.260

Oh yeah who is the way in your case also because there could be several ssh connections at a time and thus difficult to tell from netstat output. – Ken Ratanachai S. – 2010-11-30T16:43:33.373