1

I have a host on which I created a script .

The script is being executed whenever the user is logging in via ssh bashrc launches the script.

Now I'm trying to get the script to execute even if the user is not actually logging in , and just running a command . For example I want the script to be executed if a user is running the following :

ssh [email protected] some_command

Is there a way to achieve the above?

2
  • so what's the problem with the above approach? you can run commands like "ssh user@host '/path/to/script.sh'" if you have everything (permissions) configured correctly. Commented Sep 25, 2013 at 15:25
  • I guess that he wants a script being executed on the remote host when the user runs a script via ssh not only when the user logs in. Commented Sep 25, 2013 at 15:45

2 Answers 2

2

A solution affecting all the users could be using pam-exec and launch a script on the user login event. Check the pam-exec manual page and an example on how to use it pam-exec scripting.

A simple solution for a single user should be add the script in the rc file of the ssh user, add your script to:

~/.ssh/rc

I've done some tests and the rc solution works fine in your case, it gets executed when the user launches a remote command via ssh.

If you don't have a rc file just create it.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the reply! but what if I don't have an rc file under ~/ssh dir ? btw this is a win host running cygwin
You're welcome, if you find my answer helpful please upvote and/or accept it. If you don't have the rc file just create it in the ~/.ssh/.
when I create it , what should be writen inside ? '!/bin/bash ./script_name' ? or something else?
You have 2 possibilities: or the full script or a call to your script.
Nice! If you find my answer helpful please upvote and accept it. Thanks!
0

you can edit authorized_keys file and add a COMMAND , something like : command="/home/michale/bin/dothis.sh" ...public key... for more details read ssh and authorized_keys documentations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.