Author Topic: [Perl] FTP Search Engine  (Read 661 times)

0 Members and 1 Guest are viewing this topic.

Offline Freak

  • /dev/null
  • *
  • Posts: 17
  • Cookies: 3
    • View Profile
[Perl] FTP Search Engine
« on: March 20, 2015, 03:24:25 am »
Hello,

This program takes in a file containing addresses for FTP servers and, for each address, crawls through every available directory recursively printing filenames and pathnames containing either all or any of a set of user inputted keywords to a file kinda like a search engine.

At each iteration through the list of addresses the user is prompted for the username and password of the FTP server (You can just leave them blank for anonymous FTP).

Here's a screenshot of an example output for ftp://ftp.kernel.org/ with the keyword "rpm":


If you un-comment line 36 it will print out each directory that it crawls to as it crawls to them. I like to run it that way because then you can actually tell that it's making progress instead of just staring and waiting at a blank terminal. The reason I commented it out here though is because technically it's faster if it doesn't print them.

Code: [Select]
#!/usr/bin/perl -w
use strict;
use Net::FTP;
my($fname, @keys, $mode, @ips, $ftp, $check, $user, $pass);
print "Filename:\n> ";
chomp($fname = <STDIN>);
print "Keyword(s):\n> ";
chomp(@keys = split(' ', lc(<STDIN>)));
print "Select mode:\n1 - Contains all keywords.\n2 - Contains any keyword.\n> ";
chomp($mode = <STDIN>);
if($mode != 1 and $mode != 2){
   print "Invalid mode.\n";
   exit(1);
}

open IP, "<$fname" or die "Error: $!\n";
open SEARCH, ">search.txt" or die "Error: $!\n";
@ips = <IP>;
close IP;
foreach(@ips){
   chomp($_);
   $ftp = Net::FTP->new($_, Timeout => 5) or do{print "Error: $!\n"; next;};
   print "Username for $_\n> ";
   chomp($user = <STDIN>);
   print "Password for $_\n> ";
   chomp($user = <STDIN>);
   $ftp->login($user, $pass) or do{print "Error: $!\n"; next;};
   &navigate($_);
   $ftp->quit();
}
close SEARCH;

sub navigate{
   foreach($ftp->ls()){
      $ftp->cwd($_) or do{&test($ftp->pwd()."/".$_, $_[0]); next;};
      #print $ftp->pwd()."\n";
      &navigate($_[0]);
      $ftp->cdup();
   }
}
sub test{
   foreach(@keys){
      if($mode == 1){
         if(!(lc($_[0]) =~ /$_/)){
            $check = 0;
            last;
         }else{
            $check = 1;
         }
      }else{
         if(lc($_[0]) =~ /$_/){
            $check = 1;
            last;
         }else{
            $check = 0;
         }
      }
   }
   if($check){
      print SEARCH "ftp://".$_[1].$_[0]."\n";
   }
}