#!/bin/csh -f

switch (`uname -s``uname -r`)
case SunOS5*:
    set PING = /usr/sbin/ping
    set PREPING
    set POSTPING = "1"
    set SSH = /usr/local/bin/ssh
	breaksw
case Linux2*:
case Linux3*:
    set PING = /bin/ping
    set PREPING = "-c1 -w1"
    set POSTPING
    set SSH = /usr/local/bin/ssh
    if (! -e $SSH) set SSH = /usr/bin/ssh
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	case -V:
		set VVERBOSE
		shift
		breaksw
	case -f:
		set FORCE
		shift
		breaksw
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

# Rutgers hosts are now appearing in my summarize.in-addr.arpa reports/
# I think a small sleep should reduce the rate enough to get below my threshold.
# daw; 1/19/12

set SLEEP = "sleep 1"

# machines on which we know things don't work...

#set EXCEPTIONS = ( "cocoapuffs" "kilauea" "okc" "pleiades" "webshore" )
set EXCEPTIONS = ( "jetli" "luckystar" "pleiades" )

# Changing "cat" to "sort" to get ordering correct; 10/14/16
set STAFF_HOSTS = ( `sort .check-ssh-trust.staff` )
set FAC_HOSTS = ( `sort .check-ssh-trust.fac` )
set GRAD_HOSTS = ( `sort .check-ssh-trust.grad` )
set ILAB_HOSTS = ( `sort .check-ssh-trust.ilab` )
set RES_HOSTS = ( `sort .check-ssh-trust.res` )
set UG_HOSTS = ( `sort .check-ssh-trust.ug` )

#set HOST = `hostname | sed 's;\..*;;'`
set HOST = `hostname | sed 's;\.[Rr][Uu][Tt][Gg][Ee][Rr][Ss]\.[Ee][Dd][Uu];;'`

if (-e /staff/users/watrous) then
    if ($?VERBOSE) echo `date +%T` Checking staff hosts
    set HOSTS = ( `/bin/ls ~/Batch/Hosts/` )
    if ("$HOSTS" != "$STAFF_HOSTS") then
	echo Staff hosts list problem:
	echo "	$STAFF_HOSTS"
	echo "is not	$HOSTS"
	set EXIT
    endif
    set WATCHKH
    set SKH = .ssh/known_hosts
    set SKHLS = "`/bin/ls -l $SKH`"
endif
set DOMAINNAME = `domainname`
set TMP = /tmp/`basename $0`.tmp

if ($?VVERBOSE) echo `date +%T` Starting kill-hung-process

#(./kill-hung-process -kv -p $$ `basename $SSH` &) >& /dev/null
(./kill-hung-process -kv -p $$ `basename $SSH` &)

set OUT = ~/Batch/`basename $0`.$HOST
if (-e $OUT) /bin/mv $OUT{,.old}
echo $EXCEPTIONS | grep -w $HOST > /dev/null
if ($status) then
    /bin/cp /dev/null $OUT
else
    if ($?FORCE) then
	if ($?VERBOSE) echo `date` FORCE is set for exception -- continuing
	/bin/cp /dev/null $OUT
    else
	if ($?VERBOSE) echo `date` $HOST is an exception -- exiting
	exit
    endif
endif

if (! -e $SSH) exec echo $SSH does not exist on $HOST

switch ( $DOMAINNAME )
    case lcsr:
		# should be able to ssh to all staff
	breaksw
    default:
	goto fac
	endsw
endsw

if ($?VVERBOSE) echo `date +%T` Checking ssh to staff hosts

foreach RHOST ( $STAFF_HOSTS )
    if ($?VERBOSE) echo `date +%T` "   " $RHOST
    $PING $PREPING $RHOST $POSTPING >& /dev/null
    if ($status) then
	if ($?VERBOSE) echo "	$HOSTS does not respond to ping"
    endif
    $SSH $RHOST -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no domainname >& $TMP
    set STATUS = $status
    if ($?WATCHKH) then
	if ("$SKHLS" != "`/bin/ls -l $SKH`") then
	    echo $SKH updated for $RHOST
	    set SKHLS = "`/bin/ls -l $SKH`"
	endif
    endif

# SLEEP
#sleep 47				# see if we can avoid killing www.cs
    $SLEEP
if (-e $0.STOP) exec echo `date +%T` $0.STOP seen -- exiting

    if ($STATUS) then
	if ($STATUS == 255) then
	    set RESULT = `tail -1 $TMP`
	    if ("$RESULT" == "lcsr" && ! $?VERBOSE) continue
	endif
	if (! $?VERBOSE) then
	    unset SKIPERROR
	    foreach EXCEPTION ( $EXCEPTIONS )
		if ("$RHOST" == "$EXCEPTION") set SKIPERROR
	    end
	endif
	if (! $?SKIPERROR) then
	    echo ssh from $HOST to $RHOST exited with $STATUS >> $OUT
	    sed 's;\(.\);    \1;' $TMP >> $OUT
	endif
    else
#	set RESULT = `cat $TMP`
	set RESULT = `tail -1 $TMP`
	if ("$RESULT" != "lcsr") then
	    echo $RHOST thinks it\'s in $RESULT >> $OUT
	endif
    endif
end

fac:

switch ( $DOMAINNAME )
    case lcsr:
    case ringmaster:
		# should be able to ssh to all faculty
	breaksw
    default:
	goto grad
	endsw
endsw

if ($?VVERBOSE) echo `date +%T` Checking ssh to faculty hosts

foreach RHOST ( $FAC_HOSTS )
    if ($?VERBOSE) echo `date +%T` "   " $RHOST
    $PING $PREPING $RHOST $POSTPING >& /dev/null
    if ($status) then
	if ($?VERBOSE) echo "	$RHOST does not respond to ping"
	continue
    endif
    $SSH $RHOST -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no domainname >& $TMP
    set STATUS = $status
    if ($?WATCHKH) then
	if ("$SKHLS" != "`/bin/ls -l $SKH`") then
	    echo $SKH updated for $RHOST
	    set SKHLS = "`/bin/ls -l $SKH`"
	endif
    endif

# SLEEP
#sleep 47				# see if we can avoid killing www.cs
    $SLEEP
if (-e $0.STOP) exec echo `date +%T` $0.STOP seen -- exiting

    if ($STATUS) then
	if ($STATUS == 255) then
	    set RESULT = `tail -1 $TMP`
	    if ("$RESULT" == "ringmaster" && ! $?VERBOSE) continue
	endif
	if (! $?VERBOSE) then
	    unset SKIPERROR
	    foreach EXCEPTION ( $EXCEPTIONS )
		if ("$RHOST" == "$EXCEPTION") set SKIPERROR
	    end
	endif
	if (! $?SKIPERROR) then
	    echo ssh from $HOST to $RHOST exited with $STATUS >> $OUT
	    sed 's;\(.\);    \1;' $TMP >> $OUT
	endif
    else
#	set RESULT = `cat $TMP`
	set RESULT = `tail -1 $TMP`
	if ("$RESULT" != "ringmaster") then
	    echo $RHOST thinks it\'s in $RESULT >> $OUT
	endif
    endif
end

grad:

switch ( $DOMAINNAME )
    case lcsr:
    case ringmaster:
    case lcsr-grad:
		# should be able to ssh to all grad
	breaksw
    default:
	goto ilab
	endsw
endsw

if ($?VVERBOSE) echo `date +%T` Checking ssh to grad hosts

foreach RHOST ( $GRAD_HOSTS )
    if ($?VERBOSE) echo `date +%T` "   " $RHOST
    $PING $PREPING $RHOST $POSTPING >& /dev/null
    if ($status) then
	if ($?VERBOSE) echo "	$RHOST does not respond to ping"
	continue
    endif
    $SSH $RHOST -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no domainname >& $TMP
    set STATUS = $status
    if ($?WATCHKH) then
	if ("$SKHLS" != "`/bin/ls -l $SKH`") then
	    echo $SKH updated for $RHOST
	    set SKHLS = "`/bin/ls -l $SKH`"
	endif
    endif

# SLEEP
#sleep 47				# see if we can avoid killing www.cs
    $SLEEP
if (-e $0.STOP) exec echo `date +%T` $0.STOP seen -- exiting

    if ($STATUS) then
	if ($STATUS == 255) then
	    set RESULT = `tail -1 $TMP`
	    if ("$RESULT" == "lcsr-grad" && ! $?VERBOSE) continue
	endif
	if (! $?VERBOSE) then
	    unset SKIPERROR
	    foreach EXCEPTION ( $EXCEPTIONS )
		if ("$RHOST" == "$EXCEPTION") set SKIPERROR
	    end
	endif
	if (! $?SKIPERROR) then
	    echo ssh from $HOST to $RHOST exited with $STATUS >> $OUT
	    sed 's;\(.\);    \1;' $TMP >> $OUT
	endif
    else
#	set RESULT = `cat $TMP`
	set RESULT = `tail -1 $TMP`
	if ("$RESULT" != "lcsr-grad") then
	    echo $RHOST thinks it\'s in $RESULT >> $OUT
	endif
    endif
end

ilab:

switch ( $DOMAINNAME )
    case lcsr:
    case ringmaster:
    case lcsr-grad:
    case dcs-ilab:
		# should be able to ssh to all ilab
	breaksw
    default:
	goto res
	endsw
endsw

if ($?VVERBOSE) echo `date +%T` Checking ssh to cereal hosts

foreach RHOST ( $ILAB_HOSTS )
    if ($?VERBOSE) echo `date +%T` "   " $RHOST
    $PING $PREPING $RHOST $POSTPING >& /dev/null
    if ($status) then
	if ($?VERBOSE) echo "	$RHOST does not respond to ping"
	continue
    endif
    $SSH $RHOST -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no domainname >& $TMP
    set STATUS = $status
    if ($?WATCHKH) then
	if ("$SKHLS" != "`/bin/ls -l $SKH`") then
	    echo $SKH updated for $RHOST
	    set SKHLS = "`/bin/ls -l $SKH`"
	endif
    endif

# SLEEP
#sleep 47				# see if we can avoid killing www.cs
    $SLEEP
if (-e $0.STOP) exec echo `date +%T` $0.STOP seen -- exiting

    if ($STATUS) then
	if ($STATUS == 255) then
	    set RESULT = `tail -1 $TMP`
	    if ("$RESULT" == "dcs-ilab" && ! $?VERBOSE) continue
	endif
	if (! $?VERBOSE) then
	    unset SKIPERROR
	    foreach EXCEPTION ( $EXCEPTIONS )
		if ("$RHOST" == "$EXCEPTION") set SKIPERROR
	    end
	endif
	if (! $?SKIPERROR) then
	    echo ssh from $HOST to $RHOST exited with $STATUS >> $OUT
	    sed 's;\(.\);    \1;' $TMP >> $OUT
	endif
    else
#	set RESULT = `cat $TMP`
	set RESULT = `tail -1 $TMP`
	if ("$RESULT" != "dcs-ilab") then
	    echo $RHOST thinks it\'s in $RESULT >> $OUT
	endif
    endif
end

res:

switch ( $DOMAINNAME )
    case lcsr:
    case ringmaster:
    case lcsr-grad:
    case research:
		# should be able to ssh to all research
	breaksw
    default:
	goto ug
	endsw
endsw

if ($?VVERBOSE) echo `date +%T` Checking ssh to research hosts

foreach RHOST ( $RES_HOSTS )
    if ($?VERBOSE) echo `date +%T` "   " $RHOST
    $PING $PREPING $RHOST $POSTPING >& /dev/null
    if ($status) then
	if ($?VERBOSE) echo "	$RHOST does not respond to ping"
	continue
    endif
    $SSH $RHOST -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no domainname >& $TMP
    set STATUS = $status
    if ($?WATCHKH) then
	if ("$SKHLS" != "`/bin/ls -l $SKH`") then
	    echo $SKH updated for $RHOST
	    set SKHLS = "`/bin/ls -l $SKH`"
	endif
    endif

# SLEEP
#sleep 47				# see if we can avoid killing www.cs
    $SLEEP
if (-e $0.STOP) exec echo `date +%T` $0.STOP seen -- exiting

    if ($STATUS) then
	if ($STATUS == 255) then
	    set RESULT = `tail -1 $TMP`
	    if ("$RESULT" == "research" && ! $?VERBOSE) continue
	endif
	if (! $?VERBOSE) then
	    unset SKIPERROR
	    foreach EXCEPTION ( $EXCEPTIONS )
		if ("$RHOST" == "$EXCEPTION") set SKIPERROR
	    end
	endif
	if (! $?SKIPERROR) then
	    echo ssh from $HOST to $RHOST exited with $STATUS >> $OUT
	    sed 's;\(.\);    \1;' $TMP >> $OUT
	endif
    else
#	set RESULT = `cat $TMP`
	set RESULT = `tail -1 $TMP`
	if ("$RESULT" != "research") then
	    echo $RHOST thinks it\'s in $RESULT >> $OUT
	endif
    endif
end

ug:
goto done			# no more ug; 10/19/15

switch ( $DOMAINNAME )
    case lcsr:
    case ringmaster:
    case lcsr-grad:
    case dcs-ug:
		# should be able to ssh to all undergrad
	breaksw
    default:
	goto done
	endsw
endsw

if ($?VVERBOSE) echo `date +%T` Checking ssh to undergrad hosts

foreach RHOST ( $UG_HOSTS )
    if ($?VERBOSE) echo `date +%T` "   " $RHOST
    $PING $PREPING $RHOST $POSTPING >& /dev/null
    if ($status) then
	if ($?VERBOSE) echo "	$RHOST does not respond to ping"
	continue
    endif
    $SSH $RHOST -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no domainname >& $TMP
    set STATUS = $status
    if ($?WATCHKH) then
	if ("$SKHLS" != "`/bin/ls -l $SKH`") then
	    echo $SKH updated for $RHOST
	    set SKHLS = "`/bin/ls -l $SKH`"
	endif
    endif

# SLEEP
#sleep 47				# see if we can avoid killing www.cs
    $SLEEP
if (-e $0.STOP) exec echo `date +%T` $0.STOP seen -- exiting

    if ($STATUS) then
	if ($STATUS == 255) then
	    set RESULT = `tail -1 $TMP`
	    if ("$RESULT" == "dcs-ug" && ! $?VERBOSE) continue
	endif
	if (! $?VERBOSE) then
	    unset SKIPERROR
	    foreach EXCEPTION ( $EXCEPTIONS )
		if ("$RHOST" == "$EXCEPTION") set SKIPERROR
	    end
	endif
	if (! $?SKIPERROR) then
	    echo ssh from $HOST to $RHOST exited with $STATUS >> $OUT
	    sed 's;\(.\);    \1;' $TMP >> $OUT
	endif
    else
#	set RESULT = `cat $TMP`
	set RESULT = `tail -1 $TMP`
	if ("$RESULT" != "dcs-ug") then
	    echo $RHOST thinks it\'s in $RESULT >> $OUT
	endif
    endif
end

done:

if ($?VVERBOSE) echo `date +%T` Done
