perl小指令碼——ftp上傳檔案、讀取資料庫表中資料

呆呆笨笨的魚發表於2014-05-26
/* ftp上傳檔案 */
#!/usr/bin/perl-w

use strict;
use warnings;

#使用Net::FTP模組
use Net::FTP;

our $ftp_host = "10.55.15.232";
our $ftp_user = "username";
our $ftp_pw = "password";

&main();
exit(0);

sub main
{
&login_ftp();
}

sub login_ftp
{
my $ftp;
$ftp = Net::FTP->new($ftp_host,Passive=>1,Debug=>1,Timeout=>30) or die "Can not connect to ftp server $ftp_host : $@" , $ftp->message;
$ftp->login($ftp_user,$ftp_pw) or die "Can not login ", $ftp->message;;
$ftp->cwd("/test") or die "Can not change working directory !\n" , $ftp->message;
#$ftp->get("aaa.txt") or die "get failed ", $ftp->message;

$ftp->put("config.txt");

$ftp->quit;
}

=======================================================================
/* 讀取資料庫表中資料 */
#!/usr/bin/perl-w

use strict;
use warnings;
use DBI;

our $tnsname="criss";
our $username="zhangbin";
our $password="zhangbin";

our ($id ,$name);

print "This is a test for database connection !\n";

&main();
exit(0);

sub main
{
my $res=db_connect();
print "res = $res\n";
}

sub db_connect
{
my $sql=qq{select id, name from test1};

####資料庫連線
my $dbh=DBI->connect("dbi:Oracle:$tnsname", $username, $password) or die "Can not connect db: $DBI::errstr\n";

my $sth=$dbh->prepare($sql) or die "prepare $sql failed : $DBI::errstr\n";

$sth->execute or die "execute $sql failed : $DBI::errstr\n";

$sth->bind_columns(undef,\$id,\$name);

#while(@arr = $sth->fetchrow_array())
#{
#print join ("\t",@arr),"\n";

#}
while($sth->fetch)
{
print "$id,$name\n"
}

$sth->finish;
####斷開資料庫連線
$dbh->disconnect() or warn "DB disconnect failed: $DBI::errstr\n";
return 100;
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28929558/viewspace-1170202/,如需轉載,請註明出處,否則將追究法律責任。

相關文章