-
-
Save dirkakrid/4adb19101385f260b070b7801b7c3ea8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
############################ | |
# SSH Connection Script v3.0 | |
# Jeff Heidel 2012 | |
# Ian Thompson 2014 | |
############################ | |
## | |
RED="\033[0;37m\033[1;41m" | |
YELLOW="\033[1;30m\033[1;43m" | |
GREEN="\033[1;30m\033[1;42m" | |
ESC="\033[1;m" | |
#ctrl_c() { | |
# dynamic-colors init | |
# clear | |
# exit | |
#} | |
#trap ctrl_c INT | |
wait_exit() { | |
echo "Press ENTER to continue..." | |
read | |
exit | |
} | |
#Function continuously checks for a connection to the server | |
#and finally establishes a connection | |
wait_for_connection() | |
{ | |
if [ -z $HOST_ADDR ]; then | |
HOST_ADDR=$SSH_HOSTNAME | |
fi | |
if [ -z $SSH_PORT ]; then | |
SSH_PORT=ssh | |
fi | |
while : | |
do | |
if $ENABLE_SUBNET_CHECK ; | |
then | |
#Check current network setup | |
ip addr | grep $HOST_SUBNET >/dev/null | |
if [ $? -eq 1 ]; | |
then | |
echo -e "$RED Wrong network. $ESC Connect fail: `date`. Retry..." | |
sleep 1 | |
continue | |
fi | |
fi | |
#Test SSH Connection | |
#ssh -q -o ConnectTimeout=$CONTINUOUS_TIMEOUT $SSH_HOSTNAME exit | |
#if [ $? -ne 255 ]; | |
nmap $HOST_ADDR -PN -p $SSH_PORT 2>&1 | egrep -q 'open' | |
if [ $? -ne 1 ]; | |
then | |
break | |
fi | |
echo -e "$YELLOW Host $SSH_HOSTNAME offline. $ESC Connect fail: `date`. Retry..." | |
sleep 0.2 | |
done | |
clear | |
echo -e "$GREEN Connection established! $ESC" | |
TERM=rxvt-256color ssh $SSH_HOSTNAME | |
} | |
if [ -z $1 ]; then | |
echo -e "$RED Missing host $ESC" | |
echo "Usage: $0 host" | |
wait_exit | |
exit | |
fi | |
if [ -e /usr/local/etc/connect/hosts/$1 ]; then | |
source /usr/local/etc/connect/hosts/$1 | |
else | |
echo -e "$RED Invalid host $1 $ESC" | |
wait_exit | |
exit | |
fi | |
if [ -e /usr/local/etc/connect/colors/$COLORS ]; then | |
source /usr/local/etc/connect/colors/$COLORS | |
else | |
RED="\033[1;41m" | |
YELLOW="\033[1;43m" | |
GREEN="\033[1;42m" | |
ESC="\033[1;m" | |
DYNAMIC_COLORS=black | |
fi | |
if [ -z $CONN_SUBCMD ]; then | |
CONN_SUBCMD=1 dynamic-colors run $DYNAMIC_COLORS $0 $* | |
exit | |
fi | |
START=$(date +%s) | |
#dynamic-colors switch $DYNAMIC_COLORS | |
clear | |
echo -e "$GREEN Starting remote shell on $SSH_HOSTNAME... $ESC" | |
TERM=rxvt-256color ssh -o ConnectTimeout=$INITIAL_TIMEOUT $SSH_HOSTNAME | |
if [ $? -eq 255 ]; | |
then | |
END=$(date +%s) | |
DIFF=$(( $END - $START )) | |
if [ $DIFF -lt $(( $INITIAL_TIMEOUT + 2 )) ]; | |
then | |
#If initial connection fails, start continuous attempts | |
echo -e "$RED Connection FAILED! $ESC Starting continuous connection attempts..." | |
wait_for_connection | |
fi | |
fi | |
#dynamic-colors init | |
echo -e "$GREEN Connection to $SSH_HOSTNAME closed $ESC" | |
wait_exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment