Auto setup the SSH connection if the devcontainer has not been initialised.

This commit is contained in:
Hans Goor 2024-11-23 17:50:32 +01:00
parent bde737b037
commit d16d8c94ac
Signed by: eyedevelop
SSH key fingerprint: SHA256:Td89veptDEwCV8J3fjqnknNk7SbwzedYhauyC2nFBYg

View file

@ -21,7 +21,23 @@ if ! ssh_port="$(${CONTAINER} inspect --format '{{ (index (index .NetworkSetting
exit 1
fi
ssh -o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=false \
ignore_hostkey=("-oUserKnownHostsFile=/dev/null" "-oStrictHostKeyChecking=false")
if ! ssh "${ignore_hostkey[@]}" -oPasswordAuthentication=no -p "${ssh_port}" -T dev@localhost 'exit'; then
docker exec -it "${container_id}" /bin/bash -c 'echo "dev:dev" | chpasswd'
echo "[+] Changed password of container user to 'dev'"
if command -v sshpass &>/dev/null; then
sshpass -p"dev" ssh-copy-id -p "${ssh_port}" dev@localhost
elif command -v expect &>/dev/null; then
expect -c "spawn $(which ssh-copy-id) -p ${ssh_port} dev@localhost; interact -o -nobuffer -re .*assword.* return; send \"dev\r\n\"; send -- \"\r\"; expect eof;"
else
echo "[?] Could not find application to automatically copy SSH key."
echo " Please enter the password 'dev' manually below."
ssh-copy-id -p "${ssh_port}" dev@localhost
fi
fi
ssh "${ignore_hostkey[@]}" \
-p "${ssh_port}" \
dev@localhost "$@"