=== added file 'bin/ssh-slave' --- bin/ssh-slave 1970-01-01 00:00:00 +0000 +++ bin/ssh-slave 2015-10-14 19:22:19 +0000 @@ -0,0 +1,78 @@ +#!/bin/zsh +have_log=0 +die() { + (($#)) && print -r - "$*" >&2 + for jnum jtext in ${(kv)jobtexts}; do + kill %$jnum + done + exit 1 +} + +noconnect=( + -o ControlMaster=no + -o PubkeyAuthentication=no + -o KbdInteractiveAuthentication=no + -o PasswordAuthentication=no +) + +# prevent logging to syslog instead of stderr +filtered=() +for arg in "$@"; do + case $arg in + (--*) filtered+=$arg;; + (-o*) filtered+=$arg;; + (-y);; + (-*) filtered+=${arg/y/};; + (*) filtered+=$arg;; + esac +done + +check="$( ssh -O check "$filtered[@]" 2>&1 )" +if (($?)); then + for line in ${(f)check}; do + if [[ $line == 'Control socket connect('* ]]; then + master_path=${${check%)*}#*\(} + fi + done + [[ -z $master_path ]] && die 'could not determine master path' + [[ $master_path != */.ssh/master-* ]] && die master path $master_path failed sanity check + svdir=${master_path:h}/service + svname=${master_path:t} + s=$svdir/$svname + mkdir -p $s + print -r - 'args=(' ${(q)@} ')' >$s/args + if [[ -n $DISPLAY ]]; then + print -r export DISPLAY=${(q)DISPLAY} + print -r export SSH_ASKPASS=/usr/bin/x11-ssh-askpass + fi >> $s/args + touch $s/down + [[ -a $s/run ]] && rm $s/run + ln -s ${commands[sv.ssh-master.run]} $s/run + [[ -a $s/check ]] && rm $s/check + ln -s ${commands[sv.ssh-master.check]} $s/check + if [[ -o xtrace ]]; then + zsh -x ${commands[usv.bg]} $svdir || die + else + usv.bg $svdir || die + fi + while ! [[ -e $s/supervise/control ]]; do + sleep 0.1 + done + have_log=0 + if [[ -r /var/log/msg/current ]]; then + tail -F -n 0 /var/log/msg/current | grep ' ssh\[[0-9]\+\]: ' >&2 & + have_log=1 + fi + printf o >$s/supervise/control || die + out=$(sv -w 15 check $s 2>&1$) || die $out + if (($have_log)); then + for jnum jtext in ${(kv)jobtexts}; do + kill %$jnum + done + wait + fi + if [[ "$(<$s/supervise/stat)" != run* ]];then + die 'could not start ssh master' + fi +fi +exec ssh $noconnect "$@" === added file 'bin/sv.ssh-master.check' --- bin/sv.ssh-master.check 1970-01-01 00:00:00 +0000 +++ bin/sv.ssh-master.check 2014-02-14 02:27:12 +0000 @@ -0,0 +1,3 @@ +#!/bin/zsh +source ./args +ssh -O check "${args[@]}" &>/dev/null === added file 'bin/sv.ssh-master.run' --- bin/sv.ssh-master.run 1970-01-01 00:00:00 +0000 +++ bin/sv.ssh-master.run 2014-02-14 02:27:12 +0000 @@ -0,0 +1,19 @@ +#!/bin/zsh + +autoload find_ssh_agent; find_ssh_agent +source ./args + +# check for already running master or stale socket +check="$( command ssh -O check "$args[@]" 2>&1 )" +if (($?)); then + for line in ${(f)check}; do + if [[ $line == 'Control socket connect('* ]]; then + master_path=${${check%)*}#*\(} + fi + done + [[ -a $master_path ]] && rm $master_path +else + command ssh -O exit "$args[@]" +fi + +exec ssh -y -n -o LogLevel=VERBOSE -o ControlMaster=yes -N "${args[@]}" === added symlink 'ssh-slave' === target is u'bin/ssh-slave' === added symlink 'sv.ssh-master.check' === target is u'bin/sv.ssh-master.check' === added symlink 'sv.ssh-master.run' === target is u'bin/sv.ssh-master.run'