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
- Fails becase
case $- in *i*
link- Fails because
in
is not recognized?
- Fails because
- Use tty program (same as above)
tty
gave me a bizarre error code when executed fromsshrc
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?