如何使用Perl获得DOS工具的命令行输出? [英] How can I get the command-line output of a DOS tool using Perl?

查看:203
本文介绍了如何使用Perl获得DOS工具的命令行输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Perl脚本中使用Windows内置FTP工具来测量链接的吞吐量。因此,脚本会创建以下命令脚本:

I want to meassure the throughput of a link using Windows build-in FTP tool inside a Perl script. Therefore the script creates the following command script:

open <ip>
<username>
<password>
hash
get 500k.txt
quit

之后我运行使用以下Perl代码的命令脚本:

Afterwards I run the command script using the following Perl code:

system(@args);
@args = ("ftp", "-s:c:\\ftp_dl.txt");
system(@args);

如果我在DOS框中运行命令,输出如下所示:

If I run the command inside a DOS-box the output looks like this:

ftp> open <ip>
Connected to <ip>
220 "Welcome to the fast and fabulous DUFTP005 ftp-server :-) "
User (<ip>:(none)):
331 Please specify the password.

230 Login successful.
ftp> hash
Hash mark printing On  ftp: (2048 bytes/hash mark) .
ftp> get 500k.txt
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for 500k.txt (14336 bytes).
#######
226 File send OK.
ftp: 14336 bytes received in 0.00Seconds 14336000.00Kbytes/sec.
ftp> quit
221 Goodbye.

为了能够获得吞吐量,我需要提取该行:

To be able to get the throughput I need the extract that line:

 ftp: 14336 bytes received in 0.00Seconds 14336000.00Kbytes/sec.

我不太熟悉Perl。有人知道如何获得该行吗?

I'm not very familiar with Perl. Does anybody have an idea how to get that line?

推荐答案

使用打开管道模式:

open($filehandle, "$command|") or die "did not work: $! $?";
while(<$filehandle>)
{
#do something with $_
}

或使用反引号:

or use backticks:

my  @programoutput=`$command`

这篇关于如何使用Perl获得DOS工具的命令行输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆