Hi,

In my perl script p1 i am calling another script p2 which logs into a
remote machine and executes a script p3. The $file defined in p3 does
not exist. So copy operation in p3 will error out with error code 256
and p3 stops execution with exit staus 256. But p2 is not able to
recieve this value. I need the value 256 in p2. Could any one help me
to sove this..


p1
===

........ 
........ 
system(p2 $opt); 
if ($? == 0) { print "success"; } 
elsif ($? == 1) { print "failure"; } 
else { print "Undefined exit status"; } 
........ 
........

p2
===

use Net::Telnet; 

$t = new Net::Telnet(); 
$t->open("machine"); 
$t->login("user","paswd"); 
$t->cmd("p3 $flag"); 
$t->close(); 

print "Check : $?";     
# above line prints $? value as 0; 

if ($? == 0) { exit(0); } 
elsif ($? == 1) { exit(1); } 
else { exit($?); }

p3
===

my $file  = "/hom/user/file"; 
..... 
...... 
system(cp -rf $file $backup); 
unless($? ==0) 
{ 
   print "Failed to create the back up with exit_status $?"; 
   # above line prints $? value as 256. 
   exit($?); 
} 
..... 
....

Recommended Answers

All 4 Replies

From the man page of Net Telnet:

In a scalar context, the characters read from the remote side are discarded and 1 is returned on success. On time-out, eof, or other failures, the error mode action is performed. See errmode().

In a list context, just the output generated by the command is returned, one line per element. In other words, all the characters in between the echoed back command string and the prompt are returned. If the command happens to return no output, a list containing one element, the empty string is returned. This is so the list will indicate true in a boolean context. On time-out, eof, or other failures, the error mode action is performed. See errmode().

The characters that matched the prompt may be retrieved using last_prompt().

Many command interpreters echo back the command sent. In most situations, this method removes the first line returned from the remote side (i.e. the echoed back command). See cmd_remove_mode() for more control over this feature.

Use dump_log() to debug when this method keeps timing-out and you don't think it should.

Consider using a combination of print() and waitfor() as an alternative to this method when it doesn't do what you want, e.g. the command you send prompts for input.

The Output named parameter provides an alternative method of receiving command output. If you pass a scalar reference, all the output (even if it contains multiple lines) is returned in the referenced scalar. If you pass an array or hash reference, the lines of output are returned in the referenced array or hash. You can use input_record_separator() to change the notion of what separates a line.

Hi mitchems,

I am very new to perl. So could u pls help me to solve this.

I think what you need to do is this:

my @list=$t->cmd("p3 $flag");

and have p3 print out the stauts. Then, you will want to do this:

my $status=pop(@list);

I can't test the code because there's nowhere for me to telnet. Why not just run p3 via cron? Then send an email or write a log file or both witht he status of the backup. The whole architecture of what you're doing seems a bit arcane to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.