Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using system() then awk

Status
Not open for further replies.

tazhicham

Programmer
Oct 23, 2003
15
0
0
FR
hi all,
i want to ping a host (called uc421) then get the returned error value in a file called ping_error. so i did it this way;

use Tk;
system("rsh *l grl pgrl /bin/awk '{
/sbin/ping -c 1 uc421
$! >> ping_error
}'");

but it doesn't work, and the followin' message is displayed:
syntax error unexpected '}'.

if anyone has a clue, thanks

 
Is there some reason you must use AWK? If not, there are several existing modules that will let you ping and capture the error.
 
Maybe this is some use:

# Modules

use Net::ping;

# Programming rules

# use strict;
use warnings;

# Main code

open (FILE, &quot;<ip.txt&quot;) or die &quot;can not open file&quot;; # open file with IP adresses
my @list = <FILE>; # declare and read the file in an array
close FILE; # close filehandle

my $p = Net::ping->new(&quot;icmp&quot;); # declare $p and use it for ICMP ping

my %ping_totals;
my %ping_received;
my %ping_loss;
foreach (@list) # run through the array
{
chomp($_); # remove newline character

if ($_ ne 0) # if $_ has an ip declared continue
{

if ($p->ping($_)) # if ping = succesful
{
$ping_totals{$_}++; # add a ping to the total of pings for that IP addres
$ping_received{$_}++; # add a ping to the total of received pings for that IP addres
print &quot;$_ is reachable\n&quot;;
}
else
{
$ping_totals{$_}++; # add a ping to the total of pings for that IP addres
$ping_loss{$_}++; # add a ping to the total of pings lost for that IP addres
print &quot;$_ not reachable\n&quot;;
}

}
}

&Pause(); # small pause in program

# printing the results

&Message(&quot;The ping statistics\n\n&quot;);
&Message(&quot;IP address\tSent\tReceived\tLost\tUptime\n\n&quot;);

foreach (sort keys %ping_totals) # for each pair in %ping_totals, sort the keys, run through the hash
{

print &quot;$_ \t $ping_totals{$_}&quot;; # Print ping totals the tabs are not inplace more IF CLAUSES!!

if (defined($ping_received{$_}))
{
print &quot;\t$ping_received{$_}&quot;; # Print pings received
}

if (defined($ping_loss{$_})) # Print pings received
{
print &quot;\t$ping_loss{$_}&quot;;
}

my $uptime = &Calc_Uptime ($_);
printf &quot;\t\t\t %d %% \n&quot;, $uptime; # Calculate uptime

}
# closing connections / handles

$p->close(); # close ICMP object


#################################################################################################
# Library of subroutines #
#################################################################################################

sub Pause {

# Takes no parameter, but asks the user to press
# enter en then it continues, returns always true
# unless error

print &quot;Press <enter> to continue to the results...\n&quot;;
my $continue = <STDIN>;
}

sub Message {

# Takes any string between quotes as the paramater and prints
# the string out onto the command line

my $message = shift @_;
print $message;
}

sub Countdown
{
# Takes a number of seconds (int) it should wait before continuing
# the program
my($timer) = @_;
my $timenow = time();
my $endtime = $timenow + $timer;

while (time() < $endtime)
{
$remaining = $endtime - time();
#print sprintf(&quot;$bs%-9s&quot;, GetDuration($remaining));
print &quot;$remaining seconds remaining \n&quot;;
sleep 1;
}
}

sub Check_Exist {

# Takes the $_ which holds the IP address as parameter
# and returns:
# 0 IF no pings were either received or lost (not going to happen unless error)
# 1 IF pings were received AND lost for this IP
# 2 IF pings were received BUT NOT lost for this IP
# 3 IF pings were lost BUT NOT received for this IP


my $ip = shift @_;
my $r_exist = $ping_received{$ip};
my $l_exist = $ping_loss{$ip};
my $exists;

if (defined($r_exist) && defined($l_exist))
{
$exists = 1;
}
else
{
if(defined($r_exist) && !defined($l_exist))
{
$exists=2;
}
else
{
if(defined($l_exist) && !defined($r_exist))
{
$exists=3;
}
else
{
$exists=0;
}
}
}
return $exists;
}


sub Calc_Uptime {

# Takes the $_ which holds the IP address as parameter
# calls the subroutine Check_Exist to find out if the
# variables have been set in the hashes and returns the
# calculated uptime

use Switch;

my $ip = shift @_;
my $calc;
my $received = $ping_received{$ip}; # Get THEM FROM OTHER FUNCTION?
my $loss = $ping_loss{$ip}; # Get THEM FROM OTHER FUNCTION?
my $casevar = &Check_Exist($ip); # Call sub with variable

switch ($casevar) {

case 0 { $calc = &quot;N/A&quot;; return $calc; }
case 1 { $calc = ($received / $loss); return $calc;}
case 2 { $calc = &quot;100&quot;; return $calc; }
case 3 { $calc = &quot;0&quot;; return $calc; }

else { print &quot;Error evaluating switch statement in Sub Calc_Uptime&quot; }
}

}

It is not the most beautiful piece of code but it pings, stores and displays results (May need some tweaking but you get the idea). I think you can use and modify this for your own use...

- Raenius







&quot;Free will...is an illusion&quot;
 
the main issue is that i have to make a remote shell to log myself as grl on pgrl station:
rsh -l grl pgrl and then ping a host
but i don t if there is a perl command working like rsh
thanx
 
there's a number of hits for 'rsh'. If you have the option, I suggest stepping up to the more secure ssh. It's used the same way, but it doesn't transmit anything in plain text, not even login info.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top