Last active
July 31, 2018 19:13
-
-
Save necrolyte2/09afe2a0b8cf2a8b14a1e9813e890f2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The ip to ssh to to have remote ssh setup on | |
remote_ip=$1 | |
# The remote username to ssh as | |
remote_user=$2 | |
# The port to connect to the remote host on | |
remote_port=${3:-22} | |
# The port to open on the remote host to listen on | |
remote_listen_port=${4:-2222} | |
PID_FILE=~/.remote_ssh_proxy.pid | |
usage="${BASH_SOURCE[0]} <remote_ip> <remote_user> [<remote_port:22> <remote_listen_port:2222>]" | |
if [ -z $remote_ip ]; then | |
echo $usage | |
exit 1 | |
fi | |
if [ -z $remote_user ]; then | |
echo $usage | |
exit 1 | |
fi | |
function run() { | |
remote_user=$1 | |
remote_ip=$2 | |
remote_port=$3 | |
remote_listen_port=$4 | |
pod_file=$5 | |
ssh -f -N $remote_user@$remote_ip -p $remote_port -R ${remote_listen_port}:localhost:22 & | |
PID=$(ps -ef | grep 'ssh -f -N' | awk '!/grep/ {print $2}') | |
echo $PID > $pid_file | |
echo "remote ssh proxy should be running" | |
} | |
while sleep 15; do | |
# Pid file exists and has pid in it. Process should be running | |
if [ -f $PID_FILE ]; then | |
PID=$(cat $PID_FILE) | |
echo "PID: $PID" | |
if [ -n $PID ]; then | |
# ssh process is already running | |
if ps -f -p $PID | grep -v 'STIME'; then | |
echo "Looks like ssh is already running. Refusing to run it again(it would just error anyways)" | |
echo "If this is an error you might try to kill it with:" | |
echo "kill -9 $PID" | |
exit 1 | |
else | |
run $remote_user $remote_ip $remote_port $remote_listen_port $PID_FILE | |
fi | |
else | |
run $remote_user $remote_ip $remote_port $remote_listen_port $PID_FILE | |
fi | |
else | |
run $remote_user $remote_ip $remote_port $remote_listen_port $PID_FILE | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment