60 lines
1.4 KiB
Bash
Executable File
60 lines
1.4 KiB
Bash
Executable File
username="$1"
|
|
pubkey_path="$2"
|
|
slbr_port="1337"
|
|
info_port="1338"
|
|
log_port="1339"
|
|
user_sshd_port="5000"
|
|
container_id=""
|
|
container_ip=""
|
|
host_ip=""
|
|
|
|
[ -z "$1" ] && echo "please provide a username." && exit
|
|
[ -z "$2" ] && echo "please provide a pubkey file." && exit
|
|
|
|
get_user_sshd_port() {
|
|
listening_ports="$(ss -tuln \
|
|
| awk '{print $5}' \
|
|
| rev \
|
|
| cut -f1 -d ":" \
|
|
| rev
|
|
)"
|
|
if echo "$listening_ports" | grep -q "$user_sshd_port"
|
|
then
|
|
user_sshd_port="$(( user_sshd_port + 1 ))"
|
|
get_user_sshd_port
|
|
else
|
|
echo "$user_sshd_port"
|
|
fi
|
|
|
|
}
|
|
get_user_sshd_port
|
|
|
|
cp "$pubkey_path" image/pubkey
|
|
host_ip="$(
|
|
ip -br -4 addr \
|
|
| grep eth0 \
|
|
| awk '{print $3}' \
|
|
| cut -f 1 -d '/'
|
|
)"
|
|
|
|
docker build \
|
|
-t slbr:$username \
|
|
--build-arg USERNAME=$username \
|
|
--build-arg HOST_IP=$host_ip \
|
|
--build-arg SLBR_PORT=$slbr_port \
|
|
--build-arg INFO_PORT=$info_port \
|
|
--build-arg LOG_PORT=$log_port \
|
|
--build-arg SSHD_PORT=$user_sshd_port \
|
|
--label description="SLBR User Container" \
|
|
./image
|
|
|
|
rm image/pubkey
|
|
echo "Starting container..."
|
|
container_id="$(docker run -p $user_sshd_port:$user_sshd_port -h slbr -d "slbr:$username")"
|
|
container_ip="$(
|
|
docker container inspect "$container_id" \
|
|
| jq '.[0].NetworkSettings.Networks.bridge.IPAddress'
|
|
)"
|
|
echo "Done!"
|
|
echo "connect using: ssh $username@$host_ip -p $user_sshd_port"
|