Skip to content

Instantly share code, notes, and snippets.

@yousan
Last active November 9, 2016 08:08
Show Gist options
  • Save yousan/6a1339f76475cec77df6fd905bc2a695 to your computer and use it in GitHub Desktop.
Save yousan/6a1339f76475cec77df6fd905bc2a695 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# @see http://qiita.com/miya0001/items/4dc5949dd792963ef578 Thanks!
# Usage: ssh user@example.com
# Automatically adds `-i user@example.com` argument for `ssh` command.
# test: bash -c 'source ssh_bash.bash; ssh user@example.com ls'
function ssh {
local HOSTSTR
SSH=`which ssh`
for i in "$@"
do
case $i in
-*) # this is option
# echo "this is option" "${i#*=}"
;;
#echo "${i#*=}"
# the first non-hyphoned argument must be a hostname (honto?
# $ ssh -h
# ssh: illegal option -- h
# usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
# [-D [bind_address:]port] [-E log_file] [-e escape_char]
# [-F configfile] [-I pkcs11] [-i identity_file] [-L address]
# [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
# [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
# [-w local_tun[:remote_tun]] [user@]hostname [command]
*)
if [ -z "$HOSTSTR" ]; then
HOSTSTR="${i#*=}"
fi
;;
esac
done
if [ -f ~/.ssh/"${HOSTSTR}" ]; then
$SSH -i ~/.ssh/${HOSTSTR} $@
else
$SSH $@
fi
# if [ -f ./.ssh-config ]; then
# /usr/bin/ssh -F ./.ssh-config $@
# else
# /usr/bin/ssh $@
# fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment