Skip to content

Instantly share code, notes, and snippets.

@mzlee
Created April 15, 2014 18:19
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 mzlee/10755298 to your computer and use it in GitHub Desktop.
Save mzlee/10755298 to your computer and use it in GitHub Desktop.
#!/bin/bash
## Source this to add the functions to your environment
function choice {
file=${1}
lc=$(wc -l < $file)
if [ ${lc} -gt 0 ]; then
sed -n "$((1 + $RANDOM % ${lc})) p" ${file}
fi
}
function check_machine {
local ulist=$(ssh $LAST_PUBLIC "users");
for u in ${ulist}; do
if [ ${u} != "$(whoami)" ]; then
LAST_PUBLIC=""
fi
done
}
function new_machine {
export LAST_PUBLIC=""
pub_machine
}
function pub_machine {
local DOMAIN=${DOMAIN:-hosts.txt}
if [ -f ${HOME}/.${DOMAIN} ]; then
if [ -n "${LAST_PUBLIC}" ]; then
check_machine
fi
while [ -z "${LAST_PUBLIC}" ]; do
local mach=$(choice ${HOME}/.${DOMAIN})
if [ ${mach:0:1} == "#" ]; then
continue
fi
echo "Trying ${mach}..."
LAST_PUBLIC="${mach}.${DOMAIN}"
check_machine
done
fi
export LAST_PUBLIC
}
function pub_connect {
local DOMAIN=${DOMAIN:-hosts.txt}
if [ -f ${HOME}/.${DOMAIN} ]; then
pub_machine
ssh $(whoami)@${LAST_PUBLIC} ${@}
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment