why is return always 0?

J

jammer

How do I make tftp fail if the host is bad?

my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1 >/
dev/null" );
if ( $rc != 0 ) {
print "unable to tftp to '$host'\n";
exit 1;
} else {
print "success: $rc\n";
}
 
S

sandy_saydakov

How do I make tftp fail if the host is bad?

my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1 >/dev/null" );
if ( $rc != 0 ) {
print "unable to tftp to '$host'\n";
exit 1;
} else {
print "success: $rc\n";
}

Make sure the binary returns non-zero on failure. Try that command on
the command line and do "echo $?" right after.

-sandy
http://myperlquiz.com/
 
J

jammer

Make sure the binary returns non-zero on failure. Try that command on
the command line and do "echo $?" right after.

-sandyhttp://myperlquiz.com/

It prints an error when run interactively.
tftp does run, it just can't find the host.
It returns 0 because it ran, didn't work but it ran. :-(
 
S

sandy_saydakov

It prints an error when run interactively.
tftp does run, it just can't find the host.
It returns 0 because it ran, didn't work but it ran. :-(

So there must be a bug in tftp then. It makes no sense to have a clean
exit code on failure.

For example, try it with 'ls':
ls nonexistent
ls: nonexistent: No such file or directory
1
This makes perfect sense, doesn't it?

I would suggest trying a native TFTP Perl module:
http://search.cpan.org/~gsm/TFTP-1.0b3/TFTP.pm

/sandy
http://myperlquiz.com/
 
X

xhoster

It seems that that is a question for the authors of tftp, not
a Perl question. But there may be a Perl work-a-round.
It prints an error when run interactively.

Does it do that when run noninteractively? You could try capturing and
parsing its stderr. IPC::Run might be a good way. (I think it might also
help you trick tftp into thinking it is running interactively, if that is
necessary.)
tftp does run, it just can't find the host.
It returns 0 because it ran, didn't work but it ran. :-(

Well, that was probably a poor design decision. By that criterion, nothing
should ever exit with anything but 0. If it ran, it ran. If it didn't
run, it can't exit with anything at all.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
C

comp.llang.perl.moderated

How do I make tftp fail if the host is bad?

my $rc = system( "/usr/bin/tftp bad$host < $tempFile 2>&1 >/
dev/null" );
if ( $rc != 0 ) {
print "unable to tftp to '$host'\n";
exit 1;
} else {
print "success: $rc\n";
}

On Solaris anyway, tftp doesn't return an exit code - just writes to
stdout and then prompts.
So you'd have to short-circuit its prompt and
parse the output for specific errors, eg.:

$out = qx { /usr/bin/tftp badhost </dev/null };
if ( $? ) {
die "can't start tftp: $?";
} elsif ($out =~ /unknown host/ ) {
die "failed: $out";
} else {
# ok
...
}

hth,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top