#!/usr/bin/perl
# Pravin Paratey (September 28, 2002)
# proggy for testing anonymous logins


use Socket;
use strict;

my $BUF_SIZE = 1000;
my $ipaddress = $ARGV[0];
my $sa = sockaddr_in(21, inet_aton($ipaddress));
my $InBuf = "";
my $username = "anonymous";
my $password = "pravin\@cse.iitb.ac.in";
my $sock;

	# Create socket
        socket ($sock, AF_INET, SOCK_STREAM, getprotobyname('tcp'));

        # Connect to the remote host
        connect ($sock, $sa) ||  die 'Connection timed out...';

        # Receive Welcome information
        recv ($sock, $InBuf, $BUF_SIZE,0);

        send ($sock, "USER $username\r\n",0);
        recv ($sock, $InBuf, $BUF_SIZE,0);

	# 331 is the return value if the ftp server allows anonymous access
        if ( $InBuf =~ /^331/){
                #success, send in the password string
                send ($sock, "PASS $password\r\n",0);
                recv ($sock, $InBuf, $BUF_SIZE,0);
		
		# 230 is the value returned on successful login
                if ($InBuf =~ /^230/){
                        print STDOUT "[Succes] $ipaddress\n";
                        send ($sock, "QUIT\r\n",0);
                        recv ($sock, $InBuf, $BUF_SIZE,0);
                }
                else{
                        print STDOUT "[Failed] $ipaddress\t$InBuf\n";
                }
        }
        else{
        #FAILURE
                print STDOUT "[Failed] $ipaddress\t$InBuf\n";
        }
