perl - 执行外部命令时如何在 Perl 中捕获大量 STDOUT 输出

标签 perl shell sftp

我想获取远程位置中存在的文件名列表。

我在 Perl 脚本中使用以下代码片段。

my $command = "sftp -q -o${transferAuthMode}=yes -oPort=$sftpPort ${remoteUsername}\@${remoteHost} 2>\&1 <<EOF\n" . 
       "cd \"${remotePath}\"\n" . 
       "ls -l \n" . 
       "quit\n" . 
       "EOF\n";

my @files = `$command`;

当远程位置的文件数量很大 (>500) 时,@files 中不会捕获所有文件名。

当我手动执行 SFTP 并列出文件时,所有文件都会列出,但我没有通过脚本获得相同的文件。每次获得 @files 大小都不同。仅当文件数量较多时才会出现这种情况。

我无法找到这背后的原因。你能帮忙吗?

最佳答案

这可以在不需要任何额外的包模块的情况下实现。我在我的 CentOS 7 服务器(Windows VM)上对此进行了测试。

我的远程主机详细信息:我在远程主机目录中获得了约 2000 个文件。 CentOS 6.8 服务器。

%_gaurav@[remotehost]:/home/gaurav/files/test> ls -lrth|head -3;echo;ls -lrth|tail -2
total 7.9M
-rw-rw-r--. 1 gaurav gaurav 35 Feb 16 23:51 File-0.txt
-rw-rw-r--. 1 gaurav gaurav 35 Feb 16 23:51 File-1.txt

-rw-rw-r--. 1 gaurav gaurav 38 Feb 16 23:51 File-1998.txt
-rw-rw-r--. 1 gaurav gaurav 38 Feb 16 23:51 File-1999.txt
%_gaurav@[remotehost]: /home/gaurav/files/test>

来自 LocalHost 的脚本输出:请注意,我运行的命令没有 o${transferAuthMode}=yes 部分。如下所示,该脚本能够将所有结果收集到一个数组中,超过 500 个结果。

我正在打印总条目,数组中的一些特定索引号以显示结果,但请尝试使用未注释的 Dumper 行来查看完整结果。

%_STATION@gaurav * /root/ga/study/pl> ./scp.pl
Read 2003 lines from SCP command.

ArrayIndex: 2,3,1999,2000 contain:

[-rw-rw-r--    0 501      501           36B Feb 16 23:51 File-58.txt]
[-rw-rw-r--    0 501      501           37B Feb 16 23:51 File-129.txt]
[-rw-rw-r--    0 501      501           38B Feb 16 23:51 File-1759.txt]
[-rw-rw-r--    0 501      501           38B Feb 16 23:51 File-1810.txt]
%_STATION@gaurav * /root/ga/study/pl>

脚本及其工作原理:

#!/usr/bin/perl

use strict ;
use warnings ;
use Data::Dumper ;

my $sftp_port=22 ;
my ($user, $host) = ("gaurav","192.168.246.137") ;
my $remote_path = '/home/gaurav/files/test' ;

my @result ;    # To store result

my $command = "sftp -q -oPort=$sftp_port ${user}\@${host} 2>\&1 <<EOF\n"."cd $remote_path\nls -lrth\nquit\nEOF" ;

# open the command as a file handle, read output and store it.
open FH, "$command |" or die "Something went wrong!!\n" ;
while (<FH>) {
  tr/(?\r|\f|\n)//d ; # Removing any new line, carriage return or form feed.
  push(@result,"\[$_\]") ;
}
close FH ;

#print Dumper @result ;

# Just for printing a little bit of results from
# the array. Following lines can be deleted.
my $total = scalar @result ;
print "Read $total lines from SCP command.\n" ;
print "\nArrayIndex: 2,3,1999,2000 contain:\n
$result[2]
$result[3]
$result[1999]
$result[2000]
" ;

另一种方法:还可以通过创建 shell 脚本并从 perl 脚本调用它并读取其输出来解决此问题。如下所示,我的 shell 脚本被 perl 脚本调用并得到最终输出。当没有太多时间直接在 Perl 中编写/制定命令时,这可以用作一种快速技术。 您也可以在早期脚本中使用 qx 样式(如下所示)

Shell 脚本“scp.sh”

%_STATION@gaurav * /root/ga/study/pl> cat scp.sh
#!/bin/bash

sftp -oPort=${1} ${2}@${3} 2>&1 <<EOF
cd ${4}
ls -l
quit
EOF

Perl 脚本“2scp.pl”

%_STATION@gaurav * /root/ga/study/pl> cat 2scp.pl
#!/usr/bin/perl

use strict ;
use warnings ;
use Data::Dumper ;

my $sftp_port=22 ;
my ($user, $host) = ("gaurav","192.168.246.137") ;
my $remote_path = '/home/gaurav/files/test' ;

# Passing arguements to shell script using concatination.
my $command = './scp.sh '." $sftp_port $user $host $remote_path" ;

my @result = qx{$command} ;     # Runs the command and stores the result.
my $total = scalar @result ;
print "Read $total lines from SCP command.\n" ;
# End.

输出:

%_STATION@gaurav * /root/ga/study/pl> ./2scp.pl
Read 2004 lines from SCP command.
%_STATION@gaurav * /root/ga/study/pl>

尝试一下并告诉我们。 谢谢。

关于perl - 执行外部命令时如何在 Perl 中捕获大量 STDOUT 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271400/

相关文章:

c# - 在 C# 中使用 SFTP 传输大量文件很慢

perl - 使用 Net::SFTP::Foreign 通过 SFTP 上传文件

python - 合并/丢弃重叠的单词

java - 较短的代码是否会影响解释语言的性能?

linux - 在每行的开头添加带有文件名的字符串

linux - shell 脚本由 crontab 运行但没有输出

linux - 在 sftp 命令中列出文件并以毫秒显示其时间戳的命令

Perl-从输入字符串中获取第一个 "word"

c# - 如何将 WEP key 转换为 ASCII 密码?

linux - 每当我打开 gvim 时检查进程