LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 12-13-2009, 12:48 PM   #1
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Detecting an SSH Session


When in an SSH session I modify my command prompt by appending (ssh) to the command prompt string. Works nicely to remind me I'm in an SSH session.

I also modify the prompt command by inserting the host name, but the (ssh) string really helps.

Works nicely until I run the su - {username} command. Using the "-" option ignores the original SSH environment variables that are created.

Of course, when not using the "-" option then the original SSH environment variables are preserved.

Any ideas how I can determine I am in an SSH session to modify the command prompt after using the "-" option?

Thanks again.
 
Old 12-13-2009, 01:20 PM   #2
voyciz
Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 425

Rep: Reputation: 40
Can't you just modify the files in ~/.ssh (such as ~/.ssh/config ~/.ssh/environment ~/.ssh/rc) ?
 
Old 12-13-2009, 01:57 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you should be able to check the $TTY variable to know if you're on a local terminal of not, i think.
 
Old 12-13-2009, 07:11 PM   #4
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Quote:
Originally Posted by Woodsman View Post
When in an SSH session I modify my command prompt by appending (ssh) to the command prompt string. Works nicely to remind me I'm in an SSH session.

I also modify the prompt command by inserting the host name, but the (ssh) string really helps.

Works nicely until I run the su - {username} command. Using the "-" option ignores the original SSH environment variables that are created.

Of course, when not using the "-" option then the original SSH environment variables are preserved.

Any ideas how I can determine I am in an SSH session to modify the command prompt after using the "-" option?

Thanks again.
Woodsman --

Do you have an SSH_TTY env varb ?

Code:
############ local xterm ##############

[konrad@kjhlt4 ~]$ env |grep SSH_TTY
[konrad@kjhlt4 ~]$ [ "$SSH_TTY" != "" ] && echo "ssh session" || echo "bash session"
bash session

############ ssh session ##############

[konrad@kjhlt4 ~]$ ssh localhost
konrad@localhost's password:
Last login: Fri Dec 11 09:21:33 2009
Linux 2.6.24.3-smp.

Man is a rational animal who always loses his temper when he is called upon
to act in accordance with the dictates of reason.
                -- Oscar Wilde

[konrad@kjhlt4 ~]$ env |grep SSH_TTY
SSH_TTY=/dev/pts/17

[konrad@kjhlt4 ~]$ [ "$SSH_TTY" != "" ] && echo "ssh session" || echo "bash session"
ssh session

############# su - ##################

[konrad@kjhlt4 ~]$ su -
Password:

"Whom are you?" said he, for he had been to night school.
                -- George Ade

[root@kjhlt4 ~]# [ "$SSH_TTY" != "" ] && echo "ssh session" || echo "bash session"
bash session

[root@kjhlt4 ~]# exit
logout

############ ssh session ##############

[konrad@kjhlt4 ~]$ [ "$SSH_TTY" != "" ] && echo "ssh session" || echo "bash session"
ssh session

[konrad@kjhlt4 ~]$ exit
logout

############ local xterm ##############

Connection to localhost closed.
[konrad@kjhlt4 ~]$ [ "$SSH_TTY" != "" ] && echo "ssh session" || echo "bash session"
bash session
HTH


-- kjh
 
Old 12-13-2009, 08:02 PM   #5
ljb643
Member
 
Registered: Nov 2003
Posts: 526

Rep: Reputation: Disabled
Since "su -" gives you a 'clean' environment, by design, the SSH environment won't help. The terminal name (`tty`) won't help, since xterms and such also use ptys. The only way I can think of is this somewhat crude hack.

Code:
# Must be sourced to shell, not run as a script.
gppid=$(awk '/^PPid:/ {print $2}' /proc/$PPID/status)
if grep -q ^sshd /proc/$PPID/cmdline /proc/$gppid/cmdline
then
  echo YES we got here via ssh
else
  echo NO I do not recall getting here via ssh
fi
Works for me via ssh or one level of su [-] after ssh. It has to run in your top-level process, or via ". script" (like .profile), not in a subshell. It won't work for another (nested) su, but it could be extended to check more levels.
 
1 members found this post helpful.
Old 12-13-2009, 09:03 PM   #6
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Quote:
Do you have an SSH_TTY env varb ?
Yes, but . . .
Quote:
Since "su -" gives you a 'clean' environment, by design, the SSH environment won't help.
Yes, the three SSH environment variables created by the SSH session disappear.

Quote:
The terminal name (`tty`) won't help, since xterms and such also use ptys.
I concluded the same thing after exploring trying to work with the tty command.

Quote:
The only way I can think of is this somewhat crude hack.
Crude or not, I had spent a couple of hours scratching my head before posting. Your solution works wonderfully and you opened the door to learning something new. I never knew about /proc/$PPID/cmdline.

Thanks much!

Last edited by Woodsman; 12-13-2009 at 09:08 PM.
 
Old 12-14-2009, 04:27 AM   #7
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
Quote:
Originally Posted by ljb643 View Post
Since "su -" gives you a 'clean' environment, by design, the SSH environment won't help. The terminal name (`tty`) won't help, since xterms and such also use ptys. The only way I can think of is this somewhat crude hack.

Code:
# Must be sourced to shell, not run as a script.
gppid=$(awk '/^PPid:/ {print $2}' /proc/$PPID/status)
if grep -q ^sshd /proc/$PPID/cmdline /proc/$gppid/cmdline
then
  echo YES we got here via ssh
else
  echo NO I do not recall getting here via ssh
fi
Works for me via ssh or one level of su [-] after ssh. It has to run in your top-level process, or via ". script" (like .profile), not in a subshell. It won't work for another (nested) su, but it could be extended to check more levels.
Dooh !

Thanks ljb643, I COMPLETELY misread Woodsman's original post !

Interesting code there !

-- kjh
 
1 members found this post helpful.
Old 12-14-2009, 06:22 PM   #8
ljb643
Member
 
Registered: Nov 2003
Posts: 526

Rep: Reputation: Disabled
Thanks.
I should have provided a few details for those trying to learn...
"gppid" is "grandparent process ID", the parent of PPID (parent process ID).
Get the GPPID from the parent process's "status" file, by looking at his "Parent Process ID".
Then check the both parent and grandparent process' command line (cmdline) to see if either is running sshd.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SSH concurrent session limit and idle session time out lasygsd Linux - Newbie 3 10-30-2014 07:56 AM
ssh session bkone Linux - Newbie 3 10-17-2007 02:34 PM
script detecting ssh password request jp9999 Linux - Software 1 05-02-2006 02:46 PM
ssh -> perl -> spawn background proces hangs ssh session rhoekstra Programming 2 04-25-2006 01:05 AM
How to run a program in an ssh session that will run after you close out the session? dr_zayus69 Linux - Networking 5 03-05-2006 07:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 07:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration