#!/usr/bin/csh #FILE: #lookfor [-a] username [hostfile] #Coded by: Thomas Thompson #Finds a user on multiple machines #List of machines to search comes from hostfile or the enviornment #variable HOSTFILE # Check for at least enough arguments if ( 1 > $#argv) then echo "No name given, exiting. Usage: " echo "lookfor [-a] username [hostfile]" exit(-1) endif #This line sets the command line option to check on all machines #and continue eventhough the person is found on one machine... set all_flag = -a set Go_on = 0 #This is the argument check if the all flag is set in the command #arguments if($argv[1] == $all_flag) then if (2 > $#argv) then echo "Not enough arguments for this option, exiting. Usage: " echo "lookfor [-a] username [hostfile]" exit (-1) endif #Set's go_on flag so that we can skip the break if found #on one machine and there are more in the list set Go_on = 1 endif #Past the command line minimum requirements, now on to the #finding area :) #Set up vars so we don't have to keep accessing the command line args set hostfile = 0 if($Go_on == 1) then set Search_for = $argv[2] if($#argv > 2) then set hostfile = 1 set Host_File = $argv[3] if($#argv > 3) then echo " Warning: More than 1 hostfile specified," echo " only the first filename will be used." endif endif else set Search_for = $argv[1] if($#argv > 1) then set hostfile = 1 set Host_File = $argv[2] if($#argv > 2) then echo " Warning:More than 1 hostfile specified," echo " only the first filename will be used." endif endif endif if ($hostfile == 1) then set listing = `cat $Host_File` if($status != 0) then echo "Unable to open file, please specify an existing file name" exit (-1) endif else set listing = `echo $HOSTSFILE` if($listing == "") then echo "No Hostsfile specified and HOSTSFILE enviornment variable not set." #set listing equal to localhost for look so we don't fail set listing = $HOST echo "Looking on $HOST (localhost)..." #Okay, now we're ready to continue :) endif endif set i = 1 set found = 0 #TEST FOR LOCAL MACHINE IN LIST set test_local = 0 while ( $i <= $#listing ) if ( $listing[$i] == $HOST ) then set test_local = 1 endif @ i++ end #IF LOCALHOST NOT FOUND, ADD IT TO END OF LISTING if ($test_local == 0) then set listing = `echo $HOST $listing` endif #RESET i set i = 1 set okay = 0 #LOOK FOR USER while ( $i <= $#listing ) if( $listing[$i] != $HOST ) then ping $listing[$i] > /dev/null if($status != 0) then set okay = 1 else set okay = 0 endif else set okay = 0 endif if($okay != 0) then echo Machine $listing[$i] is unreachable. Skipping to next machine. else rsh $listing[$i] who | grep $Search_for > /dev/null if($status == 0) then echo Found $Search_for on $listing[$i] set found = 1 if( $Go_on == 0) then exit (0) endif endif endif @ i++ end if($found == 0) then echo Unable to find $Search_for endif exit (0)