Operating System - HP-UX
1834495 Members
3053 Online
110067 Solutions
New Discussion

Re: wrong result when executing syscall from a PERL program

 
SOLVED
Go to solution
Enrico Venturi
Super Advisor

wrong result when executing syscall from a PERL program

Hello colleagues.
I've written a very simple PERL program.
The PERL program executes a system call, namely "ping host_j -n 1" to check if the host_j is reachable;
even if the host_j is reachable the system call returns error -1 ... this happens either by system(ping) or by 'ping' ...
I check the result by testing $?

thanks
Enrico
6 REPLIES 6
Mark Grant
Honored Contributor
Solution

Re: wrong result when executing syscall from a PERL program

Not sure I understand you correctly but try this

`ping host_j -n i`;
if(($? >> 8) == 1){ # Exit status of command run by back ticks
print "Oh dear I punged with no result\";
}
Never preceed any demonstration with anything more predictive than "watch this"
H.Merijn Brand (procura
Honored Contributor

Re: wrong result when executing syscall from a PERL program

why not use Net::Ping ?

-1 might also be 255

# man perlvar

--8<---
$CHILD_ERROR
$? The status returned by the last pipe close, back�
tick (``) command, successful call to wait() or
waitpid(), or from the system() operator. This is
just the 16-bit status word returned by the wait()
system call (or else is made up to look like it).
Thus, the exit value of the subprocess is really
("$? >> 8"), and "$? & 127" gives which signal, if
any, the process died from, and "$? & 128" reports
whether there was a core dump. (Mnemonic: similar
to sh and ksh.)

Additionally, if the "h_errno" variable is sup�
ported in C, its value is returned via $? if any
"gethost*()" function fails.

If you have installed a signal handler for
"SIGCHLD", the value of $? will usually be wrong
outside that handler.

Inside an "END" subroutine $? contains the value
that is going to be given to "exit()". You can
modify $? in an "END" subroutine to change the
exit status of your program. For example:

END {
$? = 1 if $? == 255; # die would make it 255
}

Under VMS, the pragma "use vmsish 'status'" makes
$? reflect the actual VMS exit status, instead of
the default emulation of POSIX status; see "$?" in
perlvms for details.
-->8---

Attached is a small script that shows fine use of ping to scan the LAN for on-line machines in the segment of the host that's calling it

Enjoy, Have FUN! H.
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: wrong result when executing syscall from a PERL program

Nothing more complicated than:

my $rslt = system("ping host_j - n 1");
print "Result = ",$rslt,"\n";

should be required but this is a less-than-reliable method. Abount the only thing that you will know is whether or not the hostname can be resolved; this really tells you nothing about actually reaching the remote host because for almost all cases, ping will return 0.

I would use the Net::Ping module so that you can get much more meaning results.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: wrong result when executing syscall from a PERL program

Nothing more complicated than:

my $rslt = system("ping host_j - n 1");
print "Result = ",$rslt,"\n";

should be required but this is a less-than-reliable method. About the only thing that you will know is whether or not the hostname can be resolved; this really tells you nothing about actually reaching the remote host because for almost all cases, ping will return 0.

I would use the Net::Ping module so that you can get much more meaning results.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: wrong result when executing syscall from a PERL program

Here is a fully functional Perl script, ping.pl, that will do the job for you. If it returns 0, all is well. Invoke as ping.pl -u for full usage.

One thing to note using Net::Ping is that if you use the typicalICMP protocol (the same as the ping command) as a non-root user, the call will fail. Only super-user is allowed to use ICMP. For this reason, the ping program is a setuid program. Ordinary users can use the tcp or udp protocols.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: wrong result when executing syscall from a PERL program

Oops, I'm an idiot. Here's the script:

No points please.

If it ain't broke, I can fix that.