-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconnect.bash
More file actions
134 lines (125 loc) · 4.8 KB
/
connect.bash
File metadata and controls
134 lines (125 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
#set -x
# ----------------------------------------------------------------------------------------
#
# Script: connect.bash
#
# Purpose: Provide an on-screen menu for SSH connections.
#
# History: 9/2017 - Created. Adapted from previous versions writted in Bash and Python.
# 2/2018 - Updated to use bash builtin exec to execute ssh commands so that
# become its own process independent of this script.
# 3/2018 - Expanded width of menu to fit longer menu names.
# 12/2020 - Added ability to print user's RSA Token passcode via stoken.
#
# ----------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------
# Define the configuration file
# ----------------------------------------------------------------------------------------
VERSION='1.3.1'
CONFIGFILE=~/.sshconnectrc
NETSTAT=$(which netstat)
# ----------------------------------------------------------------------------------------
# Function: add_entry - Provides a series of prompts for adding a connection entry to the
# config file.
# ----------------------------------------------------------------------------------------
add_entry ()
{
echo " * Adding entry to $(echo $CONFIGFILE)..."
read -rep "Connection Name: " connname
read -rep "Username: " username
read -rep "Hostname: " hostname
read -rep "Hostname for port forwarding: " pfhostname
read -rep "Port to use: " portnum
nlines=$(cat $HOME/.sshconnectrc | wc -l)
nlines=$(($nlines+1))
if [ -z $pfhostname ]; then pfhostname=$hostname; fi
echo "$nlines:$connname:$username:$hostname:$pfhostname:$portnum" >> $CONFIGFILE
read -rep "Do you want to add another entry? [y|n]: " response
case $response in
[Yy]*) add_entry ;;
[Nn]*) ;;
*) ;;
esac
}
# ----------------------------------------------------------------------------------------
# Function: check_port - to check if a port is open and listening. This function returns
# 0 is the port is open and listening and 1 otherwise.
# ----------------------------------------------------------------------------------------
check_port ()
{
if [ ! -x $NETSTAT ]; then
echo " * error: netstat not found."
exit 1
fi
if [[ $(uname -s) == "Darwin" ]]; then
# FreeBSD or macOS
$NETSTAT -anp tcp 2>/dev/null | grep LISTEN | grep $portnum 1> /dev/null 2>&1
elif [[ $(uname -s) == "Linux" ]]; then
# GNU Linux
$NETSTAT -tulpn 2>/dev/null | grep LISTEN | grep $portnum 1> /dev/null 2>&1
fi
echo $?
}
# ----------------------------------------------------------------------------------------
# Function: print_menu - Print the on-screen menu of connection entries
# ----------------------------------------------------------------------------------------
print_menu ()
{
clear
echo ""
echo "**************************************"
echo "* SELECT HOST COMPUTER *"
echo "* **** *"
while read -r line
do
num=$(echo "$line" | cut -d":" -f 1)
con=$(echo "$line" | cut -d":" -f 2 | sed 's/\"//g')
printf "* %2d) %-30s *\n" $num "$con"
done < $CONFIGFILE
echo "* *"
echo "* A) Add entry *"
echo "**************************************"
}
# ----------------------------------------------------------------------------------------
# Main Script
# ----------------------------------------------------------------------------------------
if [ ! -f $CONFIGFILE ]; then
echo " * Creating $(echo $CONFIGFILE) ..."
touch $CONFIGFILE
add_entry
else
if [ $(cat $CONFIGFILE | wc -l) -eq 0 ]; then
add_entry
fi
fi
print_menu
read -rep "Select option: " selection
if [[ $selection == "A" ]]; then
add_entry
else
line=$(grep ^$selection $CONFIGFILE)
user=$(echo $line | cut -d":" -f 3)
host=$(echo $line | cut -d":" -f 4)
porthost=$(echo $line | cut -d":" -f 5)
portnum=$(echo $line | cut -d":" -f 6)
usestoken=$(echo $line | cut -d":" -f 7)
# Check for blank string in ~/.sshconnectrc
if [ "x$usestoken" == "x" ]; then usestoken=false; fi
port_in_use=$(check_port)
if [ $port_in_use -eq 0 ]; then
# Port is in use
exec ssh $user@$host
elif [ $port_in_use -eq 1 ];then
# Port NOT in use, so port forward here
echo " * Port $portnum is available..."
echo " * Connecting to $host binding port $portnum..."
if [ -f $HOME/.stokenrc ] && [ -x $(which stoken) ] && \
[ "$usestoken" == "true" ] ; then
echo " * RSA Token Passcodes: current: $(stoken) next: $(stoken --next)"
fi
sshportfwd="-4 -L $portnum:$porthost:22"
exec ssh $sshportfwd $user@$host
fi
fi
exit 0