Skip to content

Instantly share code, notes, and snippets.

@pankesh
Forked from BugRoger/ssh-background
Last active October 29, 2019 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pankesh/6c24ca63a8769d34d050 to your computer and use it in GitHub Desktop.
Save pankesh/6c24ca63a8769d34d050 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Installation:
# 1. Save this script to /some/bin/cf-ssh-background
# 2. chmod 755 /some/bin/cf-ssh-background
# 3. alias cfssh=/some/bin/cf-ssh-background
# 4. Configure your host colors below.
set_color() {
local HEX_FG=$1
local HEX_BG=$2
local OPACITY=$3
local NAME=$4
local FG_R=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local FG_G=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local FG_B=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
local BG_R=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local BG_G=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local BG_B=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
local TITLE=`echo $NAME | sed 's/\('"$HOST_SUFFIX"'\)//g' | sed 's/\(.*@\)//g'`
/usr/bin/osascript <<EOF
tell application "iTerm2"
tell current window
tell current session
set foreground color to {$((FG_R)), $((FG_G)), $((FG_B))}
set background color to {$((BG_R)), $((BG_G)), $((BG_B))}
set transparency to "$OPACITY"
set name to "$TITLE"
end tell
end tell
end tell
EOF
}
# Trap break and set back the color to original
trap ctrl_c INT
function ctrl_c() {
# reset back to original color
set_color 00ff00 000000 0.1
}
#Production services
if [[ "$@" =~ .*production ]]; then
set_color FFFFFF FF0000 0.1 $@
fi
cf ssh $@
# reset back to original color
set_color 00ff00 000000 0.1
#!/bin/bash
# Installation:
# 1. Save this script to /some/bin/ssh-background
# 2. chmod 755 /some/bin/ssh-background
# 3. alias ssh=/some/bin/ssh-background
# 4. Configure your host colors below.
# 5. Define HOST_SUFFIX to shorten FQDN hostnames
set_color() {
local HEX_FG=$1
local HEX_BG=$2
local OPACITY=$3
local NAME=$4
local FG_R=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local FG_G=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local FG_B=`echo $HEX_FG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
local BG_R=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$1 * 257)}'`
local BG_G=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$2 * 257)}'`
local BG_B=`echo $HEX_BG | sed 's/../0x&,/g' | awk -F "," '{printf("%d",$3 * 257)}'`
local TITLE=`echo $NAME | sed 's/\('"$HOST_SUFFIX"'\)//g' | sed 's/\(.*@\)//g'`
/usr/bin/osascript <<EOF
tell application "iTerm2"
tell current window
tell current session
set foreground color to {$((FG_R)), $((FG_G)), $((FG_B))}
set background color to {$((BG_R)), $((BG_G)), $((BG_B))}
set transparency to "$OPACITY"
set name to "$TITLE"
end tell
end tell
end tell
EOF
}
# Trap break and set back the color to original
trap ctrl_c INT
function ctrl_c() {
# reset back to original color
set_color 00ff00 000000 0.1
}
#Production servers
if [[ "$@" =~ mbomsg-..*-.*p ]]; then
set_color FFFFFF FF0000 0.1 $@
elif [[ "$@" =~ mspms-..-.*p ]]; then
set_color FFFFFF FF0000 0.1 $@
fi
ssh $@
# reset back to original color
set_color 00ff00 000000 0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment