Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Text-based FTP client


ghost's Avatar
0 0

I just got done writing a text-based FTP client in Perl. For anyone who is interested, you can download the .zip here: HelFTP .zip

Here is the src:

#!/usr/bin/perl
use Net::FTP;

$clr='cls' if $^O eq 'MSWin32';
$clr='clear' if $^O ne 'MSWin32';

sub help{
	print 'CD dir - ';
	print "Changes the current working directory to the new specified dir.\n\n";

	print 'CLEAR - ';
	print "Clears the screen.\n\n";
	
	print 'CLS - ';
	print "Same as CLEAR.\n\n";

	print 'CONNECT  host user pass - ';
	print "Connects to the specified host and logs in as the specified user.\n\n";
	
	print 'DELETE  file - ';
	print "Deletes the specified file. To delete folders, use RMDIR instead.\n\n";
	
	print 'DISCONNECT  - ';
	print "Disconnects from the current host.\n\n";
	
	print 'DOWNLOAD file - ';
	print "Downloads the specified file into the same folder that HelFTP is running.\n\n";

	print 'CONNECT  host user pass - ';
	print "Connects to the specified host and logs in as the specified user.\n\n";
	
	print 'EXIT - ';
	print "Exits HelFTP.\n\n";
	
	print 'LIST - ';
	print "Lists all files and folders in the current directory.\n\n";
	
	print 'MKDIR  dir - ';
	print "Creates the specified dir.\n\n";
	
	print 'MODTIME file - ';
	print "Returns the date when the specified file was last modified.\n\n";
	
	print 'RENAME oldfile newname - ';
	print "Renames the old filename to a new filename.\n\n";
	
	print 'RMDIR dir - ';
	print "Deletes the specified dir and all files and folders under it.\n\n";
	
	print 'SIZEOF file - ';
	print "Returns the size of the specified file in bytes.\n\n";
	
	print 'UPLOAD file mode - ';
	print "Uploads the specified local file into the current remote folder. Mode must be either binary or ascii.\n";
}

system($clr);
print "Welcome to HelFTP. To begin working with your FTP server, enter commands below. For a list of commands and their descriptions, type help.\n";
print "\n>>";
chomp($command=<STDIN>);

while($command ne 'exit'){
	if($command=~m/^(connect)/i){
		@params=$command=~m/connect (\S*) (\S*) (\S*)/gi;
		($host,$user,$pass)=@params;
		$ftp=Net::FTP->new($host);
		if(!$ftp){
			warn "Could not connect to $host\n";
		}else{
			print "Successfully connected to $host\n";
		}
		if(!$ftp->login($user,$pass)){
			warn "Could not login as $user\@$host\n";
		}else{
			print "Successfully logged in as $user\@$host\n"; 
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command eq 'disconnect'){
		if(!$ftp->quit()){
			warn "Unable to disconnect\n";
		}else{
			print "Successfully disconnected from $host\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command eq 'clear' || $command eq 'cls'){
		system($clr);
		print "\n>>";
		chomp($command=<STDIN>);
	}elsif($command eq 'list'){
		@files=$ftp->ls();
		foreach $file(@files){
			if($file ne '.' && $file ne '..'){
				print "$file\n";
			}
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(cd)/i){
		@params=$command=~m/cd (.*)/gi;
		$dir=$params[0];
		if(!$ftp->cwd($dir)){
			warn "Could not change to $dir\n";
		}else{
			print "Successfully changed to $dir\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(rename)/i){
		@params=$command=~m/rename (\S*) (\S*)/gi;
		($old,$new)=@params;
		if(!$ftp->rename($old,$new)){
			warn "Could not rename $old to $new\n";
		}else{
			print "Successfully renamed $old to $new\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(delete)/i){
		@params=$command=~m/delete (\S*)/gi;
		$file=$params[0];
		if(!$ftp->delete($file)){
			warn "Could not delete $file\n";
		}else{
			print "Successfully deleted $file\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(rmdir)/i){
		@params=$command=~m/rmdir (\S*)/gi;
		$dir=$params[0];
		if(!$ftp->rmdir($dir,1)){
			warn "Could not delete $dir\n";
		}else{
			print "Successfully deleted $dir\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(mkdir)/i){
		@params=$command=~m/mkdir (\S*)/gi;
		$dir=$params[0];
		if(!$ftp->mkdir($dir)){
			warn "Could not create $dir\n";
		}else{
			print "Successfully created $dir\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(upload)/i){
		@params=$command=~m/upload (\S*) (ascii|binary)/gi;
		($file,$mode)=@params;
		$ftp->binary() if $mode eq 'binary';
		$ftp->ascii() if $mode eq 'ascii';
		if(!$ftp->put($file)){
			warn "Could not upload $file in $mode mode\n";
		}else{
			print "Successfully uploaded $file in $mode mode\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(download)/i){
		@params=$command=~m/download (\S*)/gi;
		$file=$params[0];
		if(!$ftp->get($file)){
			warn "Could not download $file\n";
		}else{
			print "Successfully downloaded $file\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(sizeof)/i){
		@params=$command=~m/sizeof (\S*)/gi;
		$file=$params[0];
		$size=$ftp->size($file);
		if(!$size){
			warn "Could not get size of $file\n";
		}else{
			print "Size of $file: $size bytes\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command=~m/^(modtime)/i){
		@params=$command=~m/modtime (\S*)/gi;
		$file=$params[0];
		$time=localtime($ftp->mdtm($file));
		if(!$time){
			warn "Could not get last modification time of $file\n";
		}else{
			print "$file was last modified on $time\n";
		}
		print ">>";
		chomp($command=<STDIN>);
	}elsif($command eq 'help'){
		help();
		print ">>";
		chomp($command=<STDIN>);
	}else{
		print "\n>>";
		chomp($command=<STDIN>);
	}
}