Text File

#!/usr/local/bin/perl # go4check, v1.1 # #------------------------------------------------------------------------------- # Introduction # go4check checks gopher links, probing each connection and testing the # output received. It handles most types of links, reporting if the link # is ok, the host serving it is down/refusing connections, or its pathname # has changed. It is not 100% successful at this, especially when it # comes to gopher0 servers, but does indeed help you keep on top of links # in your server(s). # # To run, go4check requires only perl and socket.ph. It understands # gopher0 and gopher+ servers. # # go4check produces a line of output on stdout for each item appearing # in a gopher's menu: the name of the item plus a result. Indentation # serves to maintain items in context so problems can be located easily. # As an extra benefit, go4check's output can be used as a roadmap of # the gopher after some rather trivial editing to remove results. # # go4check is written by George A. Theall, George.A.Theall@mail.tju.edu. # You may freely use and redistribute this. I can not offer any # support for this but am interested in your comments, suggestions, # and problem reports. # #------------------------------------------------------------------------------- # Operation # Before you run go4check, make sure perl and the header file socket.ph are # available on your system. [You can generate this file by running the perl # utility h2ph on /usr/include/sys/socket.h, or something similar.] # # Invoke go4check with the name of the server to check and an optional port # number. Other options can be used to specify a non-standard starting # path or generate copious debugging info. go4check will test the items # listed in the initial menu and recurse into any menus it finds as long # as the names of server it finds match the one specified at go4check's # invocation. go4check does, though, skip recursion if pathnames refer # to ftp gateways or point back to the initial entry point. # # Results are directed to stdout, so you probably will want to redirect # to a file. You might then remove instances of "...ok.", which # indicate no problems and finally search on "...can't connect.", # "...path changed.", and "...timed out.". Another possible result # is "...n/a.", which is used when go4check doesn't know how to check # a particular type of link. # # You may want to tune the variables that go4check uses for testing # items of type 2 and 7. See below where initial values are defined. # For items of type 2, go4check sends a invalid command, which causes # many CSO servers to respond in a way that go4check interprets as a # success. As for items of type 7, I don't know of any robust way # to test searches. Currently, the best solution appears to be # to search for a word that's common to whatever searches are in the # gopher being checked. # # go4check is slow; it probably belongs in a cron job to run at night. # #------------------------------------------------------------------------------- # History # 31-Jan-95, GAT, v1.1 # - Alarms are now used to abort connections that are otherwise hung. # - Added patches from R.D. Cameron for supporting type 7 items with # non-empty paths and checking error returns of type 3. # - Fixed glitch that arose on some servers (gopher.uwsp.edu for one) # that return lines with non-standard endings. # - Explicitly added an assignment for $| and set it to true so output # will be flushed after every print. # # 17-Oct-94, GAT # - Added a semicolon after a line in make_URL. Its lack appears to # cause problems with some versions of Perl. # # 01-Sep-94, GAT, v1.0 # - Released publically. # # 10-Aug-94, GAT, v1.0b2 # - Added as a way to control how long to pause after # establishing a connection. # - Fixed initialization of %URLs. # - Changed format of internal URLs by removing ":" from between type # and path info. # - Used a configurable word to check search items. # - Added check of CSO servers. # - Adjusted regular expression used to check success/failure of # a link. # - Documented go4check's operation. # # 12-Jul-94, GAT, v1.0b1 # - Used pseudo URLs internally for storing links so they are not # checked more than once. # - Added support for most types of links, including telnet, binary # files, and searches. # - Used gopher+ protocol whenever possible to avoid retrieving # entire files. # # 09-Jun-94, GAT, v1.0a # - First version of go4check. Checks only files and directories. # #-------------------------------------------------------------------------------

# Specify where perl can find include files. push(@INC, "/usr/local/lib/perl"); push(@INC, "/usr/local/lib/perl/sys");

# Define initial values for selected variables. $| = 1; # flush after every print? = "helo"; # for searching type 2 items = "cancer"; # for searching type 7 items = " "; # indentation at each level = 3; # time to snooze after connect = 300; # max len of connect (seconds) %URLs = (); # array of URL's on server

# Check for options. = 0; # default to no debug if ([0] eq '-d') { shift; = 1; }

# Parse commandline args and provide help as needed. = shift || ""; # name of host to check = shift || 70; # port number = shift || ""; # initial directory if ( eq "" || eq "-?") { print "$0 checks links in a gopher by probing connections\n\n"; print "Usage: $0 [-d] host [port] [\"path\"]\n"; print " unless specified, port defaults to 70 and path to \"\".\n"; print " -d is used for debugging.\n"; exit(9); }

# Set up subroutines to catch some alarms. {'ALRM'} = handle_Timeout;

# Establish connection and check links. require 'socket.ph'; chop( = `hostname`); # needed for tcpconnect &check_Links(, , ); exit(0);

######################################################################## # check_Links - checks links for a given directory. # # # # Notes: # # - Links on the same host will be followed unless they point to # # the root. While this will prevent most recursion, there may # # be some gophers with odd setups that lead to infinite loops. # # - FTP links are not followed. # # Entry: # # host = hostname # # port = port number # # path = selector string # # Exit: # # New links are appended to @URLs. # ######################################################################## sub check_Links { local(, , ) = @_; local() = . ; local(); local(@Items);

# Establish connection and read contents. alarm(); && print "DEBUG: connecting to at port .\n"; () = &tcpconnect(, ); () || die "Can't connect"; && print "DEBUG: sending path \"\".\n"; send(, "\r\n", 0); @Items = <>; close(); alarm(0);

# Check each item, recursing into directories as necessary. foreach (@Items) { local(, , , , , );

s/\s*$//; # remove \r\n combo last if (/^\.$/); # done if line is just a period

# Check status of each unique URL. = &make_URL(); s/^(.)// && ( = $1); (, , , , ) = split(/\t/, ); chop() if ( =~ /\.$/); if (defined({})) { # already checked print "...{}.\n"; } else { = ({} = &test_URL(, )); print "....\n"; }

# Recurse as necessary. if ( eq "ok" && == 1 && eq && eq && ne "" && !~ /ftp.*:/) { &check_Links(, , ); } } }

################################################ # make_URL - constructs a URL from a string. # # # # Notes: # # - The URLs generated here are not 100% # # kosher, only used internally. # # # # Entry: # # string as passed by gopher server. # # Exit: # # string representing URL. # ################################################ sub make_URL { local() = @_; local(); local(, , , , );

s/^(.)// && ( = $1); (, , , ) = split(/\t/, ); chop() if ( =~ /\.$/); if ( =~ /[01245679sgMhIi]/) { = "gopher://:/"; } elsif ( =~ /[8T]/) { = "telnet://"; !~ /^$/ && .= "@"; .= ; > 0 && .= ":"; .= "/"; } return(); }

########################################################################### # test_URL - check that a URL is accessible. # # # # Notes: # # - I don't have a good way to check gopher0 servers. Currently, I # # look for the string "error.host", which servers like gn seem to # # generate. However, this fails with KA9Q, for which an error # # message is indistinguishable from regular text. # # - For gopher+, a error code indicating a server is too busy is # # treated as an error. This may not be the right thing to do. # # - If the server understands gopher+, we'll only ask for info (!) # # so as not to retrieve large files. This approach also seems to # # be the only way to check ASK blocks reliably. # # - CSO nameservers (type 2) are checked with an invalid command - # # this returns a warning message from the server that is not # # regarded as an error by go4check. Using the command "fields" # # does *not* work since this typically results in lines starting # # with -2, which look like errors. # # - Checks of telnet links only see if host is up; no attempt # # is made to login to whatever account may be specified. # # - Checks of FTP links could be improved. Currently, the info # # returned is not examined beyond looking for the usual signs # # of failure. # # Entry: # # URL = URL to test # # GPLUS = extra character indicating a gopher+ item. # # Exit: # # Text string indicating status of URL: # # "ok" = everything ok # # "can't connect" = can't connect to host # # "path changed" = path changed # # "n/a" = unknown status # ########################################################################### sub test_URL { local(, ) = @_; local(, , , , , ); local($1, $2, $3, $4, $5);

&& print "DEBUG: checking .\n"; m#^(\w+)://(.*):(\d+)/?(.?)(.*)#; = $1; = $2; = $3; = $4; = $5; if ( =~ /@/) { (, ) = split(/@/, ); } && print "protocol=; logonid=; host=; port=; type=; path=.\n";

# Check gopher links. if ( eq "gopher") { local(); local();

&& print "DEBUG: checking gopher at ;.\n"; eval { alarm(); () = &tcpconnect(, ); alarm(0); }; if ($@ && $@ =~ /Timed Out/) { return "timed out"; } () || return "can't connect"; .= "\t!" if (); # Modify selector to get only info if ( eq "2") { = if ( =~ /^$/); } elsif ( eq "7") { # Modification Oct. 19/94 by R.D. Cameron to append # handle the nonempty case: to test in this # case, we send a tab and the search term after the # . if ( =~ /^$/) { = ; } else { = "\t"; } =~ s#^waissrc:(.*)/.*$#1$1#; } && print "DEBUG: sending path \"\".\n"; eval { alarm(); send(, "\r\n", 0); alarm(0); }; if ($@ && $@ =~ /Timed Out/) { return "timed out"; } = <>; close(); && print "DEBUG: read \"\".\n";

# Test line for signs of errors. # # Modification Oct. 19/94 by R.D. Cameron to # check for type 3 error returns when a directory # listing is expected. (According to the gopher # protocol, "3" as the first character of a directory # entry always indicates error. if ((( eq "1") | ( eq "7")) & ( =~ /^3/)) { return("path changed"); } # Test line for other signs of errors. elsif ( =~ /(^\-\-\d)|(\terror.host\t\d+)/) { return("path changed"); } else { return("ok"); } }

# Check telnet links. if ( eq "telnet") { local();

&& print "DEBUG: checking telnet at ;.\n"; () = &tcpconnect(, ); () || return "host down"; return "ok"; close(); }

# If we get here, we don't know how to test the link. return("n/a"); }

################################################################ # This comes from gopherhunt by Paul Lindner. # # # # I've added a line to abort if it can't resolve an address. # # and return 0 if failure rather than die. GAT # ################################################################ sub tcpconnect { #Get TCP info in place local(, ) = @_; local(, , , ); local(, , , ); local(); = 'S n a4 x8';

(,,) = getprotobyname('tcp'); (,,) = getservbyname(, 'tcp') unless =~ /^\d+$/; (,,,,) = gethostbyname(); (,,,,) = gethostbyname(); || return(0);

= pack(, &AF_INET, 0, ); = pack(, &AF_INET, , );

sleep();

socket(N, &PF_INET, &SOCK_STREAM, ) || return(0); bind(N, ) || return(0); connect(N, ) || return(0);

return(N); }

##################################################### # handle_Timeout - Die with a specific message. # # # # Notes: # # - Calls to alarm() should be in an eval # # block. # # # # Entry: # # n/a # # Exit: # # Message "Timed Out" is returned. # ##################################################### sub handle_Timeout { die "Timed Out"; }