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

Please let me know the answer of this question. My computer is connected to other four computers with LAN connections. I want to write a perl programs so that i can know the list of processes running on the other four computers. i think we need to use unix commands in the perl programs or some other ways.

Replies are listed 'Best First'.
Re: Networking Perl
by Your Mother (Archbishop) on Oct 29, 2010 at 20:57 UTC

    This is a possible way. Just put this script on each box and run it with starman or plackup and you have an instant, on demand JSON report of the box. I added pid filtering as a *minimal* API proof of concept. I also did pretty printing on the JSON for the benefit of playing around which I wouldn't do in production.

    cow@moo[1316]~/bin>plackup proc.psgi HTTP::Server::PSGI: Accepting connections at http://0:5000/ ...

    Then you can ask for the full process data at http://localhost:5000/ and filter by pid with http://localhost:5000/123,9876,4433 and such like. As given, the code is *too* free and easy with the arg parsing.

    You might not want to publish the stuff so openly. You could *easily* add a "security" layer with Plack::Middleware::Auth::Basic. And I'd recommend a whitelist/filter of the processes you'd like to show.

    use warnings; use strict; use JSON; use Plack::Request; use Proc::ProcessTable; sub { my $req = Plack::Request->new(+shift); my %pids = map { $_ => 1 } $req->path =~ /(\d+)/g; my $procs = Proc::ProcessTable->new; my $json = JSON->new; my %status; for my $proc ( @{ $procs->table } ) { next if scalar(%pids) and not $pids{$proc->pid}; for my $field ( $procs->fields ) { $status{$proc->pid}{$field} = $proc->$field; } } return [ 200, [ "Content-Type" => "application/json" ], [ $json->pretty->encode(\%status) ] ]; };
Re: Networking Perl
by Dru (Hermit) on Oct 29, 2010 at 19:23 UTC
    You probably want to check out Net-SSH to connect securely to the other computers and run the commands.

    Thanks,
    Dru

    Perl, the Leatherman of Programming languages. - qazwart
Re: Networking Perl
by jethro (Monsignor) on Oct 29, 2010 at 18:42 UTC

    Take a look at the 'ps' unix command, for example 'ps -elf' will list all processes.

    A perl solution seems to be Proc::ProcessTable

Re: Networking Perl
by Marshall (Canon) on Oct 30, 2010 at 14:29 UTC
    I don't know what computers that you have or what Operating Systems that they are running.

    The first step in something like this is: "I know how to do it manually by typing in stuff or clicking on stuff".

    The second step is: "ok, how can I automate what I already know how to do?" - that's when you write the Perl program.

    So are you able to get to Step 1?