Skip to content

Instantly share code, notes, and snippets.

@yk-tanigawa
Created February 2, 2018 09:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yk-tanigawa/1425653dacb2216a589eb3535f2ef2c5 to your computer and use it in GitHub Desktop.
Save yk-tanigawa/1425653dacb2216a589eb3535f2ef2c5 to your computer and use it in GitHub Desktop.
ssh port forwarding scripts
#!/bin/bash
# parse args
if [ $# -lt 2 ] ; then
echo "usage: $0 <target-server> <login-server> [local port (default: 18888)] [target port (default: 8888)] [login port (default: rand)]" >&2
exit 1
fi
target=$1
login=$2
if [ $# -lt 3 ] ; then port_local=18888 ; else port_local=$3 ; fi
if [ $# -lt 4 ] ; then port_target=8888 ; else port_target=$4 ; fi
if [ $# -lt 5 ] ; then port_login=$((30000+RANDOM%29999)) ; else port_login=$5 ; fi
echo "if there is an existing process, try \$release-local-port $port_local"
echo "local:$port_local <--> $login:$port_login <--> $target:$port_target"
caffeinate \
ssh -L "${port_local}:localhost:${port_login}" -t $login \
ssh -L "${port_login}:localhost:${port_target}" $target
#!/bin/bash
# parse args
if [ $# -lt 2 ] ; then
echo "usage: $0 <target-server> [local port (default: 18888)] [target port (default: 8888)]" >&2
exit 1
fi
target=$1
if [ $# -lt 2 ] ; then port_local=18888 ; else port_local=$3 ; fi
if [ $# -lt 3 ] ; then port_target=8888 ; else port_target=$4 ; fi
echo "if there is an existing process, try \$release-local-port $port_local"
echo "local:$port_local <--> $target:$port_target"
caffeinate \
ssh -L "${port_local}:localhost:${port_target}" $target
#!/bin/bash
if [ $# -lt 1 ] ; then echo "usage: $0 pid" >&2 ; exit 1; fi
port=$1
echo "sending KILL signal to the following pids..."
pids=$( lsof -i ":$port" | awk '{ if( NR>1 ){ print $2 } }'| sort | uniq )
echo $pids
if [ ! -z "$pids" ] ; then
parallel kill -KILL {} ::: $( lsof -i ":$port" | awk '{ if( NR>1 ){ print $2 } }'| sort | uniq )
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment