FRITA-infra/forward-ssh.sh

24 lines
727 B
Bash
Raw Normal View History

2022-11-19 07:37:47 +00:00
#!/bin/bash
# Finds the SSH private key under ./.vagrant and connects to
# the Vagrant box port forwarding localhost ports: 8443, 80, 443
PRIVATE_KEY="$(find .vagrant -name "private_key")"
HOST_IP="$(vagrant ssh -c "hostname -I | cut -d' ' -f2" 2>/dev/null)"
if [ "$(pgrep -afc "$PRIVATE_KEY")" -eq 0 ]; then
set -x
sudo ssh -fNT -i "$PRIVATE_KEY" \
-L 8443:localhost:8443 \
-L 80:localhost:80 \
-L 443:localhost:443 \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
vagrant@"${HOST_IP::-1}" 2>/dev/null
set +x
else
echo "ERROR: SSH process already running"
pgrep -af "$PRIVATE_KEY"
echo -e "\nKill process:\n\tsudo kill -9 \"\$(pgrep -f \"$PRIVATE_KEY\")\""
exit 1
fi