LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with script execution in bashrc (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-script-execution-in-bashrc-597126/)

globemast 11-04-2007 04:49 PM

Help with script execution in bashrc
 
Hello,

I am running Ubuntu 7.10 and yesterday i added a script in my .bashrc file.

I want this script to be executed only when i login via SSH to my account. I have tested the script execution and it works.

Unfortunately though, the script gets executed also when i locally open a terminal window.

Is there a way i can make this script executed ONLY when i login via SSH?

Thank you in advance.

prffzc 11-04-2007 07:53 PM

Hi there,

All the scripts in .bashrc will be automatically executed with each new terminal.

The trick is to use an alias ssh_new_script='whatever_path_to_real_script', and in this way you can call only the alias.

Have fun!

complich8 11-05-2007 12:41 AM

Basically, you have two possibilities.

Option 1: add the script invocation to .bash_profile, and your other stuff in .bashrc (with .bash_profile sourcing .bashrc, assuming you've got something you want in there too). .bash_profile is sourced on login sessions, .bashrc on interactive non-login sessions. But this would also get you console logins and screen session starts, so may not be ideal.

Option 2: add it to .bash_profile or .bashrc with a conditional
Code:

[[ -n $SSH_CLIENT ]] && /path/to/script.sh
(which checks if SSH_CLIENT is a nonzero-length string, and if it is executes the script). This takes advantage of the fact that ssh sessions set a couple variables that interactive logins don't, but the downside is that those variables propagate to child processes, so descendants wouldn't be able to tell.

Combining the two approaches and a little more gives us a complete solution though.
In .bash_profile (or, really, .bashrc would work at this point too):
Code:

[[ -n SSH_CLIENT && -z $SSH_IS_CONNECTED ]] && export SSH_IS_CONNECTED=1 && /path/to/script
This checks if the SSH variables are defined and the SSH_CONNECTED variable isn't. If that's the case, then it sets it, and executes the script. The nonzero SSH_CONNECTED variable propagates with the other ssh vars, so that no longer evaluates true even if you source .bash_login again (eg: invoking screen).

globemast 11-07-2007 03:28 AM

Hi complich8,

thanks for the prompt reply.

I have used
Code:

[[ -n SSH_CLIENT && -z $SSH_IS_CONNECTED ]] && export SSH_IS_CONNECTED=1 && /etc/script.sh
in /etc/bash.bashrc file and it still executes the script.sh when i open a terminal window.

When i open a terminal window though, as you correctly said above, the $SSH_IS_CONNECTED env. variable is not defined. When i connect via SSH then it is.

Any suggestions?


Thanks in advance.

jschiwal 11-07-2007 03:42 AM

Put your commands in ~/.ssh/rc.
Quote:

~/.ssh/rc
Commands in this file are executed by ssh when the user logs in,
just before the user's shell (or command) is started. See the
sshd(8) manual page for more information.


All times are GMT -5. The time now is 04:33 PM.