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

I have following requirement.
1)There are three entities in the program. node A (Linux box),B(Windows Server) and C(Any node). Node C is unreachable from Node A directly. Node B is a Windows server, where a perl script has to manage the transfer of ping request from a perl script in node A node C and then return the message (pass/fail) to node A.
2) I am ready with a perl Server script in Windows server, which accepts ping request from client script and pings node C successfully. Using IO::Socket module.
Requirement:
there can be N number of client like node A which will be pinging node C through script running in Node B. Script in Node B should accept the IP address from Client A, Ping the node specified and send the result to that client. For that I can't figure out how in Node B can I determine the port and Ip address of node running client A kind of application. So that I can send the result to client A and client A displays the result and dies
MyServer.pl
#!/usr/bin/perl use IO::Socket; use Net::Ping; $sock = new IO::Socket::INET (LocalHost => 'xxx.xxx.xxx.xxx', LocalPor +t => 9321, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $sock; $reach = Net::Ping->new($ > ? "tcp": "icmp"); (defined $reach) or die "Couldn't create Net::Ping object: $!\n"; while ($new_sock = $sock->accept()) { while (defined ($buf = <$new_sock>)) { print $buf; $host = (split(/:/,$buf))1; print "$host"; if($reach->ping("$host")) { print "host is reachable"; } else { print "host is unreachable"; } } }#end of 1st while loop close ($sock);

MyClient.pl
#!/usr/bin/perl use IO::Socket; use Net::Ping; $destination = '198.148.129.182'; $sock = new IO::Socket::INET (PeerAddr => '172.30.166.234', PeerPort = +> 9321, Proto => 'tcp', ); die "Socket could not be created. Reason: $!\n" unless $sock; print $sock "ping:$destination \n"; close ($sock);
Cheers,
Rock

Edited by planetscape - added code tags

( keep:0 edit:23 reap:0 )

Replies are listed 'Best First'.
Re: Ping from remote host which is a Windows server
by Solo (Deacon) on Jun 13, 2006 at 15:44 UTC
    I would use POE, though it's a whole framework and can be a bit heavy. It has very good Win32 support (imo) and there are two cookbook examples that directly apply to your requirement here and here. There's also many other examples on the docu site.

    --Solo

    --
    You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
Re: Ping from remote host which is a Windows server
by jesuashok (Curate) on Jun 13, 2006 at 12:45 UTC
    Hi

    I have given the Pseudo code to you.

    Please try to apply poll or select on the socket file handle and do the stuff. It would be easy for you. I have not given the actcal code which I have.

    NOTE: If you want to write anything on the client socket pass the client socket handle as I have passed in the pseudo code. otherwise no need to pass that.

    #!/usr/bin/perl use IO::Socket; use Net::Ping; $sock = new IO::Socket::INET (LocalHost => 'xxx.xxx.xxx.xxx', LocalPor +t => 9321, Proto => 'tcp', Listen => 5, Reuse => 1 ); die "Socket could not be created. Reason: $!" unless $sock; $reach = Net::Ping->new($ > ? "tcp": "icmp"); (defined $reach) or die "Couldn't create Net::Ping object: $!\n"; while (1) { do a poll or select on Server socket Id which is '$sock'; whenever there is a new connection 'send_response' send_response ( $new_sock ); } sub send_response { my ( $new_sock ) = shift; if $reach->ping("$host") { print "host is reachable" } else { print "host is unreachable"; } return 1; }

    "Keep pouring your ideas"
      Thanks Ashok, just one more query, in your algorithm there is no need to determine the IP address and port number of clients cheers Rock
        Hi

        Yes, that is true. Because when you get the connection from client you will get the IP address to where you have to ping willl be a message in the socket. you have to read the IP address which comes as a message, then from your server script you have to ping for the IP, then you print the appropriate message on the client.

        Server <- client | reads IP from Client. Next process :- Ping the IP. Next process :- Server -> client | whether IP is pingable or not.

        "Keep pouring your ideas"