-
-
Save sickerin/8cfff6d361d43f2bee869be3d547b187 to your computer and use it in GitHub Desktop.
Iterm2: Show both IP_ADDRESS and HOSTNAME as a badge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script that updates the iTerm Badge with the hostname of the server that you are | |
# connecting to with ssh. | |
# | |
# Instructions: | |
# - Put this script in ~/bin/ssh (this will override the default ssh binary) | |
# - Run 'chmod +x ~/bin/ssh' to give execution permission to the script | |
# - Open iTerm\Preferences\Profiles, select your profile and put '\(user.current_ssh_host)' in the Badge text box | |
# - Enjoy! | |
# | |
# Troubleshoot issues: | |
# - If it's not working, make sure your shell is white-listed in the script (see $PARENT_COMMAND in the script) | |
# | |
# Credits: inspired by https://engineering.talis.com/articles/bash-osx-colored-ssh-terminal/ | |
# https://gist.github.com/danydev/bd09343bc1521ca2cac0a9f1ab611cc2 | |
iterm2_set_user_var () { | |
PARENT_COMMAND=$(ps -o comm= $PPID) | |
# Avoid to do send the command when ssh is not run by the shell | |
# If the line below doesn't print, echo the line below to debug | |
# echo $PARENT_COMMAND | |
if [ "$PARENT_COMMAND" = "bash" ] || [ "$PARENT_COMMAND" = "-zsh" ]; then | |
printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64 | tr -d '\n') | |
fi | |
} | |
on_exit () { | |
iterm2_set_user_var current_ssh_host "" | |
iterm2_set_user_var current_ssh_ip "$IP_ADDRESS" | |
} | |
trap on_exit EXIT | |
HOSTNAME=`echo $@ | sed s/.*@//` | |
IP_ADDRESS=$(grep -w "$HOSTNAME" ~/.ssh/config | awk '{print $NF}') | |
# echo $HOSTNAME | |
# echo $IP_ADDRESS | |
# # If the IP address is not found, use the original hostname | |
# if [ -z "$IP_ADDRESS" ]; then | |
# IP_ADDRESS="$HOSTNAME" | |
# fi | |
iterm2_set_user_var current_ssh_host "$HOSTNAME" | |
iterm2_set_user_var current_ssh_ip "$IP_ADDRESS" | |
/usr/bin/ssh "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note to self: Avoid echoing to the terminal, it could cause some unexplained issues.
https://stackoverflow.com/questions/8170436/git-remote-error-fatal-protocol-error-bad-line-length-character-unab?page=1&tab=scoredesc#tab-top