loading local shell aliases to ssh session dynamicaly

24

20

When I log in to some machine using ssh I wish my aliases and functions are there. In other words, log in to some machine I wish I can use my command shortcuts.

I need it be dynamic, every time I'm log in I wish I have updated aliases.

Notes: Very often it is first time log in, without knowing machine and files there. Sometimes it is single log in. Just one time for that machine. It need to be cleaned afterwards, previous configuration has to be restored too.

Tomek Wyderka

Posted 2012-11-08T18:23:24.570

Reputation: 501

Answers

18

You can temporarily copy your .bashrc to your remote machine with another name. For example, using .bashrc_temp:

user@local$ scp .bashrc user@remote:~/.bashrc_temp

Afterwards you can log into the remote machine:

user@local$ ssh user@remote

and source the file .bashrc_temp:

user@remote$ source ~/.bashrc_temp

Now you are able to use your .bashrc and your functions. When you are finished with your work you can remove the file ~/.bashrc_temp on the remote machine and logout.

The copying of the file and the login to the remote machine may be achieved with a bash function:

# copy the .bashrc to the remote machine 
# and log into the remote machine.
# parameter $1: user@remote
function s() {
  scp ~/.bashrc $1:~/.bashrc_temp
  ssh $1
}

Update:

You may also consider to copy the .bashrc to /tmp on your remote machine and source /tmp/.bashrc_temp.

Update 2:

You can log into the remote machine by using ssh -t. This will automatically use your temp .bashrc. Updated function s():

function s() {
  scp ~/.bashrc $1:/tmp/.bashrc_temp
  ssh -t $1 "bash --rcfile /tmp/.bashrc_temp ; rm /tmp/.bashrc_temp"
}

jens-na

Posted 2012-11-08T18:23:24.570

Reputation: 306

5Almost all answers (including this one) use predictable filename in /tmp/. This can trivially be exploited by any other user to run any code as a user which logs in. This should use mktemp to make sure the temporary file has an unique name. – Tometzky – 2018-01-22T23:57:18.880

This is definitely what I'm looking for. Thanks! But is it possible to do it more functional? In one step? Because now we need 3 steps: log in, source, delete before logout. – None – 2012-11-09T03:22:35.217

OK, I see now after your update. Great. One more fix: "bash --rcfile /tmp/.bashrc_temp; rm /tmp/.bashrc_temp" – None – 2012-11-09T15:29:41.233

I have added your fix to my update 2. Thanks. – None – 2012-11-09T16:23:10.650

9

jens-na provided an excellent answer. I spent a bit of time and re-worked it a bit to work a teeny bit better. This way, you can pass along any parameter to SSH, such as port numbers. The difference is that it uses the ssh command to upload the .bashrc file, instead of scp, which uses different command parameter names.

You'll also notice that it uploads a different file, .bashrc_remote, so that you can select exactly what you want to source on remote servers, instead of everything

sshs() {
        ssh $@ "cat > /tmp/.bashrc_temp" < ~/.bashrc_remote
        ssh -t $@ "bash --rcfile /tmp/.bashrc_temp ; rm /tmp/.bashrc_temp"
}

Run it as follows:

sshs user@server

The name 'sshs' is for "SSH Source". Use ssh when you don't want to source, and use sshs when you do.

https://gist.github.com/jonahbron/5549848

Jonah

Posted 2012-11-08T18:23:24.570

Reputation: 805

2

I've taken this answer and iterated on it over a few weeks. The result is a full blown tool that crushes the problem: https://github.com/Russell91/sshrc

– RussellStewart – 2014-09-12T07:35:31.060

1@Jonah can you explain to me why you used the ${*:1} instead of $@. – RussellStewart – 2014-09-21T19:37:00.973

@singular Strictly ignorance :) – Jonah – 2014-09-29T16:52:01.197

That's useful. The same set of parameters, not need to add entry to ~/.ssh/config just to specify different port no! – Tomek Wyderka – 2013-05-10T12:02:16.827

Since it is the same command name, maybe it is possible to do it in one run, and type password only once... Unfortunately joining your commands into one reports: Pseudo-terminal will not be allocated because stdin is not a terminal – Tomek Wyderka – 2013-05-10T12:04:17.697

@TomekWyderka Yeah, I tried for about 20 minutes to get it down to one command, but didn't find a way. Perhaps a person more BASH-savvy than I could figure it out, but it doesn't appear to be possible. – Jonah – 2013-05-10T21:40:50.790

9

I think sshrc is what you're looking for: https://github.com/Russell91/sshrc

sshrc works just like ssh, but it also sources ~/.sshrc after logging in remotely.

$ echo "echo welcome" > ~/.sshrc
$ sshrc me@myserver
welcome

$ echo "alias ..='cd ..'" > ~/.sshrc
$ sshrc me@myserver
$ type ..
.. is aliased to `cd ..'

You can use this to set environment variables, define functions, and run post-login commands. It's that simple, and it won't impact other users on the server - even if they use sshrc too. For more advanced configuration, continue reading.

RussellStewart

Posted 2012-11-08T18:23:24.570

Reputation: 201

Nice. I like the xxd hack! – Tomek Wyderka – 2014-09-12T17:55:21.750

@Tomek - yea the xxd hack is completely awesome. I really tried to get the job done with scp. Then with piping into ssh. It just can't be done without 2 calls to the server - 2 password entries - 2 round trips. It was unacceptable. So I did the unthinkable. – RussellStewart – 2014-09-12T17:57:09.917

I have a bug. sshrc hangs without response. Can you put some debug code (can be in the comments) into sshrc so I can trace it. – Tomek Wyderka – 2014-09-13T05:31:02.817

Interesting. It's just a bash script so usually any errors or warnings thrown will be printed to the screen automatically. How many bytes are in your .sshrc.d? Also, just type vim $(which sshrc) and you can see the bash file. You can add echo commands after each line to see where it hangs. – RussellStewart – 2014-09-13T05:34:38.780

5

Not sure of the limitations, but I was able to get this to work with something like:

function ssh_with_rc() {
   RC_DATA=`cat ${HOME}/.bashrc | base64 -w 0`
   ssh -t $@ "echo \"${RC_DATA}\" | base64 --decode > /tmp/${USER}_bashrc; bash --rcfile /tmp/${USER}_bashrc; rm /tmp/${USER}_bashrc"
}

alias ssh="ssh_with_rc"

Brad

Posted 2012-11-08T18:23:24.570

Reputation: 51

This is the best answer for me because it's a single ssh command, no scp stuff. The only remaining improvement for me would be to find a way to avoid the RC_DATA variable. – Sridhar Sarnobat – 2017-07-15T01:26:28.507

2

This is what I came up with. It allows you to maintain a normal rc file, but also do everything in one ssh connection (ie only need to login once as opposed to doing an scp first).

#copies some environment over to the remote machine
function ssh() {
  /usr/bin/ssh -t $* "echo `base64 -i ~/bin/remote_ssh_env.sh` | base64 --decode > /tmp/remote_ssh_env.sh; bash --rcfile /tmp/remote_ssh_env.sh"
}

I'm not sure how big that rc file can be though, since it might max out at some point.

mlathe

Posted 2012-11-08T18:23:24.570

Reputation: 123

I had to wrap part of the command in single quotes to get this working right. echo '`base64....sh`' -- after that, it worked beautifully. Thanks!! – K Robinson – 2015-05-12T17:25:51.433

If size becomes an issue, you could pipe the file contents through a compression utility like gzip before base64 encoding. – K Robinson – 2015-05-12T17:29:35.093

2

I think that https://github.com/fsquillace/pearl-ssh does what you need.

I wrote it long time ago before sshrc was born and it has more benefits compared to sshrc:

  • It does not require dependencies on xxd for both hosts (which can be unavailable on remote host)
  • Pearl-ssh uses a more efficient encoding algorithm
  • It is just ~20 lines of code (really easy to understand!)

For instance:

$> echo "alias q=exit" > ~/.config/pearl/sshrc
$> ssh_pearl myuser@myserver.com
myserver.com $> q
exit

user967489

Posted 2012-11-08T18:23:24.570

Reputation: 21

1

One option to make it work with a single SSH session is to use a variable to store your bash file in rather than copy it.

$ mybash=`cat mybash`
$ ssh -t 127.0.0.1 "echo $mybash > /tmp/mybash; bash --rcfile /tmp/mybash ; rm /tmp/mybash"

Seems to work for me.

gibsop1

Posted 2012-11-08T18:23:24.570

Reputation: 11

1

I was looking to solve a problem like yours, and realized what I was really looking for was sshfs. Maybe you can use it too?

My problem was that when ssh'ing, I wished to keep my colors, aliases, functions and scripts while working remote.

http://fuse.sourceforge.net/sshfs.html

Deifyed

Posted 2012-11-08T18:23:24.570

Reputation: 11

Lol, this never occurred to me either. It's quite a creative solution. – Sridhar Sarnobat – 2017-07-15T01:17:25.083