Skip to content

Instantly share code, notes, and snippets.

@snajpa
Created May 27, 2015 22:03
Show Gist options
  • Save snajpa/39e899efa2a1e5d00e70 to your computer and use it in GitHub Desktop.
Save snajpa/39e899efa2a1e5d00e70 to your computer and use it in GitHub Desktop.
is_ip() {
IP="$1"
python - "${IP}" <<EOF
import socket
import sys
# Check IPv6
if socket.has_ipv6:
try:
socket.inet_pton(socket.AF_INET6, sys.argv[1])
sys.exit(0)
except socket.error:
pass
# Check IPv4
try:
socket.inet_pton(socket.AF_INET, sys.argv[1])
sys.exit(0)
except socket.error:
sys.exit(1)
EOF
return $?
}
try_connect_ssh() {
# If only one argument is supplied and $1 resolves into a hostname,
# connect there using ssh
if [[ $# -gt 0 ]]; then
PARAMS=""
HOST=$(echo $1 | awk '{ print $1; }')
if [ "`echo $HOST | grep -P '^.+#.+$'`" != "" ]; then
VEID=$(echo $HOST | sed 's/#.*$//g')
HOST=$(echo $HOST | sed 's/^.*#//g')
PARAMS="-t vzctl enter $VEID"
fi;
RESOLVES=$(host -W 2 $HOST 2>/dev/null | grep -v NXDOMAIN)
if [ -z "${RESOLVES}" ]; then
if is_ip "$1"; then
RESOLVES="$1"
fi
fi
if [ $? -eq 0 -a -n "${RESOLVES}" ]; then
if [ "$PARAMS" != "" ]; then
ssh $HOST $PARAMS
else
ssh $@
fi
return 0
fi
fi
echo "co prosim"
return 1
}
command_not_found_handle() {
try_connect_ssh "$@"
return $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment