A bot to help FTP searches |
Back to bots.htm | |
fra_00xx 980921 darkwyrm 1000 BO PC | Feedback: darkwyrm(at)home(point)com | |
FTP Search, while useful in its own right like any other search engine, still suffers the same limitations any other search engine has - a, well, obscene amount of information to index and constant change of this information from moment to moment. To work around this, you need to use a tool that is not nearly as broad in scope as a search engine. A bot will work nicely.
I won't explain the code too much -- if you don't know Perl, learn it. If you do, then my first programming project will likely be an exercise in boredom. The code is fairly well-commented and straight-forward. If you wish to find what you want, you must have a desire to be good at looking for it.
In short, this script takes two arguments - a URL and a filename. It logs in and literally reads every directory and filename on the site. If it finds another directory, it adds it to the list of paths to search. If not, it checks to see if the file is a match. Only the first result is displayed on the screen. No wildcards, no cool file I/O, and almost no error checking for now. ;^) This was tested on Win32 Perl, but shouldn't cause any problem on other platforms. As a bot, it doesn't play nice - it wastes no time in searching for stuff, but I doubt it will cause any serious bandwidth problems. Don't blame me if some nice admin sends you a very, um, polite e-mail about this.
#! /usr/bin/perl -w############################################################################ ########### # FTP Site Searchbot ############################################################################ ###########
# Current functionality - searches site & exits if it finds a match. Tweak to your # heart's content
# Mods to include use Net::FTP; use Cwd; use Getopt::Std;
$ftp ="";
$remotehost = ''; $remotedir = '/'; # current path on remote host $username = 'anonymous'; $password = '[email protected]'; $isdir=0; $pathindex = 0; $currentfile = ""; # placeholder var for file name in current entry $filename = ""; # Name of file to be searched for $done=0; # Exit flag @dirlisting = ""; # Holds the listing generated by a dir command @currententry=""; # Current entry in question in a dirlisting @pathlist="/"; # List of all the directories on a site
# Start off the process by getting host and file from cmdline $remotehost=lc($ARGV[0]); $filename=lc($ARGV[1]);
# Did they even give us a host and filename? if ( !($remotehost) or !($filename) ) { print "\nSyntax:\n\tftpbot.pl URL filename.xxx\n"; exit 4; }
# Log in if ($ftp = Net::FTP->new("$remotehost",0)) { print "Connected to host $remotehost.\n"; } else { print "Couldn't connect to host $remotehost.\n"; exit 5; }
print "Logging in as $username.\n";
if ($ftp->login("$username","$password")) { print "Login successful.\n"; } else { print "Couldn't login"; exit 10; }
# Start from root and search
while(defined($pathlist[$pathindex])) {
# Change to new directory $remotedir = $pathlist[$pathindex]; $ftp->cwd("$remotedir");
# Get new directory listing @dirlisting = $ftp->dir();
#extract directories from listing, save, and print for($i=0; $i<=$#dirlisting; $i++) { @currententry = split / /, $dirlisting[$i];
# Skip any 'total files' listings if( (lc $currententry[0]) eq 'total') {next;} $isdir = substr($currententry[0],0,1);
$currentfile = lc($currententry[$#currententry]);
# If entry is a directory, save and print if($isdir eq 'd')
# Construct full path and place at end of list push @pathlist, ($remotedir . $currentfile . '/'); } elsif($currentfile eq $filename) { # This sucker's a match. Print & quit. print "$remotedir" . "$currentfile" . '/'; $done = 1; last; } } $pathindex++; if ($done) {last;} }
print "\nClosing connection to $remotehost.\n"; $ftp->quit;
|
Back to bots.htm |