Skip to content

Instantly share code, notes, and snippets.

@mnlwldr
Last active March 23, 2016 13:18
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 mnlwldr/debdae00549f8d1a06b9 to your computer and use it in GitHub Desktop.
Save mnlwldr/debdae00549f8d1a06b9 to your computer and use it in GitHub Desktop.
create a ssh tunnel using autossh && random from ~/.ssh/config
#!/bin/bash
# ssh user config (http://www.openbsd.org/cgi-bin/man.cgi?query=ssh_config)
SSH_CONFIG=~/.ssh/config
# local port
PORT=8888
# array with hosts from SSH_CONFIG.
# append "#socks5" at the end of the Host line.
# Example: "Host yourHost #socks5"
eval HOSTS=( $(awk '/^Host(.*?)#socks5$/ {print $2}' ${SSH_CONFIG}) )
#HOSTS=(weidmann-prod)
N=1
for index in `shuf --input-range=0-$(( ${#HOSTS[*]} - 1 )) | head -${N}`
do
echo -en "Host: ${HOSTS[$index]} Port: ${PORT}\n"
# using autossh - Automatically restart SSH sessions and tunnels
AUTOSSH_DEBUG=1
AUTOSSH_GATETIME=0
autossh -M 20000 -N -D ${PORT} ${HOSTS[$index]}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment