Verified Commit a4908e8b authored by Fangyi Zhou's avatar Fangyi Zhou
Browse files

Update whereisthatguy, deprecate isconnected

parent 4912cebc
#!/bin/bash
all=false
bare=false
prefix=""
file=1
timeout="-o ConnectTimeout=5"
ssh_opts="-q -o StrictHostKeyChecking=no"
while getopts :abpf:t:s:S: opt; do
case $opt in
a)
all=true ;;
b)
bare=true ;;
p)
prefix="\b" ;;
f)
echo -n > $OPTARG
exec {file}>"$OPTARG" ;;
t)
timeout="-o ConnectTimeout=$OPTARG" ;;
s)
ssh_opts="$ssh_opts $OPTARG" ;;
S)
ssh_opts="$OPTARG" ;;
esac
done
ssh_opts="$ssh_opts $timeout"
computer=${!OPTIND}
((OPTIND++))
username="$prefix${!OPTIND}"
if [ "$all" = true ]; then
found=$(ssh $ssh_opts $computer "who | grep '$username'" 2>/dev/null)
if [[ $found == *$username* ]]; then
if [ "$bare" = true ]; then
echo $computer $found >&${file}
else
echo "Found" $found "at" $computer >&${file}
fi
fi
else
found=$(ssh $ssh_opts $computer "finger 2>/dev/null| tr -s ' ' | grep -E 'tty[7-9]|tty[[:digit:]]{2,}' | cut -d ' ' -f1-3 | grep -E -i '$prefix$username' " 2>/dev/null)
if [[ -n $found ]]; then
if [ "$bare" = true ]; then
echo $computer $found >&${file}
else
echo "Found "$(echo $found | cut -d ' ' -f1)" ("$(echo $found | cut -d ' ' -f2-3)") at "$computer >&${file}
fi
fi
fi
#!/bin/bash
set -e
pushd `dirname $0` > /dev/null
location="$(dirname "$(readlink -f "$0")")"
pushd $location > /dev/null
hostlist=(`./gethosts`)
exclusions=$(curl http://www.doc.ic.ac.uk/~pk3115/LabStalker/exclude 2>/dev/null)
2>/dev/null
all=false
bare=false
timeout=4
onlineExclusions=true
ssh_opts="-q -o StrictHostKeyChecking=no"
usage () {
echo "Usage: whereisthatguy [flags] NAME"
echo "-E: Don't use online computer exclusion list"
echo "-e STRING: Use your own exclusion list, supplied as a single STRING"
echo "-a: Show all results (including non-tty)"
echo "-b: Show bare results"
echo "-h: Print out this help"
echo "-T: Don't limit the time to connect and get results"
echo "-t N: Limit the time to connect and get results to N seconds"
exit 0
}
while getopts :Ee:abh:Tt: opt; do
case $opt in
E)
onlineExclusions=false ;;
e)
exclusions=$OPTARG
onlineExclusions=false ;;
a)
all=true ;;
b)
bare=true ;;
h)
usage ;;
T)
timeout=0 ;;
t)
timeout=$OPTARG ;;
*)
usage ;;
esac
done
if [ "$onlineExclusions" = true ]
then
#exclusions=$(curl http://www.doc.ic.ac.uk/~pk3115/LabStalker/exclude 2>/dev/null)
exclusions=$(cat /homes/pk3115/Public/LabStalker/exclude)
fi
if [[ "tui vm-shell2 vm-shell3 vm-shell4 vm-shell1" =~ $(hostname | cut -d'.' -f1) ]]
then
echo "Please, don't run this script on shell machines" >&2
exit 1
fi
ssh_opts="$ssh_opts -o ConnectTimeout=$timeout "
if [[ -n "$1" ]]
then
username=$1
username=${!OPTIND}
else
username="$USER" # defaults to current user
fi
if [ "$all" = true ]; then
command_to_execute="who | grep '$username'"
else
command_to_execute="finger 2>/dev/null | tr -s ' ' | grep -E 'tty[7-9]|tty[[:digit:]]{2,}' | cut -d ' ' -f1-3 | grep -E -i '$username' "
fi
function process_output {
host=$1
found=$2
if [ "$all" = true ]; then
if [[ $found == *$username* ]]; then
if [ "$bare" = true ]; then
echo $host $found
else
echo "Found" $found "at" $host
fi
fi
else
if [[ -n $found ]]; then
if [ "$bare" = true ]; then
echo $host $found
else
echo "Found "$(echo $found | cut -d ' ' -f1)" ("$(echo $found | cut -d ' ' -f2-3)") at "$host
fi
fi
fi
}
for host in ${hostlist[*]}
do
if ! [[ $exclusions =~ $host ]]
then
timeout -k 1 4 ./isconnected $2 $3 $host $username &
process_output $host "$(echo $(timeout -k 0.1 $timeout ssh $ssh_opts $host "$command_to_execute" 2>/dev/null) $host)" $all $bare $username &
fi
done
wait >/dev/null 2>&1
popd > /dev/null
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment