-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_ssh_config.sh
More file actions
executable file
·40 lines (35 loc) · 1.05 KB
/
create_ssh_config.sh
File metadata and controls
executable file
·40 lines (35 loc) · 1.05 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
#!/bin/bash
findAvailablePort() {
# Find an available availablePort
minPort=2222
maxPort=2999
for port in $(seq ${minPort} ${maxPort} | shuf); do
out=$(netstat -aln | grep LISTEN | grep ${port})
if [ -z "${out}" ]; then
# To prevent multiple users from using the same available port --> Write file to reserve it
portFile=/tmp/${port}.port.used
if ! [ -f "${portFile}" ]; then
touch ${portFile}
availablePort=${port}
echo ${port}
break
fi
fi
done
if [ -z "${availablePort}" ]; then
echo "ERROR: No service port found in the range ${minPort}-${maxPort} -- exiting session"
exit 1
fi
}
user_container_ssh_port=$(findAvailablePort)
cat > ~/.ssh/config <<HERE
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
Host usercontainer
HostName localhost
User ${USER}
Port ${user_container_ssh_port}
StrictHostKeyChecking no
IdentityFile ~/.ssh/pw_id_rsa
HERE