-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.sh
executable file
·147 lines (134 loc) · 3.9 KB
/
connect.sh
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
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# set -x
getSshConfig=`cat ~/.ssh/config | grep "^Host " | awk '{print $2}' | sort`
getActiveContainers=`docker ps --format "{{.Names}}"`
if [[ -z "${1}" ]] ; then
echo -e "connect requires at least 1 argument.
See 'connect -h'.
Usage: connect [OPT] [ARG]."
exit
fi
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "Add description of the script functions here."
echo
echo "Syntax: connect [OPT] [ARG]"
echo "options:"
echo "h Print this Help."
echo "l List known connections."
echo "s Select another shell for the docker connection."
echo "c Connect to a client."
echo "g Generate a new connection in .ssh/config."
echo
}
############################################################
# Main program #
############################################################
List()
{
# display list of possible connections
if [[ -z "${getSshConfig}" ]]; then
echo "Nothing configured in ~/.ssh/config."
else
echo "From your ~/.ssh/config file :"
echo "${getSshConfig}"
echo
fi
if [[ -z "${getActiveContainers}" ]] ; then
echo "No active containers."
else
echo "From your active docker containers :"
echo "${getActiveContainers}"
fi
echo
}
launchConnection()
{
checkSshConfig=`cat ~/.ssh/config | grep "^Host " | awk '{print $2}' | sort | grep ${Name}`
checkActiveContainer=`docker ps --format "{{.Names}}" | grep ${Name}`
if [ -z "${checkActiveContainer}" ] ; then
sshConnection
else
dockerConnection
fi
}
sshConnection()
{
echo -n "Connecting to ${Name}."
echo
ssh "${Name}"
}
dockerConnection()
{
if [ -z ${Shell} ]; then
echo -n "Connecting to ${Name}."
echo
docker exec -it "${Name}" bash
else
echo -n "Connecting to ${Name} with ${Shell}."
echo
docker exec -it "${Name}" "${Shell}"
fi
}
getVariablesForCreation()
{
echo -n "Which user will you connect to ?" # Pretty self explanatory
read -p "#? " User
echo -n "Which Address/Name will you connect to ?"
read -p "#? " FQDN
echo -n "What port you will be connecting to ?"
read -p "#? " Port
echo -n "What will be the connection name ? (Pick an easy one to remember)"
read -p "#? " ConName
echo -n "Do you really want to create the ssh connection '${User}@${FQDN} -p ${Port}' ? (y/N)" # Asking for a confirmation there, nothing fancy
read -p "#? " confirmation
if [ "${confirmation}" = 'y' ] || [ "${confirmation}" = 'Y' ]
then
echo
createConnection
echo "Now you only need to launch 'ssh ${ConName}' or use this script for easy picking."
exit 1
else
exit 1
fi
}
createConnection()
{
echo -e "\n \
Host ${ConName} \
\n HostName ${FQDN} \
\n User ${User} \
\n Port ${Port}" >> ~/.ssh/config
}
############################################################
# Process the input options. #
############################################################
# Get the options
while getopts ":lghs:c:" option; do
case $option in
h) # display Help
Help
exit;;
l) # display list of possible connections
List
exit;;
c) # connect to a client
Name=${OPTARG}
launchConnection
exit;;
s) # Set another shell for docker
Shell=${OPTARG}
;;
g) # create a new connection
getVariablesForCreation
exit;;
\?) # Invalid option
echo "Error: Invalid option"
Help
exit;;
esac
done