SSH With Local Aliases

As developer oftentimes we need to manage so many servers. Staging servers there, DeploymentServer here, IssueTracking there, WebServer here, DatabaseServer there, FileServer here,  putYourFavouriteServersHere. I always use command aliases to boost those task, less type – productive more. Unfortunately the party suddenly over when i logged in to the remote machine. That aliases can’t be used anymore, i have to type the complete command again, and again, and again. What a boring lifes.

Lets automate this problem. This is the scenario : We want our local aliases  loaded  automagically to the remote machine everytime when we doing ssh. I’m using ubuntu here, my aliases are stored in ~/.bash_aliases by default. Lets the magics begin:

# Create the aliases. Here’s some of mine:
alias ssu='sudo su'
alias v="vim"
alias c="cd"
alias l='ls -hltX --color -h --group-directories-first'
alias d='cd /home/brain/Documents/sql/;psql -U some_db -h dev some_db_user -p 5433'
alias g='git'
alias gl='git log --pretty=format:"%ar -> %s"'
alias grep='grep --color=auto'
alias wget='wget --no-proxy'

# Create custom default ssh command, store it into /usr/bin/sshrc
set -x
scp ~/.bash_aliases $1:
ssh -t $1 "bash --rcfile .bashrc ; rm ~/.bash_aliases" -vv

Grant execute permission. Done !. After that, you just have to type sshrc user@remote-machine-ip and your local bash_aliases will loaded automagically to the remote machine.

# Test if magic works smoothly

sshrc brain@192.168.5.12
+ scp /home/brain/.bash_aliases brain@192.168.5.12
.bash_aliases 100% 1768 1.7KB/s 00:00
+ ssh -t brain@192.168.5.12 'bash --rcfile .bashrc ; rm ~/.bash_aliases' -vv
$ alias
alias ssu='sudo su'
alias v="vim"
alias c="cd"
alias l='ls -hltX --color -h --group-directories-first'
alias d='cd /home/brain/Documents/sql/;psql -U some_db -h dev some_db_user -p 5433'
alias g='git'
alias gl='git log --pretty=format:"%ar -> %s"'
alias grep='grep --color=auto'
alias wget='wget --no-proxy'

Yippie !,  Less type, productive more – Again.

Thanks To:
http://superuser.com/questions/503784/loading-local-shell-aliases-to-ssh-session-dynamicaly?answertab=votes#tab-top

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s