When I ssh into a remote production server I would like the colour scheme of my terminal window to change to something brigh and scary, preferably red, to warn me that I am
Xterm-compatible Unix terminals have standard escape sequences for setting the background and foreground colors. I'm not sure if Terminal.app shares them; it should.
case $HOSTNAME in
live1|live2|live3) echo -e '\e]11;1\a' ;;
testing1|testing2) echo -e '\e]11;2\a' ;;
esac
The second number specifies the desired color. 0=default, 1=red, 2=green, etc. So this snippet, when put in a shared .bashrc, will give you a red background on live servers and a green background on testing ones. You should also add something like this to reset the background when you log out.
on_exit () {
echo -e '\e]11;0\a'
}
trap on_exit EXIT
EDIT: Google turned up a way to set the background color using AppleScript. Obviously, this only works when run on the same machine as Terminal.app. You can work around that with a couple wrapper functions:
set_bg_color () {
# color values are in '{R, G, B, A}' format, all 16-bit unsigned integers (0-65535)
osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"
}
sshl () {
set_bg_color "{45000, 0, 0, 50000}"
ssh "$@"
set_bg_color "{0, 0, 0, 50000}"
}
You'd need to remember to run sshl instead of ssh when connecting to a live server. Another option is to write a wrapper function for ssh that scans its arguments for known live hostnames and sets the background accordingly.
/.bashrc
I needed the same thing, something to make me aware that I was on a Staging or Production server and not in my Development environment, which can be very hard to tell, especially when in a Ruby console or something.
To accomplish this, I used the setterm
command in my server's ~./bashrc
file to inverse the colours of the terminal when connecting and restore the colours when exiting.
# Inverts console colours so that we know that we are in a remote server.
# This is very important to avoid running commands on the server by accident.
setterm --inversescreen on
# This ensures we restore the console colours after exiting.
function restore_screen_colours {
setterm --inversescreen off
}
trap restore_screen_colours EXIT
I then put this in all the servers' ~/.bashrc
files so that I know when my terminal is on a remote server or not.
Another bonus is that any of your development or devops team get the benefit of this without making it part of the onboarding process.
Works great.
You should change the color of username and host machine name.
add the following line to your ~/.bash_profile
file:
export PS1=" \[\033[34m\]\u@\h \[\033[33m\]\w\[\033[31m\]\[\033[00m\] $ "
The first part (purple colored) is what you're looking for.
Preview:
This is my preferred colors. You can customize each part of prompt's color by changing m
codes (e.g. 34m
) which are ANSI color codes.
List of ANSI Color codes:
Combining answers 1 and 2 have the following:
Create ~/bin/ssh
file as described in 1 with the following content:
#!/bin/sh
# https://stackoverflow.com/a/39489571/1024794
log(){
echo "$*" >> /tmp/ssh.log
}
HOSTNAME=`echo $@ | sed s/.*@//`
log HOSTNAME=$HOSTNAME
# to avoid changing color for commands like `ssh user@host "some bash script"`
# and to avoid changing color for `git push` command:
if [ $# -gt 3 ] || [[ "$HOSTNAME" = *"git-receive-pack"* ]]; then
/usr/bin/ssh "$@"
exit $?
fi
set_bg () {
if [ "$1" != "Basic" ]; then
trap on_exit EXIT;
fi
osascript ~/Dropbox/macCommands/StyleTerm.scpt "$1"
}
on_exit () {
set_bg Basic
}
case $HOSTNAME in
"178.222.333.44 -p 2222") set_bg "Homebrew" ;;
"178.222.333.44 -p 22") set_bg "Ocean" ;;
"192.168.214.111") set_bg "Novel" ;;
*) set_bg "Grass" ;;
esac
/usr/bin/ssh "$@"
Make it executable: chmod +x ~/bin/ssh
.
File ~/Dropbox/macCommands/StyleTerm.scpt
has the following content:
#https://superuser.com/a/209920/195425
on run argv
tell application "Terminal" to set current settings of selected tab of front window to first settings set whose name is (item 1 of argv)
end run
Words Basic, Homebrew, Ocean, Novel, Grass
are from mac os terminal settings cmd,:
Put following script in ~/bin/ssh
(ensure ~/bin/
is checked before /usr/bin/
in your PATH):
#!/bin/sh
HOSTNAME=`echo $@ | sed s/.*@//`
set_bg () {
osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"
}
on_exit () {
set_bg "{0, 0, 0, 50000}"
}
trap on_exit EXIT
case $HOSTNAME in
production1|production2|production3) set_bg "{45000, 0, 0, 50000}" ;;
*) set_bg "{0, 45000, 0, 50000}" ;;
esac
/usr/bin/ssh "$@"
Remember to make the script executable by running chmod +x ~/bin/ssh
The script above extracts host name from line "username@host" (it assumes you login to remote hosts with "ssh user@host").
Then depending on host name it either sets red background (for production servers) or green background (for all other). As a result all your ssh windows will be with colored background.
I assume here your default background is black, so script reverts the background color back to black when you logout from remote server (see "trap on_exit").
Please, note however this script does not track chain of ssh logins from one host to another. As a result the background will be green in case you login to testing server first, then login to production from it.
Why not just changing the shell prompt whenever you are logged in via SSH? There are usually specific shell variables: SSH_CLIENT, SSH_CONNECTION, SSH_TTY