[Perl] Capture system call error message.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Perl] Capture system call error message.
# 1  
Old 08-18-2009
[Perl] Capture system call error message.

Hi,

I googled a bit, but could not find the answer to my problem.
But I am sure it is a common issue.

I have this code:

Code:
#!/bin/perl -w #-d

use strict;

sub remsh_test()
{
my $host = $_[0];

printf "\n----\n\n";

printf "remsh to $host with system call\n";
my $result = system ("remsh $host hostname");
printf "Contents of \$result :\n";
printf "$result\n";

printf "\n----\n\n";

printf "remsh to $host with backticks\n";
$result = `remsh $host hostname`;
printf "Contents of \$result :\n";
printf "$result\n";

printf "\n----\n\n";

printf "remsh to $host with open\n";
open (RESULT, '-|', "remsh $host hostname");
while (<RESULT>) {
        chomp;
        printf "Contents of \$_ :\n";
        printf "$_\n";
        $result = $_;
        printf "Contents of \$result :\n";
        printf "$result\n";
}
close (RESULT);

printf "\n----\n\n";

} # end sub

# === MAIN ===

foreach my $host ("nbsol151", "nbfuz159") {
        &remsh_test($host);
}

The output is following:

Code:
----

remsh to nbsol151 with system call
nbsol151
Contents of $result :
0

----

remsh to nbsol151 with backticks
Contents of $result :
nbsol151


----

remsh to nbsol151 with open
Contents of $_ :
nbsol151
Contents of $result :
nbsol151

----


----

remsh to nbfuz159 with system call
rcmd: nbfuz159: Unknown host
Contents of $result :
256

----

remsh to nbfuz159 with backticks
rcmd: nbfuz159: Unknown host
Contents of $result :


----

remsh to nbfuz159 with open
rcmd: nbfuz159: Unknown host

So the observation is that, when remsh works on the other host, you are able to capture its output and use it in your program.
But when remsh fails, you cannot capture the error and use it in your program.

I am looking for a way to capture this "rcmd: nbfuz159: Unknown host" and to use it in my program.
And I do not want the see "rcmd: nbfuz159: Unknown host" on my screen.

Any thoughts ?

Thanks in advance,

E.J.
# 2  
Old 08-18-2009
Did you try to redirect stderr into stdout inside the system "script"?
Code:
$somevar = system( "[some command ] 2&>1");

# 3  
Old 08-18-2009
Jim,

Thanks for replying.

I tried, but then for the 'good' system I do not get my command output any more.
And for the 'bad' system I still get the error message on my screen (and it is not assigned to my $result variable).

Code:
----

remsh to nbsol151 with system call
Contents of $result :
0

----

remsh to nbsol151 with backticks
Contents of $result :


----

remsh to nbsol151 with open

----


----

remsh to nbfuz159 with system call
rcmd: nbfuz159: Unknown host
Contents of $result :
0

----

remsh to nbfuz159 with backticks
rcmd: nbfuz159: Unknown host
Contents of $result :


----

remsh to nbfuz159 with open
rcmd: nbfuz159: Unknown host

----



---------- Post updated at 01:46 PM ---------- Previous update was at 01:39 PM ----------

My hostname on the other host (nbsol151) is changed to '2' now.
So it looks like I need to escape the 2$>1 somehow.
Now it did 'hostname 2' and '$>1'.

---------- Post updated at 02:31 PM ---------- Previous update was at 01:46 PM ----------

I think this will do (using /dev/null):

Code:
printf "remsh to $host with backticks\n";
$result = `remsh $host hostname 2> /dev/null`;
chomp $result;
printf "Contents of \$result :\n";
printf "$result\n";
if ($result eq $host) {
        printf "remsh successful.\n";
} else {
        printf "remsh NOT successful.\n";
}

That produces:

Code:
----

remsh to nbsol151 with backticks
Contents of $result :
nbsol151
remsh successful.

----

remsh to nbfuz159 with backticks
Contents of $result :

remsh NOT successful.

Although then I do not know the reason for it not to be successful.
# 4  
Old 08-19-2009
Quote:
Originally Posted by jim mcnamara
Did you try to redirect stderr into stdout inside the system "script"?
Code:
$somevar = system( "[some command ] 2&>1");

The "2&>1" is a little typo.
It has to be "2>&1".
That explains why my hostname was changed to '2' on the other host.

This works (with backticks):

Code:
my $host = "nbsol152";
my $redirect = "2>&1";
my $return;
$return = `remsh $host hostname $redirect`;
printf "return : $return\n";

That will capture all output, including the error output.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

C: error in wait system call

i made this program but when i compile this code, compiler make this error, is an error on wait() system call but argumenti is right, or not?: esercizio.c:80:9: error: incompatible type for argument 1 of ‘wait' /usr/include/i386-linux-gnu/sys/wait.h:116:16: note: expected ‘__WAIT_STATUS' but... (4 Replies)
Discussion started by: tafazzi87
4 Replies

2. Shell Programming and Scripting

Capture all error message in Log file and send the Log file by email

Hi I have a requirement to write a script to capture all errors in a Logfile and send the file in email. If there is any error occurred the subject of email will be ERROR , If there are no error occurred the subject of email will be SUCCESS. So I created a Log file and put the Appropriate... (2 Replies)
Discussion started by: dgmm
2 Replies

3. Shell Programming and Scripting

How to call the System command twice in the same perl script...

Hello experts, I have a perl script which looks for the ARGV and then loads the data as per it. Example. #Checking the server to connect if ($ARGV eq 'QA') { $ENV{"ORACLE_HOME"} = "/oracle/product/11.2.0"; $ENV{"PATH"} = "$ENV{'PATH'}:/oracle/product/11.2.0/bin"; ... (1 Reply)
Discussion started by: msrahman
1 Replies

4. Shell Programming and Scripting

PERL SSH call error

I have a Perl Script #!/usr/local/bin/perl $server_name = "info-xyz-05"; my $value = `/usr/bin/ssh -f $server_name /users/salesadm/MGFINFA/bridgerftpup.sh`; The above calls a shell script on another server called bridgerftpup.sh. This script has several commands and needs to call... (2 Replies)
Discussion started by: alfredo123
2 Replies

5. Shell Programming and Scripting

How to capture actual error message when a command fails to execute

I want to capture actual error message in case the commands I use in my shell script fails. For eg: ls -l abc.txt 2>>errorlog.txt In this case I understand the error message is written to the errorlog.txt and I assume its bacause the return code from the command ls -l abc might return 2 if... (3 Replies)
Discussion started by: prathima
3 Replies

6. Shell Programming and Scripting

Lay person needs perl help solving error message

Hi, My name is Tex I am past 60 and in need of perl help. My hobby is genealogy and I am using a perl program to display my data on my web pages. I don't even know enough to know how to ask in the right way for the help I need. This program is written in perl it is open and has been updated... (2 Replies)
Discussion started by: tex
2 Replies

7. Shell Programming and Scripting

How to capture status code and echo a message

Im trying to execute application and its return code is below IF Status code=o echo "........" else Staus Code =-2 DJRE then echo "......" Can any one help me how to handle the status code and echo some message. (12 Replies)
Discussion started by: laknar
12 Replies

8. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

9. Shell Programming and Scripting

Sh Prgram to capture Log message.

Hi All, I have a log file which consists of log messages as follows -> GLOBALCALLID_CLUSTERID_B NEXT * , O(") CHARACTER JOINONBEHALFOF NEXT * , O(") CHARACTER Record 1: Rejected - Error on table IFA_MMV_CDR, column CDRRECORDTYPE.... (1 Reply)
Discussion started by: rahulrathod
1 Replies
Login or Register to Ask a Question