Small ssh improvement for Mac

Here’s a little script to make ssh a bit nicer on Mac, especially if you also occasionally use XQuartz.

#!/bin/sh

# This is a simple script to make ssh less annoying on Mac.  In particular:

#  - If XQuarts is not running, don't cause it to auto start

#  - If the user explicitly gives -x or -X options, honor that behavior

#  - Always reset the title bar if we're running in Apple Terminal

#

# Add this line to your ~/.bashrc

#   alias ssh="${HOME}/bin/ssh"

#

# 20140820 - Created by Danny Sung <danny@dannysung.com>



# Use 'which ssh' before installing this script to ensure you find the path to the real ssh program

ORIG_SSH=/usr/bin/ssh



X11_MODE=auto

NEW_ARGS=()

while [ $# -gt 0 ]; do

case "$1" in

-x)

X11_MODE=off

;;

-X)

X11_MODE=on

;;

esac

# Preserve all original arguments

NEW_ARGS+=("$1")

shift

done



if [ "$X11_MODE" == "auto" ]; then

# Xquartz defaults to using pipes, so we can detect if it's running this way...

xisrunning=$(netstat -n | grep -- "$DISPLAY" | wc -l)

if [ $xisrunning -ge 2 ]; then

NEW_ARGS=("-X" "${NEW_ARGS[@]}")

else

NEW_ARGS=("-x" "${NEW_ARGS[@]}")

fi

fi


${ORIG_SSH} "${NEW_ARGS[@]}"

retval=$?


# Make sure to restore Mac Terminal title

if [ "$TERM_PROGRAM" = "Apple_Terminal" ]; then

printf '\e]0;\a'

fi

exit $retval


Leave a Reply

Your email address will not be published. Required fields are marked *