Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to call a program when any SSH user logs in that prints a welcome message. I did this by editing the /etc/ssh/sshrc file:

#!/bin/bash

ip=`echo $SSH_CONNECTION | cut -d " " -f 1`
echo $USER logged in from $ip

For simplicity, I replaced the program call with a simple echo command in the example

The problem is, I learned SCP is sensitive to any script that prints to stdout in .bashrc or, apparently, sshrc. My SCP commands failed silently. This was confirmed here: https://stackoverflow.com/a/12442753/2887850

Lots of solutions offered quick ways to check if the user is in an interactive terminal:

  • if [[ $- != *i* ]]; then return; fi link
    • Fails becase [ is not linked
  • case $- in *i* link
    • Fails because in is not recognized?
  • Use tty program (same as above)
    • tty gave me a bizarre error code when executed from sshrc

While all of those solutions could work in a normal BASH environment, none of them work in the sshrc file. I believe that is because PATH (and I suspect a few other things) aren't actually available when executing from sshrc, despite specifying BASH with a shebang. I'm not really sure why this is the case, but this link is what tipped me off to the fact that sshrc is running in a limited environment.

So the question becomes: is there a way to detect interactive terminal in the limited environment that sshrc executes in?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
458 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use test to check $SSH_TTY (final solution in this link):

test -z $SSH_TTY || echo $USER logged in from $ip

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...