×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Parsing system calls in HP-UX

Parsing system calls in HP-UX

Parsing system calls in HP-UX

(OP)
Hello all,
      Here is my problem.  I am writing a perl script that pings a number of nodes through out my company's network for network monitoring.  What is happing is i call a ping command from the system then run it through a parse.  The problem is that it is not placing the parsed information into the $1 var.  When i do a cat of the information it shows all the output except for the ping time.  What i am parsing is the avg ping time.  I need it to be in the format of "$host,`date +%x`, 'avg ping time here'" so that it can inported to a SQL DB automaticly through an ftp script.  Everything works except for the parse of the avg ping time.  I am running this on a HP-UX 11.00 and i have perl 5.6.0 on the box.  Below I have supplied the code.  Please Help me. I have asked a couple of my perl friends who are more knowlegelbe then myself and they have no idea.  They say the code looks solid but they dont understand what is happening.
#/opt/perl5/bin/perl

use strict;
$| = 1; # flush buffers on end of print statements


my $data = 'testresult'; #output file
my $hosts = 'ip_address'; #ip address input file


my @hosts;


open(INFO,$hosts) || die "cant open $hosts ($!)\n";
@hosts = <INFO>;
close INFO;


chomp(@hosts); # get rid of the \ns at the end of each line


my $time = `date +%x`; #grab the time
chop ($time);  #Removing \n from date

open(INFO,">>$data") || die "Cant open output file $data ($!)\n";


my %status; #tmp storage for misc crap
print INFO "starting ping sweep : $time\n";


foreach my $host (@hosts){
    print "pinging $host...";


    open(PING, "ping $host -n 10|");
    while(<PING>){
        # parse the output, we only care bout packet loss and times
        if(/(\d+?)% packet loss/){
            # grab the packet loss
            $status{'loss'} = $1;

        }
        if(/^min\/avg\/max = (.+?) $/){
            $status{'time'} = {}; # anon hash, man perlref
            # dump data into the min, avg, and max hash indexs using a slice
            @{$status{'time'}}{'min','avg','max'} = split("\/",$1);
        }
    }
    close PING;



    print "done\n";
    print INFO "host $host\n";
    if($status{'loss'} != 100){
        print INFO $host . "," . $date . "," . $status{'time'} -> {'avg'} . "\n";
    } else {
        print INFO $host . "," . $date . "," . "TO" "\n";
    }
}


close INFO;

Thanks for the help.
Steve-0

RE: Parsing system calls in HP-UX

try

my ($min, $avg, $max);

($min, $avg, $max) = split("\/",$1);
$status{'time'}{'min'} = $min;
$status{'time'}{'avg'} = $avg;
$status{'time'}{'max'} = $max;

to be honest I'm not sure what your intention is with

@{$status{'time'}}{'min','avg','max'} = split("\/",$1);

it *looks* as if you're trying to assign to a hash of hashes, is that what you meant to do?


Mike
michael.j.lacey@ntlworld.com

RE: Parsing system calls in HP-UX

(OP)
That is what i did the problem i have it that the $1 var is not getting assigned a value from the (.+?) so there is nothing there to split.

Steve-0

RE: Parsing system calls in HP-UX

Sorry Steve, my post wasn't clear at all. I meant to say that you should try saving the value of $1 straightaway. Like this:

if(/^min\/avg\/max = (.+?) $/){
    ($min, $avg, $max) = split("\/",$1);

I'm not sure what the exact output of your version of ping is but you are checking for a line that begins with the string 'min/avg/max' -- is that what you intended?

And (probably a stupid question) wouldn't the regular expression (/^min\/avg\/max = (.*) $/ work better? I'm not sure what effect using + and ? next to each other has.


Mike
michael.j.lacey@ntlworld.com

RE: Parsing system calls in HP-UX

try this

if(/^min\/avg\/max\s+=\s+/){
  ($min, $avg, $max) = split("\/",$');

this will match 'min/avg/max = ' and what was *after* that match will get put into $'

i think you'll find that the output hpux ping is ascii, it will be something incorrect in the script. (from an offline discussion in case anyone's wondering)


Mike
michael.j.lacey@ntlworld.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close