Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

executing commands and getting output

by tja_ariani (Acolyte)
on Sep 05, 2003 at 06:46 UTC ( [id://289118]=perlquestion: print w/replies, xml ) Need Help??

tja_ariani has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,
Need help with your wise advice here.

I was trying to run a daemon in a server, and I need to execute commands to get information regarding what active nodes is running in the server(for a specific application, particularly Netsolve), in order to then kill or restart the nodes (must be done one by one).

I was trying to use exec and system command, but with this, I could not get the output from the application, since I need them for deciding the subsequent actions in the program and also to update clients connected to the server regarding what's going on in the server.(The connection to clients are using IO::Socket::INET)

Can you give me suggestion to do this? ( execute command, get response from the application, and based on this response,execute different kinds of commands)

A bunch of thanks to you all.

Replies are listed 'Best First'.
Re: executing commands and getting output
by zby (Vicar) on Sep 05, 2003 at 07:16 UTC
    You should investigate the open function (using a "|" in the file name). Or you could use the backtics: $output = `command`.
      Hi, I tried the $output = 'command' method but it doesn't seem to work. $output is empty and does not contain anything.
      With the open command, could you give me a clue of what does it actually do ?
      Thanks for your help
        $output = `command`; # ^ ^ # | |
        The quotes around command are backticks (ASCII 96, hex 60), not single quotes.

        As an alternative, you may use

        $output = qx(command);
Re: executing commands and getting output
by bm (Hermit) on Sep 05, 2003 at 09:10 UTC
    Use backticks. This only captures STDOUT, so you should add shell redirection to capture any errors that appear on STDERR:
    my $cmd = '/this/is/a/command -sdfw'; my $result = `$cmd 2>&1`; do_something() if ($result =~ /regex/);
    see  perldoc -q capture
    --
    bm
Re: executing commands and getting output
by Roger (Parson) on Sep 05, 2003 at 11:02 UTC
    Assume your command will return code on STDOUT or STDERR.

    Then you could simply do:
    my $result = `program`; # for collecting data from STDOUT
    and
    my $result = `program 2>&1`; # for collecting data from STDERR.
    There is no need to redirect the file handle. In the second case, the 2>&1 directive tells the shell to redirect all the output of program's STDERR (file handle 2) to STDOUT (file handle 1), so your perl program can collect the output of it.
Re: executing commands and getting output
by ctilmes (Vicar) on Sep 05, 2003 at 11:29 UTC
    As already mentioned, backtics (`) will do the basics. If you want to do fancy stuff with stdin/stdout/stderr, etc. take a look at IPC::Run which provides some great mechanisms for perl interactions with background processes.
Re: executing commands and getting output
by jmanning2k (Pilgrim) on Sep 05, 2003 at 13:53 UTC
    There's 3 options detailed for you in this post. Give them a try.
Re: executing commands and getting output
by gilbert0 (Monk) on Sep 05, 2003 at 16:24 UTC
    If you want to see the output of the text, your best option is the Pipe.
    Ironicly i just asked rfp for some advice about his 'Pesky Pipe' problem so i can give you the example script and output:

    [root@wicca /root]# cat ppipe.pl #!/usr/bin/perl open(FILE,"/bin/netstat|") || die("can't open"); while(<FILE>){ print $_; } close(FILE); [root@wicca /root]# perl ppipe.pl Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address St +ate tcp 0 0 10.9.205.4:netbios-ssn aeon-gig:1028 ESTAB +LISHED tcp 0 0 10.9.205.4:ssh aeon-gig:4931 ESTAB +LISHED Active UNIX domain sockets (w/o servers) Proto RefCnt Flags Type State I-Node Path unix 5 [ ] DGRAM 644 /dev/log unix 2 [ ] DGRAM 1512 unix 2 [ ] DGRAM 1504 unix 2 [ ] DGRAM 904 [root@wicca /root]#

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://289118]
Approved by Corion
Front-paged by castaway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-03-28 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found