On Aug 20, Dave Kettmann said:

>What I am wanting to do, is from a webpage, run a tail command on another
>machine to read x amount of lines from a log file and print it on a web
>page.

Ok.  Then what you do is you set up a listening socket on the server with
the log file:

  use IO::Socket;
  my $server = IO::Socket::INET->new(
    LocalAddr => 'this.machine.com',
    LocalPort => '9090',
    Reuse => 1,
    Listen => 10,
  ) or die "can't create $server: $!";

You make this listening socket accept a connection and print the results
of the command back to the client:

  while (my $client = $server->accept) {
    print $client `/usr/bin/tail -75 some_file`;
  }

Voila, you're done.  The thing is, that program needs to be running
constantly, because it has to listen for an incoming connection.

To connect to it, you would write this in your CGI program:

  use IO::Socket;

  my $log_socket = IO::Socket::INET->new(
    RemoteAddr => 'that.machine.com',
    RemotePort => '9090',
  ) or die "can't create socket: $!";

That connects you to the listening server.  Then you just print what's
waiting on the socket:

  print <$log_socket>;

And then you're done.  When the server's done sending you data, it closes
the connection, so your socket is closed.

>Another question is, what is wrong with IO::Socket?? I am still very new
>at perl and would appreciate input on the pros/cons of using Socket as
>opposed to using IO::Socket.

You misread me:

>>while (($new_sock, $c_addr) = $sock->accept())
>>{
>> my ($client_port, $c_ip) =sockaddr_in($c_addr);
>> my $client_ipnum = inet_ntoa($c_ip);
>> my $client_host =gethostbyaddr($c_ip, AF_INET);
>
>Eww.  You're using IO::Socket, so *use* it:

What I meant was that you're using the IO::Socket module, but instead of
using its easy and convenient object methods, you're using the klunky and
awkward Socket module's functions.

IO::Socket is easier to use, but does the same thing as Socket.  I always
use IO::Socket because I'm more comfortable and it's easier to understand
what I'm doing.

-- 
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to