tc1364 has asked for the wisdom of the Perl Monks concerning the following question:

Can you please assit me with capturing the output from the top command using Perl's version of Expect? The code runs on the box that needs to have the output from the top command captured.
#!/usr/bin/perl use Expect; $Expect::Log_Stdout=0; my $exp = Expect->spawn(); $exp->log_file("/ossd/tc1364/top.log"); $exp->send("/usr/local/bin/top\r"); $exp->expect(10,'CPU states') or exit(1); sleep(1); $exp->soft_close(); exit(0);

Replies are listed 'Best First'.
Re: Expect & top
by saintmike (Vicar) on Aug 08, 2005 at 18:34 UTC
    Capturing the output of top in graphical mode (think it uses curses) is probably not what you want. If you use top's batch mode instead, capturing is much easier, you don't even need Expect:
    open PIPE, "top -b -n 1 |" or die "Cannot open"; my $output = join '', <PIPE>; close PIPE or die "Running top failed";
      ty
        The output from this is much cleaner than using Expect but for example "CPU states: 43.5% idle, 54.2% user, 1.9% kernel, 0.5% iowait, 0.0% swap" line is missing. Could you please help me with obtaining this line?
        open PIPE, "top -b |" or die "Cannot open";
Re: Expect & top
by runrig (Abbot) on Aug 08, 2005 at 18:36 UTC
    You should be passing the command ("top") to spawn(), not send(). send() is used to give the spawned command input. use exp_internal() to debug the interaction between the command's output and the input you send to it.