-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerScript.sh
267 lines (217 loc) · 8.74 KB
/
dockerScript.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
#Comprobación de que el usuario es root
clear
echo "----------------------------------";
echo "Checking if it's Docker installed on the LocalHost...";
echo "----------------------------------";
if [ $(whoami) != "root" ]; then
echo " -------------------------------- ";
echo "You must to be ROOT";
echo "Execute "sudo su" and turn to exec the program";
echo " -------------------------------- ";
exit 1
else
echo " ---------------------------------";
echo " Running Script ";
echo " ---------------------------------";
fi
sleep 2
#Compración de que Docker está instalado
if [ $(command -v docker) ]
then
echo " ";
echo "-----------------------------------";
echo "Docker is installed.."
echo "-----------------------------------";
echo " ";
else
read -p "Docker is not installed, do you want to install it?(y/n)" answer
if [ $answer = "y" ]
then
echo "Instaling Docker....";
echo "-----------------------------------";
sudo apt update -y 1>/dev/null
echo ' ########## (25%)\r';
sleep 1
sudo apt-get install docker.io -y 1>/dev/null
echo ' ################ (65%)\r';
sudo apt-get install docker -y 1>/dev/null
echo ' ######################### (100%)\r';
else
echo " ";
echo "Good Bye ;-)";
echo " ";
exit
fi
fi
# A continuación pregunta si hay un contenedor iniciado con el puerto 22 abierto
####### En caso de haberlo, lista los contenedores, el usuario selecciona un contenedor y introduce un usuario y contraseña.
####### A continuación el script instala todo el arsenal y por último pregunta si el usuario quiere entrar o no al contenedor.
echo " ";
echo "------------------------------";
read -p "Do you have a container running with port 22 openned?(y/n)" answer
echo "------------------------------";
echo " ";
if [ $answer = "y" ]
then
echo "Listing contairners with port 22 opened.....";
echo " ";
docker ps --format "ID: {{.ID}} , NAME: {{.Names}} , IMAGE: {{.Image}} , PORTS: {{.Ports}}" | grep --color=never "22/tcp" | grep -e NAME -e IMAGE -e ID -e PORTS
echo " ";
echo "--------------------------------";
echo "Give me the following data about the container:"
read -p "Container id: " container
read -p "User for ssh: " user
read -p "Password for ssh: " password
echo "--------------------------------";
echo "Installing openssh-server at the container";
echo " ";
docker exec -it $container apt-get update -y 1>/dev/null
echo ' ####### (25%)\r'
docker exec -it $container apt-get install openssh-server -y 1>/dev/null
echo ' ############ (50%)\r'
docker exec -it $container apt-get install systemctl -y 1>/dev/null
echo ' ################### (75%)\r'
docker exec -it $container systemctl status ssh 1>/dev/null
echo ' ##########################(100%)\r'
sleep 1
echo "--------------------------------- "
echo "Adding user and password to container";
docker exec -it $container adduser $user
clear
echo "--------------------------------";
echo "Starting openssh-server......"
docker exec -it $container service ssh start 1>/dev/null
echo "--------------------------------";
read -p "Do you want to connect to the container?(y/n): " answ
if [ $answ = "y" ]
then
ip=$(docker inspect --format {{".NetworkSettings.IPAddress"}} $container)
ssh-keygen -f "/root/.ssh/known_hosts" -R "$ip"
ssh $user@$ip
else
echo " ";
echo "--------------------------------";
echo "openssh-server is now installed at the container $container , with the user $user, don't forget your password ;-)";
echo "--------------------------------";
echo " ";
fi
else
# Este es el caso en el que usuario no tiene un contenedor con el puerto 22 abierto:
##### Se le pregunta, si quiere abrir el puerto 22 en un contenedor existente o si quiere crear un contenedor desde 0
echo " ";
clear
echo " ";
read -p "What do you want to do?:
1) Create a ubuntu container and open 22
2) Expose port 22 in some container
" answ
case $answ in
"1")
echo "Give me the following data about the container:"
read -p "Container Name: " name
read -p "User for ssh: " user
read -p "Password for ssh: " password
echo "You must to write 'exit'";
#Cambiar el puerto cada vez que se ejecute el script
docker run -i -p 32771:22 --name $name ubuntu
docker start $name
echo "--------------------------------- ";
echo "Installing openssh-server at the container";
echo " ";
docker exec -it $name apt-get update -y 1>/dev/null
echo ' ####### (25%)\r'
docker exec -it $name apt-get install openssh-server -y 1>/dev/null
echo ' ############ (50%)\r'
docker exec -it $name apt-get install systemctl -y 1>/dev/null
echo ' ################### (75%)\r'
docker exec -it $name systemctl status ssh 1>/dev/null
echo ' ##########################(100%)\r'
sleep 1
echo "--------------------------------- ";
echo "Adding user and password to container";
docker exec -it $name adduser $user
echo " "
echo " "
echo "Starting openssh-server......"
docker exec -it $name service ssh start 1>/dev/null
echo "--------------------------------- ";
read -p "Do you want to connect to the container?(y/n): " answ
if [ $answ = "y" ]
then
ip=$(docker inspect --format {{".NetworkSettings.IPAddress"}} $name)
ssh-keygen -f "/root/.ssh/known_hosts" -R "$ip"
ssh $user@$ip
else
echo " ";
echo "--------------------------------";
echo "openssh-server is now installed at the container $name , with the user $user, don't forget your password ;-)";
echo "--------------------------------";
echo " ";
fi
;;
#En este caso, se seleccionará un contenedor cualquiera y se le expondrá el puerto 22.
#Importante mencionar que durante el proceso, el servicio de Docker se detendrá.
"2")
echo " " ;
echo "--------------------------------";
echo " Listing all containers.....";
echo "--------------------------------";
echo " ";
docker ps -a --format "ID: {{.ID}} , NAME: {{.Names}} , IMAGE: {{.Image}} , PORTS: {{.Ports}}" | grep -e NAME -e IMAGE -e ID -e PORTS
echo " ";
echo "--------------------------------";
echo "Give me the following data about the container:"
read -p "Container id: " container
read -p "User for ssh: " user
read -p "Password for ssh: " password
read -p "Port from host: " port
echo "--------------------------------";
containerID=$(docker inspect --format {{".Id"}} $container)
systemctl stop docker.socket
systemctl stop docker
content=$(echo $content)
jq --arg port "$port" '.PortBindings |= . + {"22/tcp":[{"HostIp":"","HostPort":$port}]}' /var/lib/docker/containers/$containerID/hostconfig.json > temp.json && mv temp.json /var/lib/docker/containers/$containerID/hostconfig.json
chmod 777 /var/lib/docker/containers/$containerID/config.v2.json
jq '.Config.ExposedPorts |= . + {"22/tcp":{}}' /var/lib/docker/containers/$containerID/config.v2.json > temp3.json && mv temp3.json /var/lib/docker/containers/$containerID/config.v2.json
systemctl start docker.socket
systemctl start docker
docker start $containerID
echo "---------------------------------";
echo " ";
echo "Installing openssh-server at the container";
echo " ";
docker exec -it $container apt-get update -y 1>/dev/null
echo ' ####### (25%)\r'
docker exec -it $container apt-get install openssh-server -y 1>/dev/null
echo ' ############ (50%)\r'
docker exec -it $container apt-get install systemctl -y 1>/dev/null
echo ' ################### (75%)\r'
docker exec -it $container systemctl status ssh 1>/dev/null
echo ' ##########################(100%)\r'
sleep 1
echo "--------------------------------- "
echo "Adding user and password to container";
docker exec -it $container adduser $user
echo " "
echo " "
echo "--------------------------------";
echo "Starting openssh-server......"
docker exec -it $container service ssh start 1>/dev/null
echo "--------------------------------";
read -p "Do you want to connect to the container?(y/n): " answ
if [ $answ = "y" ]
then
ip=$(docker inspect --format {{".NetworkSettings.IPAddress"}} $container)
ssh-keygen -f "/root/.ssh/known_hosts" -R "$ip"
ssh $user@$ip
else
echo " ";
echo "--------------------------------";
echo "openssh-server is now installed at the container $container , with the user $user, don't forget your password ;-)";
echo "--------------------------------";
echo " ";
fi
;;
esac
fi