Skip to content

Instantly share code, notes, and snippets.

@Fusion
Last active February 13, 2021 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fusion/a81a6b5c90e40846e2f9f5ab03d41bc0 to your computer and use it in GitHub Desktop.
Save Fusion/a81a6b5c90e40846e2f9f5ab03d41bc0 to your computer and use it in GitHub Desktop.
Temporarily run a lightweight SSH server in a docker container

To run in a given container:

export C={container name} && curl https://gist.githubusercontent.com/Fusion/a81a6b5c90e40846e2f9f5ab03d41bc0/raw/sshon | bash
[ "$C" == "" ] && { echo "Syntax: export C={containername} && curl ..."; exit 1; }
PORT="$(docker exec -t $C ps x | grep dropbear | grep -Po '(222[0-9]{2})')" \
&& PID="$(docker exec -t $C cat /var/run/dropbear.pid | tr -cd '[:print:]')"
docker exec -t $C kill $PID
docker stop ssher-${PORT}
[ "$C" == "" ] && { echo "Syntax: export C={containername} && curl ..."; exit 1; }
P1=$(netstat -plnt 2>/dev/null | grep -Po '(222[0-9]{2})' | sort | tail -1) && P2=${P1:-22199} && PORT=$((P2 + 1)) \
&& IP=$((docker exec -t $C ip addr) | grep -Po '(172\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})')
[ $? -ne 0 ] && { exit 1; }
while [ "$(docker exec -t $C ps aux | grep dropbear)" == "" ]; do
echo "Attempting to run dropbear"; docker exec -t $C dropbear -p $PORT; sleep 1
done
[ $? -ne 0 ] && { exit 1; }
docker run -d --rm --name ssher-${PORT} -p ${PORT}:${PORT} svendowideit/ambassador ${IP} ${PORT} \
&& echo "To terminate this SSH environment: export C={containername} && curl https://gist.githubusercontent.com/Fusion/a81a6b5c90e40846e2f9f5ab03d41bc0/raw/sshoff | bash";
@Fusion
Copy link
Author

Fusion commented Feb 13, 2021

Your version of dropbear may be pretty old. Such is life with older containers. You may thus need to use your ssh client as follows:

ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -p <port number> <user>@<host>

@Fusion
Copy link
Author

Fusion commented Feb 13, 2021

Or in ~/.ssh/config:

Host alias1 alias2 .. aliasn
    HostName <ip or host name>
    Port 22200 (or higher)
    User root (or hopefully a less privileged user)
    KexAlgorithms +diffie-hellman-group1-sha1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment