1

I'm managing a bunch of servers so I frequently have to ssh into them. I'd like to automatically run a basic custom script on whatever remote server after logging in. Say to set up some aliases, colors for vim, PS1, and what-not.

I don't want to configure every server with a startup script, I want this to live locally on my computer and just get executed on whatever server I ssh into.

There are similar questions but after executing the script ssh logs out. How to stay logged in?

This answer is quite near, yet the new bash -l does not seem to register the environment variables set like: ssh -t user@remote 'export PS1=myps1; bash -l'. Here, PS1 is not set in the new prompt, maybe because some remote .bashrc

How could this be possible?

  • 1
    This is what the .bashrc file is designed for. Can you explain why this does not fulfill your use case? – jeremysprofile Jul 26 '18 at 16:05
  • Using .bashrc would imply to configure dozens of remote server. I want to execute a script after loggin in to any server. I'd like to be able to change my prompt, for example. – arod Jul 26 '18 at 18:37
  • ... scp ~/.bashrc user@remote:~/.bashrc && ssh user@remote – jeremysprofile Jul 26 '18 at 18:43
0

It's quite simple:

ssh user@10.0.0.7 'bash -s' < local_bash_script.sh

After execution of the script local_bash_script.sh ssh-session will be closed immediately.

Update.

If you want to execute something on a remote server after ssh logging in, just create ssh rc file. Create on remote server rc file inside user's .ssh directory:

touch ~/.ssh/rc

Open the file with your favorite text editor, let it be nano for instance:

nano ~/.ssh/rc

and copy/paste content of the script file:

#/bin/sh

date
whoami
uname -r

exit 0

Every time during logging in the script will be executed (replace date, whoami, uname -r with your commands).

The script ~/.ssh/rc will be executed for a definite user who is owner of the directory where the rc file resides. If you wish to execute rc script for all of the users on remote server, create another global sshrc file:

/etc/ssh/sshrc

That's all.

  • If I do that, after executing the script ssh logs out. I wish to set to environment variables in the new server, even changing PS1. – arod Jul 26 '18 at 18:37
  • @arod I have updated my answer according to your comment. – Bob Jul 26 '18 at 19:06
  • Thanks Bob. But what I want to avoid is precisely creating any script on remote server (there are potentially hundreds). I want to have the script locally, and just running my local script on the remote server after login through ssh, mostly to set up PS1 to my liking and other alias, and things like that. – arod Aug 9 '18 at 14:55
0

Add the needed commands in your ~/.bashrc.

In case, you'd check the value of SSH_CLIENT/SSH_TTY to detect if you are connected via ssh.

if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then ... ; fi

Check How can I detect if the shell is controlled from SSH? for more details

  • Thanks but what I want to avoid is precisely creating any script on remote server (there are potentially hundreds). I want to have the script locally, and just running my local script on the remote server after login through ssh, mostly to set up PS1 to my liking and other alias, and things like that. – arod Aug 9 '18 at 14:56
0

You can check my answer here.

Since I'm using iTerm2, I came up with:

You can also have a script like ~/test.py

#!/usr/local/bin/python3

print("echo hello world")

And then execute this script as a co-process in iTerm2 menu Session->Run Co-Process, and specifying the script as ~/test.py

I think its necessary to make you script executable (though I haven't tried otherwise), chmod +x ~/test.py.

Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.