2013年10月26日 星期六

An ssh session management trick with roxterm.


[2015/1/6] Edit: some modification of the script. 

I used putty/pietty and putty session manager to manage and organize ssh profiles/sessions in Windows. The main reasons of choosing this combination are:



  1. Clean: As some of fancy multi-session managers, there are full of bars, trays and buttons occupy the space which should be used for terminal.
  2. Profile management: several items in the list with different kinds, an alias/nickname of each is really necessary.
  3. Connection management: tons of connections to different servers with difference purposes: development, testing, or production, a misundestanding will cause disaster.
As using Ubuntu 13.10 as my desktop environment in workplace, some of the tricks would be quite helpful:
  • ssh config: set the alias name of profile in ssh config file with a group manner. For instance:
Host prod.svr1
    HostName path.to.svr.one
    User svr1user
    IdentityFile svr1ID
Host test.svr2
    HostName path.to.svr.two
    User svr2user
Host prod.svr3
    HostName path.to.svr.three
    User svr3user
    IdentityFile svr3ID
Then it could be fetched as:
$ssh prod.svr1
to connect the target production server. Refer the man page of ssh_config for further usage of ssh config file. Moreover, by the convenience of auto-completion in bash, we could use it as list/search feature:
$ssh prod.<TAB>
$ssh prod.
prod.svr1   prod.svr3
  • Characteristic of connections: A common problem of connection management is there are lots of connections exist at the same time, which have the similar looking; it is dangerous enough to switch between the production and testing ones. My solution is changing the looking for each kind of remote sites to regconize them clearly. The naming manner in ssh config file make sense for regular expression for scripting; and roxterm provides entries through dbus to apply the profile and color scheme instantly:
dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.SetColourScheme string:$ROXTERM_ID string:GTK
will change the color scheme to the one named as "GTK" of current terminal applying. Then our little script wrapper like:
#!/bin/bash
function SetColor
{
# Function with first argument be the name of color scheme
dbus-send --session /net/sf/roxterm/Options net.sf.roxterm.Options.SetColourScheme string:$ROXTERM_ID string:$1
}
for [ argu in $@ ]; do
    # For the usage xxx@bind_addr
    arguhost=${$argu#*@}

    ( [[ $arguhost =~ ^- ]] ) || # Skip the ssh arguments
    ( [[ $arguhost =~ ^prod. ]] && SetColor Prod) || # Production env. connection
    ( [[ $arguhost =~ ^test. ]] && SetColor Test) || # Testing env. connection
    SetColor Remote
done

ssh $@; SetColor Normal

Save it in somewhere, say $HOME/bin/sshell.sh, and make an bash alias to wrap ssh command:
alias ssh="$HOME/bin/sshell.sh". The alias makes it easy to do the connection as usual, and keeps the bash-complement feature. Of course more complicate settings could be applied, such as fonts, windows title, shortcuts... etc. Complete usage could be found in the guide of roxterm.


Now connecting to remote server through ssh will change the looking to keep us in mind what situation we are in and how caution should us be.





沒有留言:

張貼留言