Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

At my workplace, we have a number of Linux machines on an isolated network, and I occasionally need to determine which of them I am logged in to. This can be a pain because I usually have a large number of multi-tab terminal windows strewn across two monitors, four gnome workspaces, and sometimes multiple login sessions.

I do have a list of most of the machines I use and something like $(rsh $M ps -wef | grep username | wc -l) usually works, but it is slow and not robust. I am hoping there is some Unix utility or directory service that keeps track of all network logins and that I can simply check that and grep for my username. I am surprised that I have not been able to find this by searching on Google and I suspect that no such utility exists, but I hope I am wrong.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Very simple, and showing whatever names or shortcuts you use when you specify the server to ssh:

ps --no-heading -o args -C ssh

returns:

ssh server1
ssh server2
ssh server1

(see man ssh_config for details of defining shortcuts)

share|improve this answer
    
Thanks. All good answers, but for my particular workflow, this one is definitely the most straightforward, and it works perfectly for me after a little tweaking. –  monguin Jul 5 '10 at 18:00

There's no such utility, but one thing that might get you closer is looking at netstat and checking that the destination is in a list of servers and that the process name is ssh.

Also, don't use rsh unless you like getting your machines compromised.

Edit: More info

netstat lists all currently active network connections to anywhere, given the correct flags. (man netstat). Approaching your problem from another direction, have you tried organizing your connections more? I'd use screen and name each window in screen with the server you're connecting to (Using ^a,A.)

share|improve this answer
    
netstat seems like it does almost exactly what I need - it shows a list of servers, most of which I am logged in to, plus a few extras that I'm not sure about. Is there some caveat to this program that I should know about? These machines are just on a local network, they are not connected to the internet. –  monguin Jun 30 '10 at 17:51
    
I actually had my PS1 variable set to put the host name in the terminal window title. I have no idea why, but that stopped working at some point, which certainly makes this whole thing more frustrating. I'm thinking I can use netstat's options and some miscellaneous unix tools to print out my list - is there any reason that shouldn't work? –  monguin Jun 30 '10 at 18:11

Accurate, but quicker than netstat:

lsof -i -a -c ssh

It returns data like this:

COMMAND   PID    USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
ssh     15072  myuser    3u  IPv4 8118099      0t0  TCP self:51941->server1:22 (ESTABLISHED)
ssh     26242  myuser    3u  IPv4 9051562      0t0  TCP self:35685->server2:22 (ESTABLISHED)
ssh     26249  myuser    3u  IPv4 9051889      0t0  TCP self:46646->server1:22 (ESTABLISHED)

So you can strip out the list of server names like this:

lsof -i -a -c ssh | sed -n -e 's/.*>//' -e 's/:.*//p' | sort -u

which returns:

server1
server2
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.