Is my terminal connected to production?

If you have access to production, or even a staging environment, you may sometimes get a wave of paranoia when you are executing commands in your terminal. Or worse, you may have accidentally run something you should not in your production environment. rm -rf / anyone?

I use iTerm for my Mac, and it has profile support so I can change the background colour of my terminal. However, I don’t want to have to remember to set the profile every time I SSH into a production box. Actually, more to the point, I know I will forget. Luckily, iTerm has a feature that lets you set the profile by sending an escaped string to it from the connected terminal.

Screen Shot 2015-03-19 at 12.47.06

First of all you need to add a profile. Here the steps I followed:

  1. In iTerm, go to menu item Profiles > Open Profiles…
  2. Click Edit Profiles
  3. Click the + icon in the bottom left to add a new profile
  4. Give the new profile a name like ‘Production’ and click on Colors tab
  5. Click the Background colour selector
  6. Choose a colour for the background of the terminal. I used a hex colour of #FFECEC which gives a subtle red/pink background (but you might want something more in-your-face!)
  7. Close the preferences window and the profiles window

Next you need to tell Iterm to use this profile when you log into a particular server. As mentioned above, to do this you need to get your server to print an escaped string of characters when you log in. To do this, simply add this snippet to your .bashrc or .bash_profile file:

if [ "$SSH_TTY" ]
then
  echo -e "\033]50;SetProfile=Production\a"
fi

You may need to change Production to the name of your profile. The last thing you need to do is get the server to reset the profile when you log out. This prevents your terminal looking like production when you’re actually on your local machine! To do this, add this snippet to the .bash_logout file on the server:

if [ "$SSH_TTY" ]
then
    echo -e "\033]50;SetProfile=\a"
fi

All done. Enjoy your colourful consoles!

Print Friendly

Leave a Reply