RE: Capturing the results from Expect with backtick

From: Tyson Sommer (tysons_at_firstcash.com)
Date: 02/17/05


To: <beginners@perl.org>
Date: Thu, 17 Feb 2005 10:42:34 -0600


 

> -----Original Message-----
> From: glidden, matthew [mailto:glidden_matthew@emc.com]
> Sent: Thursday, February 17, 2005 10:15 AM
> To: 'beginners@perl.org'
> Subject: Capturing the results from Expect with backtick
>
> I'm using combined perl and Expect scripts to do the following:
>
> 1. ping a host using expect (for technical reasons, I've had
> better luck using expect than perl here) 2. for each system
> that responds to ping, perform a series of rsh commands via perl
>
> The expect script is this, where $hosts is a list of
> potential names to
> ping:
>
> foreach h $hosts {
> spawn ping $h
>
> expect {
> icmp_seq {
> puts $h
> exit 0
> }
> }
> }
>
> puts ""
> exit -1
>
> Calling the expect script looks like this, where $sName is
> the system name:
>
> my $sPing = `./ping.exp $sName`;
>
> Later on, I check the results like this:
>
> if ($sPing ne '') {
> print "$sPing is up";
> [rsh commands]
> } else {
> print "$sName is down";
> }
>
> When I run this from a terminal window, it returns the pinged
> name and everything works fine. However, I need it automated,
> so I set up a cron job.
> The cron job never pings the hosts successfully--everything
> returns as "down." My hunch is that it has to do with using
> "puts" in Expect and backtick in perl--do they need a STDOUT
> stream to work properly? How can I set this up to ping
> properly while automated?
>
> Matthew

You shouldn't need Expect. I've had great success with something like this
(I'm sure there's a more condensed way to write it, but this works every
time):

...

        ### open ping command with filehandle so we can grab the STDERR (in
case of Unknown Host) using the 2>&1 and pipe
        ### Also using switches to make ping faster and limit to only four
pings -- tailor to suit your needs
        open (PINGTEST, 'ping -c 4 -i .2 -w 3 ' . $sName . ' 2>&1 |') || die
"Can't run PING!!!\n";
        
        ### set $ping_data to result of <PINGTEST> using join() to put into
scalar context
        my $ping_data = join ("", <PINGTEST>);
        
        ### print $ping_data if debugging (useful for checking results of
open() above)
        print "\n$ping_data\n" if $debug;
        
        
        ######### Might have to change one or more of the following regexes
########
        ######### depending on how your system returns ping data
########

        ### search $ping_data string...
        if ( $ping_data !~ /bytes\sfrom/ ) { ### <-- REGEX HERE

                if ( $ping_data =~ /unknown\shost/ ) { ### <-- REGEX HERE

                        ### ...and set $error if device unknown...
                        $error = "$sName unknown!!\n";

                } elsif ( $ping_data =~ /100% loss/ ) { ### <-- REGEX HERE

                        ### ...or set $error if device known but
unreachable...
                        $error = "$sName is down!!\n";

                }
        } else {

                ### ...or set $error to "successful" if we are
                $error = "$sName is up!\n";
        }

...

If anyone can make this simpler, let me know!



Relevant Pages

  • Capturing the results from Expect with backtick
    ... I'm using combined perl and Expect scripts to do the following: ... for each system that responds to ping, perform a series of rsh commands ... where $hosts is a list of potential names to ... Calling the expect script looks like this, where $sName is the system name: ...
    (perl.beginners)
  • 2005-10-23 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)
  • 2005-10-23 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)
  • 2005-10-23 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)
  • 2005-11-20 [de.comp.lang.perl.cgi] FAQ
    ... Programmiersprache Perl in CGI-Scripten. ... Wo gibt es Dokumentation über Perl und CGI? ... Hilfe zum Programm perldoc kann man mit 'perldoc perldoc' ... Mein Script läuft nicht bei meinem Provider. ...
    (de.comp.lang.perl.cgi)