Skip to content

Instantly share code, notes, and snippets.

@dmuth
Created January 11, 2024 17:29
Show Gist options
  • Save dmuth/713ed31b60a89ac2d6eac5519b3da779 to your computer and use it in GitHub Desktop.
Save dmuth/713ed31b60a89ac2d6eac5519b3da779 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This script is to test specific Autobahn jumphosts.
#
# Errors are fatal
set -e
#set -x # Debugging
TARGET=""
NUM_TRIES=3
USE_SSH=1
USE_SSH_SIMPLE=""
#
# Print our syntax and exit.
#
function print_syntax() {
echo "! "
echo "! Syntax: $0 [ jumphost [ jumphost [ ... ] ] ] [ --num-tries n ] [ --ssh-simple ] "
echo "! "
echo "! jumphost - jumphosts to test. If not specified, all hosts will be tested."
echo "! "
echo "! --num-tries - How many times to try connecting? Default is 5."
echo "! --ssh-simple - Use SSH, but don't include proxy config, just SSH directly to the jumphost"
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()
#
# Parse our arguments.
#
function parse_args() {
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
USE_SSH_SIMPLE=""
elif test "${ARG}" == "--ssh-simple"
then
USE_SSH=""
USE_SSH_SIMPLE=1
else
TARGETS="${TARGETS} $1"
fi
# Next argument
shift
done
if test ! "${TARGETS}"
then
TARGETS=$(dig +short a jump.autobahn.comcast.com | tr "\n" " ")
fi
} # End of parse_args()
parse_args $@
if test "${DEBUG}" != ""
then
echo "DEBUG ARGS: targets=${TARGETS}, num_tries=${NUM_TRIES}, ssh=${USE_SSH}, ssh_simple=${USE_SSH_SIMPLE} "
fi
echo "# Starting test run against Autobahn Jumphosts..."
echo "# "
echo "# Jumphost(s) to test: ${TARGETS}"
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)"
fi
echo "# "
for I in $(seq ${NUM_TRIES})
do
for TARGET in ${TARGETS}
do
echo -n "# Test ${I}/${NUM_TRIES} on ${TARGET}... "
SSH_CMD="ProxyCommand ssh -x -i $HOME/.ssh/id_rsa -o CertificateFile=$HOME/.ssh/id_rsa-cert.pub svcAutobahn@${TARGET} -W %h:%p"
SUCCESS_STRING="It worked! Congratulations!"
set +e
if test "${USE_SSH}"
then
#echo ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} # Debugging
TIME_START=$(python3 -c 'import time; print(int(time.time() * 1000))')
#ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} > /dev/null
OUTPUT=$(ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} 2>&1 )
RETVAL=$?
TIME_END=$(python3 -c 'import time; print(int(time.time() * 1000))')
elif test "${USE_SSH_SIMPLE}"
then
#echo ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} # Debugging
TIME_START=$(python3 -c 'import time; print(int(time.time() * 1000))')
#ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} > /dev/null
OUTPUT=$(ssh -i $HOME/.ssh/id_rsa -o "${SSH_CMD}" testlogin@${TARGET} 2>&1 )
RETVAL=$?
TIME_END=$(python3 -c 'import time; print(int(time.time() * 1000))')
fi
TIME_DIFF=$(awk "BEGIN { print ( ${TIME_END} - ${TIME_START} ) / 1000 }")
if [[ "${OUTPUT}" == *"${SUCCESS_STRING}"* ]]
then
echo -n "We connected successfully! "
else
echo -n "!! We did NOT connect successfully !! "
fi
set -e
if test "${RETVAL}" -ne 0
then
echo "! Retval: ${RETVAL}"
fi
echo "Time elapsed: ${TIME_DIFF} sec."
done # for TARGET in TARGETS
done # for seq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment