Last Updated: October 18, 2020
·
39.61K
· itseranga

Change terminal color when SSH from OS-X

My terminal

Picture

Background

  • In day today developments and deployments we have to ssh into different machines/servers
  • For an instance we may have to ssh into

    1. Production servers
    2. Staging servers
    3. Amazon boxes
    4. Local testing servers etc..
  • Usual behaviour is open new terminal tab to and starting ssh session

  • Sometimes we may have multiple ssh session in different terminal tabs simultaneously

  • In these kind of situations its bit confuse to identifies where I am(which host currently I am in)

  • To solve this confusing issue we can use different terminal themes(terminal colors) for different ssh sessions

iTerm profiles with oh-my-zsh

  • In my scenario I'm using different iTerm profiles with different themes(colors)
  • I'm loading these terminal profiles when ssh into different environments
  • Its(terminal profile selecting) done by using custom oh-my-zsh command(shell script)
  • In my scenario I'm using three different iTerm profiles(with different colors) for ssh session

    1. Production ssh session - dark red color
    2. Staging ssh session - dark purple color
    3. Other ssh session - dark blue color
  • You can use different iTerm themes to obtain different terminal colors - iTerm themes

  • Following are the steps that need to follow in order to change the terminal color when SSH'ing

1. Create iTerm terminal profiles

  • Goto iTerm preferences and create profiles according to your ssh environments
  • In my scenario I have 4 iTerm profiles
  • Default profile uses in local terminal and other three profiles(Production, Staging and Other) use in different ssh sessions

Picture

2. Create custom shell script

  • Goto ~/.oh-my-zsh/custom and create a file iTrem2-ssh.zsh

Picture

  • Put following content into iTrem2-ssh.zsh file
function tabc() {
  NAME=$1; if [ -z "$NAME" ]; then NAME="Default"; fi
  # if you have trouble with this, change
  # "Default" to the name of your default theme
  echo -e "\033]50;SetProfile=$NAME\a"
}

function tab-reset() {
    NAME="Default"
    echo -e "\033]50;SetProfile=$NAME\a"
}

function colorssh() {
    if [[ -n "$ITERM_SESSION_ID" ]]; then
        trap "tab-reset" INT EXIT
        if [[ "$*" =~ "production*" ]]; then
            tabc Production
        elif [[ "$*" =~ "staging*" ]]; then
            tabc Staging
        else
            tabc Other
        fi
    fi
    ssh $*
}
compdef _ssh tabc=ssh

alias ssh="colorssh"
  • colorssh() function selecting different terminal profile according to ssh'ing host
  • It selects

    1. Production profile when ssh to production server (ssh eranga@production_box)
    2. Staging profile when ssh to staging server(ssh eranga@staging_box)
    3. Other profile when ssh to any other server(local server, amazon box etc)

    Change tabc <profile-name> according to your profile names

  • tab-reset() function reset the terminal profile to Default profile when exit from the ssh session

  • alias ssh="colorssh" creates an alias to ssh(when execute ssh from the terminal it calls to colorssh function)

  • You can get this code from my github

Example terminal session

  • Default terminal

Picture

  • SSH into production (ssh eranga@production_box1)

Picture

  • SSH into staging (ssh eranga@staging_box1)

Picture

  • SSH into other server (ssh eranga@10.2.4.201)

Picture

Reference

1 Response
Add your response

Thanks, I failed to setup Automatic Profile Switching and this is what I was looking for.

over 1 year ago ·