如何使用 Perl 获取 DOS 工具的命令行输出? [英] How can I get the command-line output of a DOS tool using Perl?
本文介绍了如何使用 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在管道模式下:
open($filehandle, "$command|") or die "did not work: $! $?";
while(<$filehandle>)
{
#do something with $_
}
或使用反引号:
my @programoutput=`$command`
这篇关于如何使用 Perl 获取 DOS 工具的命令行输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文