After logging onto a linux machine via SSH, I would like to be able to retrieve the name of the computer from which the connection was made. Ideally I am looking for some command like hostname or uname but that would retrieve the name of the client instead of the host.

This information must be accessible somewhere, since when I log on I get a message that contains the clinent name from the last login:

Last login: Thu Mar 11 18:42:01 2010 from my.address.com


The reason for wanting to do this is to be able to take different action in my .login file depending on which computer I am currently connecting from.

link|improve this question
feedback

1 Answer

up vote 3 down vote accepted

Short answer:

who -m

For bonus marks (yes, this is filthy, someone please leave a comment as to how to do it better):

who -m | awk '{print $5;}' | sed s/\(// | sed s/\)//

link|improve this answer
That was a quick answer, thanks! – amicitas Mar 12 '10 at 9:31
@amicitas: Check again, I made it better :D Upvote if it helped you! – Alexander Burke Mar 12 '10 at 9:32
Thanks, I was about to spend the next 30 minutes trying to remember how to use awk and sed again. – amicitas Mar 12 '10 at 9:40
@amicitas: My cleanup (for the parentheses) was quick-and-dirty, but it should do the job. You might even be able to get a raw IP from who instead of an FQDN; check the who manpage. This way you could adjust your .login so as to achieve "if logging in from 17.x.x.x (Apple, Inc.) then do $FOO", as not all of Apple's IPs may have PTR records ending in .apple.com. – Alexander Burke Mar 12 '10 at 9:47
1  
This is even cleaner :-): who -m | awk -F '[()]' '{print $2}' – janmoesen Mar 12 '10 at 11:46
show 1 more comment
feedback

Your Answer

 
or
required, but never shown

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