How can I get the command-line output of a DOS tool using Perl?

前端 未结 4 1327
挽巷
挽巷 2021-01-06 17:36

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:

         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-06 17:45

    You can't get the output with system().

    Instead use bactkicks:

    my $throughput = 0;
    my $output = `ftp -s:c:\\ftp_dl.txt`;
    if (($? == 0) && ($output =~ /([\d+\.]+)\s*K?bytes\/sec/m)) {
        $throughput = $1;
    }
    

    $output will contain all the lines from the execution of the ftp command (but not any error message sent to STDERR).
    Then we check if ftp returned success (0) and if we got a throughput somewhere in the output.
    If so, we set $throughput to it.

    This being Perl, there are many ways to do this:

    You could also use the Net::FTP module that supports Windows to deal with the file transfer and use a timing module like Time::HiRes to time it and calculate your throughput.

    This way you won't depend on the ftp program (your script would not work on localised version of Windows for instance without much re-work, and you need to rely on the ftp program to be installed and in the same location).

提交回复
热议问题