-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchDeamon.sh
executable file
·132 lines (119 loc) · 4.08 KB
/
launchDeamon.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
#!/bin/bash
# Written by Maxence G. de Montauzan
# GLOBAL VARIABLES
# TODO Put in another files
readonly CONTAINER_NAME="sshd" # Name of the docker container
readonly IMG_NAME="mgdemontauzan/ssh" # Name of the docker image to run
#=== FUNCTION ================================================================
# Launch a new docker container with a name.
# Parameter : name of the container
#===============================================================================
launch_new_container() {
if [[ -z "$1" ]]; then
echo "Function launch_new_container - Problem in script!" 1>2&
exit 1
fi
echo ""
echo "Run docker container"
sudo docker run -d -p 22 --name $1 $IMG_NAME
}
#=== FUNCTION ================================================================
# SSH connection to a container. Show IP, PORT and SSH command.
# Parameter : name of the container
#===============================================================================
connect_to_container() {
if [[ -z "$1" ]]; then
echo "Function connect_to_container - Problem in script!" 1>2&
exit 1
fi
echo "Add SSH Key to connect with docker container"
eval $(ssh-agent -s)
ssh-add sshkey
echo ""
echo "Get port to connect to docker container"
local port=$(sudo docker port $1 22 | awk -F ':' '{print $2}')
echo "$port"
echo ""
echo "Get ip address of docker container"
#ifconfig | grep docker -A 1 | awk '/[0-9]\./ {print $2}' | sed 's/adr://'
local ipadress=$(ip addr show | grep docker \
| awk '/[0-9]\./ {print $2}' \
| sed 's/\/[0-9]*$//')
echo $ipadress
echo ""
echo "Connecting to container:"
echo "ssh root@$ipadress -p $port"
echo ""
ssh root@$ipadress -p $port
}
#=== FUNCTION ================================================================
# Check if old containers exists and propose to delete it.
# Parameter 1: The name of the searched container.
# Return:
# 0 - Sucess
# 1 - Failure: container not deleted, can't launch a container
#===============================================================================
check_old_container() {
if [[ -z "$1" ]]; then
echo "Problem Function check_old_container - in script !!!" 1>2&
exit 1
fi
if [[ -n $(sudo docker ps -a | cut -c141- | grep $1) ]]; then
echo "Docker container SSHD history exists."
read -r -p "Would you remove it ? [Y/n] " response
if [[ $response =~ (^(yes|y)$|^$) ]]; then
echo "Removing old sshd container..."
sudo docker rm $1
return 0
else
echo "Can't launch container - Conflict case" 1>2&
return 1
fi
fi
}
# A container (or more) is already running
if [[ -n $(sudo docker ps | cut -c141- | grep $CONTAINER_NAME) ]]; then
echo "Docker SSHD container already run."
read -r -p "Would you stop it and launch a new container ? Reconnect to it ?
Keep it and launch another (and connect to it) ? [S/r/l/n]" response
if [[ $response =~ (^(stop|s|S)$|^$) ]]; then
./stopDeamon.sh
elif [[ $response =~ (^(reconnect|reco|r|R)$) ]]; then
echo "Reconnection to running container"
if [[ $(sudo docker ps | cut -c141- | grep sshd | wc -l) -eq 1 ]]; then
connect_to_container $(sudo docker ps | cut -c141- | grep sshd \
| awk '{print $1}')
else
echo "Choose a running container to connect it :"
sudo docker ps
echo "Name ?"
read name
connect_to_container $name
fi
exit
elif [[ $response =~ (^(launch|l|L)$) ]]; then
echo "launch another container"
nbContainer=$(sudo docker ps | cut -c141- | grep sshd | wc -l)
nbContainer=$(expr $nbContainer + 1)
containerName=sshd-$nbContainer
check_old_container $containerName
launch_new_container $containerName
connect_to_container $containerName
exit 0
else
echo "So don't run another deamon !"
exit 0
fi
else
containerName=$CONTAINER_NAME # Name of the docker container
check_old_container $containerName # Name of the docker image to run
if [[ $? -eq 1 ]]; then
echo ""
echo "An old container exists - create another container"
nbContainer=$(sudo docker ps -a | cut -c141- | grep sshd | wc -l)
nbContainer=$(expr $nbContainer + 1)
containerName=sshd-$nbContainer
fi
launch_new_container $containerName
connect_to_container $containerName
fi