troubleshooting Question

Script help

Avatar of ashsysad
ashsysadFlag for United States of America asked on
PerlShell ScriptingRegular Expressions
7 Comments1 Solution791 ViewsLast Modified:
Hello,

I have a text-pattern as shown below:
-----------------------------------------------------------------------------------------------------------------------------------
Pinging ora-stage-inf-d1.itc.com [172.113.52.123] with 32 bytes of data:
Reply from 172.113.52.123: bytes=32 time=213ms TTL=54

Pinging ora-stage-fmsoa-a4.itc.com [172.113.68.139] with 32 bytes of data:
Request timed out.

Pinging ora-stage-fmsoa-d1.itc.com [172.113.68.198] with 32 bytes of data:
Request timed out.
-----------------------------------------------------------------------------------------------------------------------------------------

I have developed a perl script that captures the Hostname and IP Address, and displays it in tabular form as shown below:


# cat ping_res.pl
#!/usr/bin/perl

@inputfile = `cat pingresult.txt`;
$status;

print "Hostname\t\t\t\tIP Address\t\tPing Status\n";
foreach(@inputfile)
{
        if($_ =~ m/Pinging\s(\S+)\s\[(\S+)\]\swith/) {
                $hstnm = $1;
                $ipaddr = $2;
                print "$hstnm\t\t$ipaddr\n";
        }
}



Hostname                                 IP Address                 Ping Status
ora-stage-inf-d1.itc.com                172.113.52.123
ora-stage-fmsoa-a4.itc.com           172.113.68.139
ora-stage-fmsoa-d1.itc.com           172.113.68.198

My requirement is, I want to capture the Ping Status from the text-pattern. i.e., if the next line (after Pinging host......................) has the string "Reply from", a variable called "$status" should be set with the string "Success".

The other case, if the next line(after Pinging host............) has the string "Request timed out", the $status variable should be set with the string "Timed out".

Please help me to complete this. Thanks.

The expected output is shown here:
Hostname                                 IP Address                 Ping Status
ora-stage-inf-d1.itc.com                172.113.52.123         Success
ora-stage-fmsoa-a4.itc.com           172.113.68.139         Timed out
ora-stage-fmsoa-d1.itc.com           172.113.68.198         Timed out
ASKER CERTIFIED SOLUTION
Lee Wadwell
Project Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros