我的要求是在远程计算机上查找特定文件(filename.tup),并根据该文件从子目录(找到filename.tup文件的位置)复制其他文件。 这是我的试用剧本。
use File::Basename ;
@tuppaths=`ssh $host "/usr/bin/find /cygdrive/d/jobs/Results/$Target/$build/ -type f -name *.tup -print"`;
##above cmd will print the file name along with directory path e.g. /jobs/results/xxxxx/xxxxx/20150208_105308_j260_b150208/filename.tup
foreach my $tups (@tuppaths) {
my $copytups = $tups; ## this shows all the *.tup with fullpath
}
从这里开始,如何转到每个filename.tup所在的每个目录,并搜索.txt和.xml文件(在每个filename.tup的子目录中),并通过创建新目录将它们复制到本地计算机每个filename.tup。
用于复制计划使用rsync或scp的文件(我尝试手动复制scp并且它有效)。 请指导我如何使用perl脚本执行此操作。
答案 0 :(得分:0)
您应该使用类似http://perldoc.perl.org/File/Basename.html之类的内容解析路径 比你再做ssh,做'ls $ dirname / * .txt'之类的东西,并使用mkdir函数在另一个循环中解析元素来创建本地dirs和scp来获取文件。
答案 1 :(得分:0)
对不起晚回放我很抱歉,我出城了:) 这是我试过的代码,它工作正常。 @serenesat,我没有尝试使用:: scp,我觉得系统执行现在看起来还不错。
use File::Basename;
use Getopt::Long;
my $Target = undef;
my $build = undef;
my $host1 = undef;
my $USER = tester;
GetOptions (
't|target=s' => \$Target,
'b|build=s' => \$build,
'h|host1=s' => \$host1,
);
my $host = "$USER" . "@" . "$host1";
my $Test_dir = "Test_reports_" . "$build";
mkdir $Test_dir;
my $Results = "/cygdrive/C/Results/$Test_dir";
@tuppaths = `ssh $host "/usr/bin/find /cygdrive/d/jobs/Results/$Target/$build/ -type f -name *.tup -print"`;
print "TUPPATHS\n@tuppaths\n";
foreach my $dirpath(@tuppaths) {
my ($tup,$dir) = fileparse($dirpath);
my @tupname = split('.tup', $tup);
print "tupnames==>@tupname<===\n";
$testdirname = "$Results/$tupname[0]";
mkdir $testdirname;
print "creating directory for $tupname[0]\n";
#Get the concat text reports
#print "ssh $host /usr/bin/find $dir -type f -name *_concat.txt -print\n";
@txt_reports = `ssh $host " /usr/bin/find $dir -type f -name *_concat.txt -print"`;
foreach my $reportpath (@txt_reports) {
my ($concat,$dirs) = fileparse($reportpath);
print "copying $concat to $tupname[0] \n";
system ("scp","$host:$dirs$concat"," ","$testdirname"); # scp command to copy concat.txt files to related test directories
#Get the xml concat reports
@xml_reports = `ssh $host " /usr/bin/find $dir -type f -name *_concat.xml -print"`;
foreach my $xml_reportpath (@xml_reports) {
my ($xml,$dirs) = fileparse($xml_reportpath);
print "copying $concat to $tupname[0] \n";
system ("scp","$host:$dirs$xml"," ","$testdirname"); # scp command to copy concat.xml files to related test directories
#Get fail reports
@afrs = `ssh $host " /usr/bin/find $dir -type d -name atomic_fail_reports -print"`;
foreach my $pathtoafrs (@afrs) {
my ($afr, $afrdir) = fileparse ($pathtoafrs);
#print "afr =>$afr\n";
#print "afrdir =>$afrdir\n";
print "coyping $tupname[0] AFRs to $tupname[0] directory \n";
system ("scp","-r","$host:$afrdir$afr"," ","$testdirname")
}
}
}
}
print "Sucessfully Finished copying reports for $build\n";
答案 2 :(得分:-1)
使用CPAN
等Net::SCP
模块在服务器之间安全地复制文件。
use Net::SCP;
$scp = Net::SCP->new( {
"host"=>$hostname,
"user"=>$username
} );
$scp->scp($source, $destination);