Skip to content

Instantly share code, notes, and snippets.

@dmuth
Created March 29, 2023 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmuth/1e5ef9ee95d17076cebe5896ce759e0a to your computer and use it in GitHub Desktop.
Save dmuth/1e5ef9ee95d17076cebe5896ce759e0a to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This script is to test specific Autobahn jumphosts.
#
# Errors are fatal
set -e
#
# Print our syntax and exit.
#
function print_syntax() {
echo "! "
echo "! Syntax: $0 jumphost [ --num-tries n ] [ --ssh ] [ --curl-v4 ] [ --curl-v6 ]"
echo "! "
echo "! jumphost - the IP of the host you are trying to connect to. "
echo "! "
echo "! --num-tries - How many times to try connecting? Default is 5."
echo "! --ssh - Use SSH (the default)"
echo "! --ssh-simple - Use SSH, but don't try to jump, just SSH directly to the jumphost"
echo "! --curl-v4 - Use curl over IPv4 to port 22"
echo "! --curl-v6 - Use curl over IPv6 to port 22"
echo "! "
echo "! You can get a list of IPs with this command: dig jump.autobahn.comcast.com"
echo "! "
echo "! Here are some example commands: "
echo "! "
for IP in $(dig +short a jump.autobahn.comcast.com)
do
echo "! $0 ${IP} 2>&1 | tee ${IP}.txt"
done
echo "! "
exit 1
} # End of print_syntax()
TARGET=$1
NUM_TRIES=5
USE_SSH=1
USE_SSH_SIMPLE=1
USE_CURL_V4=""
USE_CURL_V6=""
#
# We should always have a target
#
if test ! "${TARGET}"
then
print_syntax
fi
# Next arg!
shift
while true
do
ARG=$1
ARG_NEXT=$2
if test ! "${ARG}"
then
break
fi
if test "${ARG}" == "--help" -o "${ARG}" == "-h"
then
print_syntax
fi
if test "${ARG}" == "--num-tries"
then
if test ! "${ARG_NEXT}"
then
print_syntax
fi
NUM_TRIES=${ARG_NEXT}
shift # Remove an extra argument
elif test "${ARG}" == "--ssh"
then
USE_SSH=1
elif test "${ARG}" == "--ssh-simple"
then
USE_SSH=""
USE_SSH_SIMPLE=1
elif test "${ARG}" == "--curl-v4"
then
USE_SSH=""
USE_CURL_V4=1
elif test "${ARG}" == "--curl-v6"
then
USE_SSH=""
USE_CURL_V6=1
fi
# Next argument
shift
done
#echo "DEBUG ARGS: target=${TARGET}, num_tries=${NUM_TRIES}, ssh=${USE_SSH}, ssh_simple=${USE_SSH_SIMPLE}, curl_v4=${USE_CURL_V4}, curl_v6=${USE_CURL_V6}" # Debugging
echo "# Starting test run..."
echo "# "
echo "# Jumphost to test: ${TARGET}"
echo "# Num tries: ${NUM_TRIES}"
if test "${USE_SSH}"
then
echo "# Test method: SSH"
elif test "${USE_SSH_SIMPLE}"
then
echo "# Test method: SSH (simple)"
elif test "${USE_CURL_V4}"
then
echo "# Test method: curl over IPv4"
elif test "${USE_CURL_V6}"
then
echo "# Test method: curl over IPv6"
fi
echo "# "
SSH_CMD="ProxyCommand ssh -x -i $HOME/.ssh/id_rsa -o CertificateFile=$HOME/.ssh/id_rsa-cert.pub svcAutobahn@${TARGET} -W %h:%p"
TIMEFORMAT="# Number of seconds elapsed: %R"
CURL_V4="curl -g4 ${TARGET}:22"
CURL_V6="curl -g6 ${TARGET}:22"
if test "${USE_SSH}"
then
echo "# SSH Command: ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET}"
elif test "${USE_SSH_SIMPLE}"
then
echo "# SSH Command: ssh -i $HOME/.ssh/id_rsa -o testlogin@${TARGET}"
elif test "${USE_CURL_V4}"
then
echo "# Curl command: ${CURL_V4}"
elif test "${USE_CURL_V6}"
then
echo "# Curl command: ${CURL_V6}"
fi
echo "# "
for I in $(seq ${NUM_TRIES})
do
echo "# Attempt ${I}..."
set +e
time {
if test "${USE_SSH}"
then
ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} > /dev/null
elif test "${USE_SSH_SIMPLE}"
then
ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} > /dev/null
elif test "${USE_CURL_V4}"
then
RESULT=$(${CURL_V4} 2>/dev/null)
elif test "${USE_CURL_V6}"
then
RESULT=$(${CURL_V6} 2>/dev/null)
fi
}
RETVAL=$?
set -e
if test "${USE_SSH}" -o "${USE_SSH_SIMPLE}"
then
if test "${RETVAL}" -eq 0
then
echo "# Return value is 0. (success?)"
else
echo "! Retval: ${RETVAL}"
fi
elif test "${USE_CURL_V4}"
then
if [[ "${RESULT}" =~ "SSH" ]]
then
echo "# Valid SSH server! String: ${RESULT}"
else
echo "! Invalid SSH server! String: ${RESULT}"
fi
elif test "${USE_CURL_V6}"
then
if [[ "${RESULT}" =~ "SSH" ]]
then
echo "# Valid SSH server! String: ${RESULT}"
else
echo "! Invalid SSH server! String: ${RESULT}"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment