Skip to content

Instantly share code, notes, and snippets.

@paugustyn-cbsi
Created July 10, 2021 06:00
Show Gist options
  • Save paugustyn-cbsi/b890b13ec2fcf397b7252047b7f1ed83 to your computer and use it in GitHub Desktop.
Save paugustyn-cbsi/b890b13ec2fcf397b7252047b7f1ed83 to your computer and use it in GitHub Desktop.
Configure iterm2 to switch profiles for ssh connections (badge and profile)

Configuration

  • requires env file (associative array) with commands to execute
  • requires to add two profiles with custom background colors to iTerm2
  • both profiles should have their badge set to
 \(user.badge)
aws-dev=ssh -i ~/.ssh/aws-dev [email protected]
aws-ent=ssh -i ~/.ssh/aws-ent [email protected]
aws-prod=ssh -i ~/.ssh/aws-prod [email protected]
#!/bin/zsh
if [ $# -eq 0 ]
then
echo No arguments supplied
exit 1
fi
config=~/.ssh/env
declare -A MYMAP
if [[ -f "$config" ]]; then
while IFS= read -r line; do
MYMAP[${line%%=*}]=${line#*=}
done < "$config"
else
echo Missing configuration file
exit 1
fi
on_exit () {
# back to default profile with its badge
echo "\033]50;SetProfile=Default\a"
}
# want traps so we can restore default profile after we are done
trap on_exit INT
trap on_exit EXIT
trap on_exit SIGINT
trap on_exit ERR
# setting variables for iTerm2
iterm2_set_user_var (){
printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
}
# change profile of iTerm2
change_profile(){
# change profile
echo "\033]50;SetProfile=$1\a"
}
cmd=${MYMAP[$1]}
if [[ -z "$cmd" ]]; then
echo $1 not congfigured
exit 3
fi
echo $cmd
case $1 in
*"dev"*)
change_profile Dev
;;
*"ent"*|*"prod"*)
change_profile Prod
;;
esac
iterm2_set_user_var badge "$1"
eval $cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment