#!/usr/bin/perl # # Matthias Gaertner, 02 May 2004 (v1.0) # # This simple script provides command-line FTP delete. # Somehow there does not seem to exist a free utility for this yet ?!? # # This code is freeware. Provided AS IS without any warranty of any kind. # # Usage: # ftpdelete [-i] ftp://[user[:password]@]host/path/file # # Defaults: # user: anonymous # password: nobody@no.such.host.com # # Flags: # The -i flag indicates that the actual result of the delete operation # should be ignored. # # Return values: # 0 - OK # 1 - URL parsing error # 2 - Can't connect # 3 - Can't login # 4 - Can't delete # use Net::FTP; sub usage() { $! = 1; die "Usage: ftpdelete [-i] ftp://[user[:password]@]host/path/file (v1.0)\n"; } $ignore = 0; if( $ARGV[0] =~ "-i" ) { $ignore = 1; shift; } # Read URL from command line $url = $ARGV[0]; ($proto, $host, $path ) = $url =~ m|(.+?)://(.+?)(/.+)|; &usage() unless $proto eq "ftp"; if( $host =~ m|(.*)@(.*)| ) { $user = $1; $host = $2; if( $user =~ m|(.*?):(.*)| ) { $user = $1; $pass = $2; } } $user = "anonymous" unless $user; $pass = "nobody@no.such.host.com" unless $pass; #print "Protocol: $proto\n"; #print "User: $user\n"; #print "Pass: $pass\n"; #print "Host: $host\n"; #print "Path: $path\n"; my $ftp = Net::FTP->new($host); unless (defined ($ftp)) { $! = 2; die "Can't connect $host: $@\n"; } if (!$ftp->login($user,$pass)) { $! = 3; die "Can't login to $host\n"; } if (!$ftp->delete($path)) { if( !$ignore ) { $! = 4; die "Can't delete $path on $host\n"; } } $ftp->quit(); exit(0); # End