Files
safe-x/ssh-to
Ville Rantanen a19385fe3c ssh helper
2017-06-22 09:38:26 +03:00

58 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
function helpExit() {
echo "SSH to Docker session."
echo " arg: container name "
echo " --help This help"
exit
}
function rsaMissing() {
echo "RSA keys missing. have you started a container here?"
exit 1
}
function listContainers() {
docker ps -a
}
function isRunning() {
[[ $( docker inspect --format '{{ .State.Running }}' "$1" ) = true ]]
}
function containerRestart() {
docker restart "$1"
}
for (( i=1; i<=$#; i++ )); do
j=$(( $i + 1 ))
[[ "${!i}" = "--help" ]] && helpExit
[[ "${!i}" = "-h" ]] && helpExit
done
set -e
[[ -f .id_rsa.pub ]] || rsaMissing
[[ -f .id_rsa ]] || rsaMissing
[[ -z "$1" ]] && {
listContainers
echo "Pass container name / id"
exit
}
cd $( dirname $( readlink -f $0 ) )
export USER=$( id -u -n )
nname=$1
isRunning $nname || containerRestart $nname
while :; do
sleep 3
IP=$( docker inspect --format '{{ .NetworkSettings.IPAddress }}' $nname )
[[ -z "$IP" ]] || break
done
echo $IP
while :; do
sleep 3
ssh-keyscan $IP && break
done
ssh -i .id_rsa -XY -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $USER@$IP