LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-04-2009, 11:30 AM   #1
ccargo
LQ Newbie
 
Registered: Feb 2009
Distribution: Slackware
Posts: 20

Rep: Reputation: 1
How to detect if logged in via ssh?


Hi!

This probably has an easy solution.. I want my bash prompt to reflect if I am logged in via ssh or not.

I have tried checking if $SSH_CLIENT is set, but this does not work if I su to root after logging in on a remote machine ($SSH_CLIENT will not be set for root in that case).

A second option I've tried is simply checking if $SCREEN is set, this works well on remote clients. However, when I su to root on my local machine, $SCREEN is no longer set so I get a false alarm.

I'd be grateful for any suggestions.

Cheers
 
Old 08-04-2009, 03:24 PM   #2
deesto
Member
 
Registered: May 2002
Location: NY, USA
Distribution: FreeBSD, Fedora, RHEL, Ubuntu; OS X, Win; have used Slackware, Mandrake, SuSE, Xandros
Posts: 448

Rep: Reputation: 31
Quote:
Originally Posted by ccargo View Post
This probably has an easy solution.. I want my bash prompt to reflect if I am logged in via ssh or not.
What about simply:
Code:
 w|grep ssh|grep -v grep
If it returns anything, you can see who is logged in via ssh.
 
Old 08-04-2009, 04:11 PM   #3
ccargo
LQ Newbie
 
Registered: Feb 2009
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 1
Thanks for your reply!

However, 'w' does not return the word 'ssh' in my case. The 'FROM' field simply states the host from which I am logged in, and that varies.

Also, I just realised that I can avoid the problem (without actually solving it) by using 'su' instead of 'su -' when switching to the superuser as that leaves $SSH_CLIENT intact.
 
Old 08-04-2009, 09:11 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
... but in that case you don't get root's env, (specifically PATH) ...
 
Old 08-05-2009, 01:43 AM   #5
ccargo
LQ Newbie
 
Registered: Feb 2009
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 1
No, I don't. So I'm still interested in finding an actual solution. At least if there's a reasonably straightforward one
 
Old 08-05-2009, 06:58 AM   #6
maddes.b
LQ Newbie
 
Registered: Aug 2009
Location: Germany
Distribution: Debian, OpenWrt
Posts: 23

Rep: Reputation: 1
Finding login method by going up the process tree

This should work on any full Linux (like Debian):
Code:
#!/bin/sh

# check if this script is run from inside a client session
PROG='/usr/sbin/sshd'
pid="$$"
while [ "${pid}" -ne 0 ]
 do
	# get parent process id
	pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"`
	[ "${pid}" -eq 0 ] && break

	# check if client connection
	ps -f -p "${pid}" | grep "${PROG}" >/dev/null
	[ $? -eq 0 ] && break
done
[ "${pid}" -ne 0 ] && echo "Logged in via ${PROG}"
On embedded machines with Busybox the ps command may not support the -f switch.
For BusyBox use :
Code:
ps | grep -e "^[ ]*${pid} " | grep "${PROG}" >/dev/null
Maddes

Last edited by maddes.b; 08-05-2009 at 07:03 AM.
 
Old 08-05-2009, 08:18 AM   #7
deesto
Member
 
Registered: May 2002
Location: NY, USA
Distribution: FreeBSD, Fedora, RHEL, Ubuntu; OS X, Win; have used Slackware, Mandrake, SuSE, Xandros
Posts: 448

Rep: Reputation: 31
maddes, JFYI: that script would throw up multiple chunks if run on a Mac (ps options and no proc) ... but Darwin isn't really Linux

Incidentally, my simple 'w' line above would do the trick in Darwin but not for Linux.

Last edited by deesto; 08-05-2009 at 08:28 AM.
 
Old 08-06-2009, 03:25 AM   #8
maddes.b
LQ Newbie
 
Registered: Aug 2009
Location: Germany
Distribution: Debian, OpenWrt
Posts: 23

Rep: Reputation: 1
Thanks deesto for the info.
I'm mainly working on Debian GNU/Linux, so do not know much about Darwin.
According to ccargo's profile he is using Slackware, I hope that my solution will work on it.

What to do if you encounter problems with this:
If ps doesn't support "-p" to select a specific pid, then make sure that it lists all pids and grep it as mentioned for Busybox.

If ps doesn't support "-f" to print the executable, then check for other options to do so.
If not possible then reduce the program check to just the name (sshd instead of /usr/bin/sshd). Note that a script called "sshd" could lead to a false positive here.

If /proc is not available, then try to get parent pid via ps and sed.

If all fails maybe you can find other ways/commands to go up the process tree to check for the login method.
Good luck.
 
Old 08-10-2009, 03:45 AM   #9
ccargo
LQ Newbie
 
Registered: Feb 2009
Distribution: Slackware
Posts: 20

Original Poster
Rep: Reputation: 1
Thank you maddes.b! Your script works perfectly on my machines (and yes, they are running Slackware). Thanks also to deesto and chrism01. I'm marking this thread as solved.

Thanks once again!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet issues when logged in using SSH garethjones4 Linux - Networking 1 04-17-2008 03:45 AM
Unable to us SU to root when logged in via SSH CaptainReboot Linux - Server 5 12-29-2007 05:36 AM
How to transfer files via ssh once logged Nerox Linux - Networking 3 05-06-2007 12:19 PM
user logged on with ssh not shown with w or who galle Linux - General 4 02-10-2006 10:24 AM
how to run GUI while logged in with ssh? kpachopoulos Linux - General 5 11-03-2004 04:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:08 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