What is it?
edit: I've experimented with different ways to do this, and will likely make an update post describing a new method.
I used to work for a dedicated server hosting company. So naturally, we dealt with lots of servers every day, and one of the guys I work with said something like “I wish all my aliases would follow me to other servers”. That sounded like a cool idea to me, and basically explains what the script does. It will open a persistant ssh connection to the server you specify, use scp to copy $bashrc_remote to /tmp on the server, initiate an actual ssh session invoking bash with the –rcfile option to automatically source the bashrc_remote file, and then delete it when you exit the remote shell. Since I’m using persistant ssh connections, this will only ask you for the remote password once. Any additional ssh connections to the same server/user/port while the script is running automatically re-uses the connection.
How do I use it?
Download the script from below. You can either use the included bashrc-remote which has some useful aliases added to it already(including a lot of plesk-specific ones that only get sourced if plesk’s installed), or you can choose to use your own bashrc. Open up remotebashrc.sh in a text editor, and change the bashrc_remote variable to the location of the bashrc you want to use. By default, it’s set to ~/bashrc-remote because I like to keep a separate .bashrc on my local shell with functions and aliases I don’t want on other servers.
You can run it like this: remotebashrc user port host
./remotebashrc.sh root 22 10.0.0.2
Until I clean it up, you can add alias like this to your .bashrc file:
alias root=’~/remotebashrc.sh root 22′;
Then to connect to a remote server using the root server, all you need to do is type ‘root myserver.com’
Update 08/10/2011: Source is available on github now. I haven’t updated it for a long time, and don’t actively use it anymore. If there’s any interest in it, I’d be more than happy to continue developing it.
You can check this script out from a public read-only git repo by issuing this command:
git clone git://github.com/justyns/remotebashrc.git
And to update, just type:
11/30/2009 – Initial commit to svn 12/01/2009 – by default, the script only uploads the bashrc file now and tells you to source it manually. bashrc-remote also automatically tries to delete itself after you source it now 12/02/2009 – added some new aliases to the bashrc-remote file (lla, llah, :q, :q!, and :wq) – added a couple commands to the end of bashrc-remote to display the servers uptime/load and total processes when you first log in – added a line to show the currently running services.. kind of.. It uses an array of process names, and pidof to see if they’re running – if ~/.bashrc exists, source it before doing anything else. bashrc_remote will overwrite anything in it though – changed the default PS1 prompt. – added /sbin and /usr/sbin to PATH – added a $debug option to remotebashrc.sh 02/14/2010 – changed $bashrc_remote to look for bashrc-remote in the same directory as remotebashrc.sh instead of ~/
- accept parameters like [email protected]
- random_file doesn’t always work. $RANDOM returns nothing some times.
- check to see if it’s a freebsd box before trying to ssh in.. /bin/bash doesn’t seem to be installed by default
- add some logging to log each server the script connects to
- run a ‘ls $random_file’ on the remote server before uploading it to make sure we don’t overwrite anything
- error checking to see if we actually got connected and didn’t use a bad password or something