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.

I have a shell script which I would like to do one thing if the user is physically using the computer, and one thing if the user is logged in via SSH.

How would I go about doing that?

In more specific terms, my normal machine is OS X, and there I want to use a OS X graphical editor (BBEdit) as my EDITOR. However, if I'm SSHing into my Mac from my netbook, I want to use vim - I don't have a graphical connection to my Mac.

Thanks!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You should be able to use the SSH_CONNECTION, SSH_TTY, or DISPLAY environment variables.

See: http://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/ssh.1.html

 SSH_CONNECTION        Identifies the client and server ends of the connection.  The variable contains
                       four space-separated values: client IP address, client port number, server IP
                       address, and server port number.

Edit: SSH_CLIENT is also an option, but it doesn't appear in the man page.

share|improve this answer
    
AWESOME! Thank you very much for your link to the ssh man page! –  RyanWilcox Jan 27 '12 at 17:07

Compare your env locally and when logged in via SSH.

I have e.g. SSH_CONNECTION, SSH_CLIENT, SSH_TTY that are only defined when logged in via SSH.

share|improve this answer
    
You beat me to a few seconds ;) –  Karolos Jan 27 '12 at 16:45
    
@Karolos See, you still got the checkmark for the more thorough answer ;) –  Daniel Beck Jan 27 '12 at 17:08
    
I'm new here, so I am not used to think that several people are also looking at it too while I reply ;) –  Karolos Jan 27 '12 at 17:11

I believe /var/run/utmpx (see "man utmpx") has this information. If you run "who", you see that the who command can tell if you are connected from a remote node (and even knows where you connected from).

So you could always use the who command and massage the output to determine this.

Note: I believe the answers above (about using SSH_CONNECTION) are a better solution.

share|improve this answer
    
The utmpx file (more commonly named utmp) already has a strictly defined format. It's better to read it directly than parse who, since the latter is not always consistent. –  grawity Jan 27 '12 at 16:54
1  
Gwawity: I disagree from a shell script. If doing this from Python or C, I would agree, but from a shell script I would argue against parsing this (binary) file directly. But others have suggested using the SSH_* environment variables, which is a better answer. –  Lee-Man Jan 27 '12 at 17:47
    
Notifying @grawity about a response. –  Daniel Beck Jan 27 '12 at 18:00

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.