Last active
August 29, 2015 14:05
-
-
Save lilithmooncohen/42e4d8b4a9abbffb28c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ------------------------------------------------------------------ | |
# Author: Ryan C Koch - ryanckoch@gmail.com | |
# Purpose: CHECK TCP AND UDP CONNECTIVITY BETWEEN | |
# SOURCE AND DESTINATION HOSTS | |
# ------------------------------------------------------------------ | |
SOURCE_FILE="source.hosts" | |
DESTINATION_FILE="destination.hosts" | |
LOG_VERBOSE="logs/verbose.log" | |
LOG_SSH_ERROR="logs/ssh_error.log" | |
LOG_COMPLETED="logs/completed.log" | |
LOG_ERRORED="logs/errored.log" | |
#VERIFY SOURCE AND DESTINATION FILES EXIST | |
if [ ! -f $SOURCE_FILE ] || [ ! -f $DESTINATION_FILE ]; then | |
echo 'SOURCE AND/OR DESTINATION FILE DO NOT EXIST. EXITING.' | |
exit 1 | |
fi | |
# PARSE SOURCE AND DESTINATIONS FILES INTO ARRAYS | |
declare -a SOURCES_ARRAY | |
declare -a DESTINATIONS_ARRAY | |
readarray -t SOURCES_ARRAY < $SOURCE_FILE | |
readarray -t DESTINATIONS_ARRAY < $DESTINATION_FILE | |
# VERIFY SOURCE AND DESTINATIONS WERE NOT EMPTY | |
if [ ${#SOURCES_ARRAY[@]} -eq 0 ] || [ ${#DESTINATIONS_ARRAY[@]} -eq 0 ] ; then | |
echo 'SOURCE OR DESTINATION FILE IS EMPTY. EXITING NOW.' | tee -a $LOG_VERBOSE | |
exit 1 | |
fi | |
#VERIFY LOG DIRECTORY EXISTS AND IF NOT, CREATE IT | |
if [ ! -d logs ] ; then | |
mkdir logs | |
fi | |
# PROMPT FOR USERNAME | |
USERNAME=`whoami` | |
read -p 'SSH USERNAME ['$USERNAME']: ' -r | |
if [ -n "$REPLY" ] ; then | |
USERNAME=$REPLY | |
fi | |
# PROMPT FOR SSH KEY | |
read -p 'USE SSH KEY ~/.ssh/id_rsa ([y]/n)?: ' -r | |
if [[ $REPLY =~ ^[Yy]$ ]] || [ -z "$REPLY" ] ; then | |
KEYFILE=~/.ssh/id_rsa | |
elif [[ $REPLY =~ ^[nN]$ ]] ; then | |
read -p 'SSH KEY (FULL PATH): ' -r | |
KEYFILE=$REPLY | |
else | |
echo 'INVALID INPUT. EXITING. NOTHING EXECUTED.' | |
exit 1 | |
fi | |
if [ ! -f $KEYFILE ] ; then | |
echo 'SSH KEY NOT FOUND. EXITING. NOTHING EXECUTED.' | |
exit 1 | |
fi | |
# PROMPT FOR PORT NUMBER | |
read -p 'PORT TO CHECK [80]: ' -r | |
if [ -n "$REPLY" ] ; then | |
if [[ $REPLY =~ ^-?[0-9]+$ ]] ; then | |
PORT=$REPLY | |
else | |
echo 'INVALID INPUT. EXITING. NOTHING EXECUTED.' | |
exit 1 | |
fi | |
else | |
PORT="80" | |
fi | |
# PROMPT FOR PROTOCOL | |
read -p 'UDP or TCP? [tcp]: ' -r | |
if [ -n "$REPLY" ] ; then | |
if [ ${REPLY,,} == "tcp" ] || [ ${REPLY,,} == "udp" ] ; then | |
PROTO=${REPLY,,} | |
else | |
echo 'INVALID INPUT. EXITING. NOTHING EXECUTED.' | |
exit 1 | |
fi | |
else | |
PROTO="tcp" | |
fi | |
echo | |
# OUTPUT INFO | |
echo '*******************' | |
echo 'SSH USER: '$USERNAME | |
echo '*******************' | |
echo 'SSH KEY: '$KEYFILE | |
echo '*******************' | |
echo 'PORT TO CHECK: '$PORT | |
echo '*******************' | |
echo 'PROTOCOL: '$PROTO | |
echo '*******************' | |
echo 'SOURCE HOSTS: ' | |
for SOURCE_HOST in "${SOURCES_ARRAY[@]}" ; do | |
echo $SOURCE_HOST | |
done | |
echo '*******************' | |
echo 'DESTINATION HOSTS: ' | |
for DESTINATION_HOST in "${DESTINATIONS_ARRAY[@]}" ; do | |
echo $DESTINATION_HOST | |
done | |
echo '*******************' | |
echo | |
# VERIFY WITH USER ABOVE INFO IS CORRECT | |
read -p 'IS THIS CORRECT ([y]/n)?: ' -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] || [ -z "$REPLY" ] ; then | |
echo '' | |
elif [[ $REPLY =~ ^[nN]$ ]] ; then | |
echo 'EXITING BASED ON USER INPUT. NOTHING EXECUTED.' | |
exit 0 | |
else | |
echo 'INVALID INPUT. EXITING. NOTHING EXECUTED.' | |
exit 1 | |
fi | |
echo | |
echo 'VERIFYING SSH ACCESS ON SOURCE HOSTS' | |
echo | |
TIMESTAMP=`date +"%Y-%m-%d %H:%M:%S"` | |
echo '*******************SSH CHECK BEGIN '$TIMESTAMP'*******************' | tee -a $LOG_VERBOSE $LOG_SSH_ERROR | |
# VERIFY SSH ACCESS TO SOURCE HOSTS | |
declare -a SSH_SUCCESS_ARRAY | |
declare -a SSH_FAILED_ARRAY | |
for SOURCE_HOST in "${SOURCES_ARRAY[@]}" ; do | |
echo $SOURCE_HOST | tee -a $LOG_VERBOSE | |
ssh -i $KEYFILE -n -o BatchMode=yes -o StrictHostKeyChecking=no $USERNAME@$SOURCE_HOST 'echo "SSH CONNECTION SUCCESFUL"' >> $LOG_VERBOSE 2>&1 | |
if [ 0 = $? ]; then | |
SSH_SUCCESS_ARRAY+=($SOURCE_HOST) | |
echo 'SSH CONNECTION SUCCESFUL' | |
else | |
SSH_FAILED_ARRAY+=($SOURCE_HOST) | |
echo 'SSH CONNECTION FAILED' | tee -a $LOG_VERBOSE | |
echo $SOURCE_HOST >> $LOG_SSH_ERROR | |
fi | |
done | |
echo | tee -a $LOG_VERBOSE | |
if [ ${#SSH_FAILED_ARRAY[@]} -eq 0 ]; then | |
echo | tee -a $LOG_VERBOSE | |
echo "SSH ACCESS TO ALL SOURCE HOSTS SUCCESFUL" | tee -a $LOG_VERBOSE | |
elif [ ${#SSH_FAILED_ARRAY[@]} -eq 0 ]; then | |
echo | tee -a $LOG_VERBOSE | |
echo "SSH FAILED ON ALL SOURCE HOSTS" | tee -a $LOG_VERBOSE | |
echo "EXITING NOW" | tee -a $LOG_VERBOSE | |
exit 1 | |
else | |
echo "SSH FAILED ON THE FOLLOWING SOURCE HOSTS: " | tee -a $LOG_VERBOSE | |
for SSH_FAILED_HOST in "${SOURCES_ARRAY[@]}" ; do | |
echo $SSH_FAILED_HOST | tee -a $LOG_VERBOSE | |
done | |
echo | tee -a $LOG_VERBOSE | |
echo "UNSUCCESFUL HOSTS ARE LISTED IN LOG FILE" | tee -a $LOG_VERBOSE | |
echo "CONTINUING ON TO NEXT STEP FOR SUCCESFUL HOSTS" | tee -a $LOG_VERBOSE | |
fi | |
echo | tee -a $LOG_VERBOSE | |
TIMESTAMP=`date +"%Y-%m-%d %H:%M:%S"` | |
echo '*******************SSH CHECK END'$TIMESTAMP'*******************' | tee -a $LOG_VERBOSE $LOG_SSH_ERROR | |
echo | tee -a $LOG_VERBOSE $LOG_SSH_ERROR | |
echo | |
echo 'INITIATING PORT CHECKS' | |
echo | |
echo | tee -a $LOG_VERBOSE $LOG_COMPLETED $LOG_ERRORED | |
TIMESTAMP=`date +"%Y-%m-%d %H:%M:%S"` | |
echo '*******************PORT CHECK BEGIN '$TIMESTAMP'*******************' | tee -a $LOG_VERBOSE $LOG_COMPLETED $LOG_ERRORED | |
# RUN PORT CHECKS ON SOURCE HOSTS | |
for SSH_SUCCESS_HOST in "${SSH_SUCCESS_ARRAY[@]}" ; do | |
for DESTINATION_HOST in "${DESTINATIONS_ARRAY[@]}" ; do | |
echo $SSH_SUCCESS_HOST' > '$PORT' > '$PROTO' > '$DESTINATION_HOST | tee -a $LOG_VERBOSE | |
ssh -i $KEYFILE -n -o BatchMode=yes -o StrictHostKeyChecking=no $USERNAME@$SSH_SUCCESS_HOST "cat < /dev/$PROTO/$DESTINATION_HOST/$PORT" >> $LOG_VERBOSE 2>&1 | |
if [ 0 = $? ]; then | |
echo 'CONNECTION ESTABLISHED' | tee -a $LOG_VERBOSE | |
echo $SSH_SUCCESS_HOST' > '$PORT' > '$PROTO' > '$DESTINATION_HOST >> $LOG_COMPLETED | |
else | |
echo 'CHECK FAILED' | tee -a $LOG_VERBOSE | |
echo $SSH_SUCCESS_HOST' > '$PORT' > '$PROTO' > '$DESTINATION_HOST >> $LOG_ERRORED | |
fi | |
echo | |
echo >> $LOG_VERBOSE | |
done | |
done | |
TIMESTAMP=`date +"%Y-%m-%d %H:%M:%S"` | |
echo '*******************PORT CHECK END '$TIMESTAMP'*******************' | tee -a $LOG_VERBOSE $LOG_COMPLETED $LOG_ERRORED | |
echo | tee -a $LOG_VERBOSE $LOG_COMPLETED $LOG_ERRORED | |
echo | |
echo 'PORT CHECKS COMPLETE' | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment