我们知道,要往FTP上面上传或下载文件,通常的做法是这样的:
1. 在命令行终端上,输入类似:
test@test ~: lftp ftp://public@xxxx
Passwd: xxx
登录到FTP服务器上,输入帐号和密码
2. 执行类似“put/get"指令来下载文件或是上传文件
自己对这种做法进行了改良,改良后的使用方法如下:
1. 比如想要上传某文件(如test1)到FTP服务器上,可以执行下面命令:
test@test ~: ./upload.pl public@xxxx:test1
Passwd: xxx
2. 想要下载某文件,可以使用下面命令:
test@test ~: ./download.pl public@xxx:test1
Passwd: xx
具体脚本如下: 注意(这里FTP服务器端口默认为22, 如果不是22还需要设置下)
upload.pl:
- #!/usr/bin/perl
-
-
#This script is used to upload file from ftp server automatically
-
-
use strict;
-
use Net::FTP;
-
-
-
die "Usage:\n\t$0 [user]\@server:file\n" if (! defined @ARGV);
-
-
-
my ($user,$passwd, $server, $file);
-
my $debug = 0;
-
my $argv = $ARGV[0];
-
-
-
if ($argv =~ /\@(\d+.\d+.\d+.\d+):(.*)/){
-
$server = $1;
-
$file = $2;
-
}
-
-
print "FTP Server is :$server\n";
-
print "Put filename is :$file\n";
-
-
-
if ($argv =~ /(.*)\@/){
-
$user = $1;
-
print "Login Name :$user\n";
-
-
system "stty -echo";
-
print "Input password:";
-
-
chomp($passwd = <STDIN>);
-
system "stty echo";
-
-
# print $passwd;
-
}
-
-
print "\n";
-
-
###Connect the ftp server
-
print "Connect the ftp server....\n";
-
-
my $ftp = Net::FTP->new("$server", Debug => 0)
-
or die "Cannot connect :$@";
-
-
-
$ftp->login("$user", "$passwd")
-
or die "Cannot login: $ftp->message";
-
-
-
#$ftp->binary()
-
# or die "Cannot set binary mode: $ftp->message";
-
-
$ftp->put("$file")
-
or die "Put file faild: $ftp->message";
-
-
print "File has been uploaded successfully...\n";
-
-
$ftp->quit;
download.pl
- #!/usr/bin/perl
-
-
#This script is used to download file from ftp server automatically
-
-
use strict;
-
use Net::FTP;
-
-
-
die "Usage:\n\t$0 [user]\@server:file\n" if (! defined @ARGV);
-
-
-
my ($user,$passwd, $server, $file);
-
my $debug = 0;
-
my $argv = $ARGV[0];
-
-
-
if ($argv =~ /\@(\d+.\d+.\d+.\d+):(.*):(.*)/){
-
$server = $1 . ":" . $2;
-
$file = $3;
-
}
-
-
print "FTP Server is :$server\n";
-
print "Get filename is :$file\n";
-
-
-
if ($argv =~ /(.*)\@/){
-
$user = $1;
-
print "Login Name:$user";
-
-
system "stty -echo";
-
print "Input password:";
-
-
chomp($passwd = <STDIN>);
-
system "stty echo";
-
-
# print $passwd;
-
}
-
-
-
###Connect the ftp server
-
print "Connect the ftp server....\n";
-
-
my $ftp = Net::FTP->new("$server", Debug => 0)
-
or die "Cannot connect :$@";
-
-
$ftp->login("$user", "$passwd")
-
or die "Cannot login: $ftp->message";
-
-
$ftp->get("$file")
-
or die "Put file faild: $ftp->message";
-
-
print "File has been downloaded successfully...\n";
-
-
$ftp->quit;
脚本可能还比较的粗糙,欢迎大家提点建议...
阅读(3182) | 评论(0) | 转发(0) |