SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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 !
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.