I'm frequently logging into different servers from my os x terminal window.
I'd like to assign a color scheme for different hosts so that my terminal windows are easier to tell apart. Can this be done automatically?
Here's a complete solution. Keep a list of your servers ip addresses and/or domains and the colors you want for them in in ~/.server_colors:
192.168.122.102,Red Sands 192.168.122.103,Ocean www.foo.com,Grass foo.com,Grass
Then add this line to ~/.profile to hijack the ssh command:
alias ssh="~/bin/safe_ssh $1"
Then compare whatever's after the @ in your ssh target to your list. If there's a match, run an AppleScript to change the screen to the corresponding color. Here's ~/bin/safe_ssh:
#!/bin/bash ip=`echo $1 | cut -d"@" -f2` match=`cat ~/.server_colors | grep $ip | wc -l` if [ $match -gt 0 ] then color=`cat ~/.server_colors | grep $ip | cut -f2 -d","` osascript ~/bin/change_terminal_color.scpt "$color" 2>/dev/null fi /usr/bin/ssh $1
And last, here's ~/bin/change_terminal_color.scpt
on run argv tell application "Terminal" to set current settings of selected tab of window 1 to (first settings set whose name is (item 1 of argv)) end run
I took most of this code from this blog post.
The solution of @muirbot works really well for me. I made some little improvements to that. I'll add it below his post once I have enough reputation.
Replace the line
ip=echo $1 | cut -d"@" -f2
with ip=echo $@ | grep -Eio [[:alnum:]_.-]+@[[:alnum:]_.-]+ | cut -d@ -f2
This change allows for giving additional arguments to your ssh command like "ssh -p 1111 userName@host"
The regular expression allows for simple ipv4 addresses and domain names.
To further support multiple arguments change the last line into
/usr/bin/ssh $@
Update 2021-03-03:
When you are having domains like example.com and www.example.com with different colors, then the script will not work as expected.
Make sure you place the shortest URL on top and then change
color=cat ~/.server_colors | grep $ip | cut -f2 -d","
to color=cat ~/.server_colors | grep -m 1 $ip | cut -f2 -d","
The grep -m 1 command will stop when it finds the first match.
Yes.
Either you use e.g. "screen" and customize it: http://www.slac.stanford.edu/comp/unix/package/epics/extensions/iocConsole/screen.1.html
Or you manage to do it on your SSH Client, if possible.
You could also try this: http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/
I was just looking for the same thing and found this article:
http://akrabat.com/php/osx-terminal-colours/
It uses a php script to change the terminal colors by applescript. You can set up mappings of different colors for each server. Works great for me, though feel the urge to rewrite the php stuff into ruby :)
fk
I use a script that launches SSH in xterm windows with different bg/fg colors. It selects the colors based on the hostname's hash from a color span so that no configuration is needed.
The script is written in Ruby: https://github.com/mickeyil/ssx
If you are using iTerm2, create a file named ~/bin/ssh-host-color.sh
with the contents from https://gist.github.com/jbochi/31f118b8ae2882a2c90fa46c46509b57:
set_term_bgcolor(){
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "iTerm"
tell the current window
tell the current session
set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))}
end tell
end tell
end tell
EOF
}
if [[ "$@" =~ "production.example.com" ]]; then
set_term_bgcolor 40 0 0
elif [[ "$@" =~ "qa.example.com" ]]; then
set_term_bgcolor 0 40 0
fi
trap "set_term_bgcolor 0 0 0" EXIT
ssh $@
And add the following line to your ~/.aliases.sh
file:
alias ssh="~/bin/ssh-host-color.sh $@"