Run bash script upon ssh - linux

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 user#host.com some_command
Is there a way to achieve the above?

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.

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.

Related

Run bash script after login

I have a bash script with a series of whiptail menus that allows a user to setup their new system, which is Ubuntu server, with no GUI, just CLI (it's going to be a Virtual Machine image).
I'm already forcing a root login by editing /etc/default/grub and /etc/init/tty1.conf, so the user is dropped directly into the root command prompt. From there the user has to type in ./whiptail.sh to start the script and get the whiptail prompts to further setup their host.
Now, I'd like for my script to be what appears up after the the login occurs instead of the user being dropped to the command prompt. How can I do this?
All interactive sessions of bash will read the initialization file ~/.bashrc.
So you can just add the script at the end of the root's .bashrc i.e. /root/.bashrc, assuming the script is executable:
echo '/path/to/whiptail.sh' >>/root/.bashrc
Now the script will be always run when root opens a new interactive shell. If you only want to run while login only, not all all interactive sessions you should rather use ~/.bash_profile/~/.bash_login/~/.profile (the first one available following the order).
If you want it to be global, add you script to
/etc/profile
If you want it to be user-specific, add you script to
/home/$USER/.profile
Consider upvoting the original answer here: https://unix.stackexchange.com/a/56088/343022

How to run shell script in another server in a script?

No where online can i find a way to run a shell script on a remote server from another script. This is for automation, so the script on the host machine will automatically trigger another script on a different server. The server that my script will ssh to will either have a password prompt or have RSA key pair set up
Thanks!
Just pass the command as an argument to ssh.
ssh someserver /path/to/some/script.bsh
Let's say you want to execute a script on node2 but you have your script on node1 file name of script is sp over location /home/user/sp. Simply
ssh node2 < /path-of-the-script-including-the-filename
Another way, using expect package.
Disclaimer: This you can use for testing environments since it has an
open password. but depends on your usecase
If your server does not have expect, you may add the package then. run the command. You can also put this command inside an .sh script.
expect -c 'spawn ssh user#10.11.12.13 "/path/to/my.sh"; expect "assword:"; send "Y0urp#ssw0rd\r"; interact'

Shell script - SSH

I am using shell script to add some file to server. Is there any way to write a shell script that will execute one part on local computer and the other part when you're logged into that server?
For example, I want to log in, do something, add some file, and then I want to list everything on that server.
ssh something#something
I enter password
Then list files from server.
You can just add a command to end of the ssh command; for example:
ssh username#host ls
will run ls on the server, instead of giving you a login shell.

Automatically open terminal when running bash script

To get the script to run in terminal, I have to select the option to open in terminal and write sh script name.shIs there a way I can reduce that to a single step, i.e. a launcher that automatically opens the script in a terminal after logging as a root ? I've tried to look it up on Google, but I haven't found any useful advice (perhaps I'm not executing the search properly).
I think what you mean is running your script as start up script. In that case place the script you want to run in the /etc/init.d directory and make the script executable with command chmod 755 scriptname.sh.
See the below related threads for more information
https://askubuntu.com/questions/290099/how-to-run-a-script-during-boot-as-root
How to run a shell script at startup
EDIT:
if you want to run your script after your login is successful then you need to place your script in ~/.bash_profile. See this related post
How do you run a script on login in *nix?

Run script on two machines

I have a shell script that I need to automate with cron. At our office, there is a specific machine that I must log in to in order to use cron. My problem is, that the script I have written interacts with git, using git commands to pull code and switch branches. The machine where I am able to schedule cron jobs and the script is being run from does not have git on it. I have a separate machine that I log in to when I am using git. Is there an easy way for me to run my script from the cron system and run the git part from the git system?
UPDATE: I am still interested if this can be done, but my team has acquired a new machine that we will set up however we choose, meaning that it will have cron and git. Thanks for any ideas
As some people have mentioned above, ssh is the way to do this. This is a bash line that I use a lot in my job, for gathering data from other servers:
ssh -T $server -l username "/export/home/path/to/script.sh $1 $2" 1>traf1.txt 2>/dev/null
The above code sample will connect to the ip $server, as user username and run the script script.sh, passing it the parameters $1 and $2. Instead of redirection you could also assign the command output to a variable, just as you would with any other command in your script.
PS: Please note that in order for the above to work, you will need to set up passwordless login between those machines. Otherwise your script will break to request password input, which is most probably not the desired behavior.

Resources