Skip to content

Instantly share code, notes, and snippets.

@lherich
Last active April 13, 2021 10:49
Show Gist options
  • Save lherich/69c258786f32e85bad4c to your computer and use it in GitHub Desktop.
Save lherich/69c258786f32e85bad4c to your computer and use it in GitHub Desktop.
Automatically set random background color in iTerm depending on ssh host
#!/bin/bash
#!/bin/bash
#
# ssh into a machine and automatically set the background
# color of Mac OS X Terminal depending on the hostname.
#
# Installation:
# 1. Save this script to /usr/local/bin/ssh-host-color
# 2. chmod 755 /usr/local/bin/ssh-host-color
# 3. alias ssh=/usr/local/bin/ssh-host-color
# 4. export PRODUCTION_HOST="<hostname_production_server>"
# 5. Configure your host colors below.
#
# Taken from http://talkfast.org/2011/01/10/ssh-host-color
# Taken from https://gist.github.com/st3v/5008165
#
set_term_bgcolor(){
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "iTerm2"
tell current session of current window
set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255)), 0}
end tell
end tell
EOF
}
if [ "$@" = "$PRODUCTION_HOST" ]; then
set_term_bgcolor 255 220 220
else
set_term_bgcolor $(($RANDOM % 75 + 180)) $(($RANDOM % 75 + 180)) $(($RANDOM % 75 + 180))
fi
ssh $@
set_term_bgcolor 247 247 247
clear
Copy link

ghost commented Apr 12, 2021

For new version of iTerm

tell application "iTerm2"
  tell current session of current window
    set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255)), 0}
  end tell
end tell

... I'm using this to simply set random dark colour for my terminals:

#!/bin/bash
set_term_bgcolor() {
  local R=$1
  local G=$2
  local B=$3
  /usr/bin/osascript <<EOF
tell application "iTerm2"
  tell current session of current window
    set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255)), 0}
  end tell
end tell
EOF
}

set_term_bgcolor $(($RANDOM % 25)) $(($RANDOM % 25)) $(($RANDOM % 25))

@lherich
Copy link
Author

lherich commented Apr 13, 2021

@tpokki-igt: Thanks for your input, I adjusted my gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment