Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl script

Status
Not open for further replies.

ajay458

Technical User
Joined
Jun 8, 2009
Messages
2
Location
US
Hi,

I am trying to write a server client perl daemon that would do the following:

1) Server would send out requests to the clients to perform certain actions

2) Clients would receive the actions and send output back to server

TIA,
Ajay
 
Congratulations!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Sincere apologies. I was very vague in my request. After looking at several examples on this forum, I have a sample script working with one-way communication but need help not only in sending data back to the server but to also convert these to daemons. BTW, here is my code:

Sender:

#!/usr/bin/perl

use IO::Socket::INET;

# Create a new socket
$MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',PeerAddr=>'3.24.141.91');

# Send actions
$def_action="Action: ";
print "\n",$def_action;
while($action=<STDIN>)
{
chomp $action;
if ($action ne '')
{
print "\n Action to be performed: '",$action,"'";
if($MySocket->send($action))
{
print ".....<done>","\n";
print $def_action;
}
}
else
{
# Send an empty action and exit
$MySocket->send('');
exit 1;
}
}


Receiver:

#!/usr/bin/perl
use IO::Socket::INET;

# Create a new socket
$MySocket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp');

$def_msg="\nReceived action.....\n";
print "\n",$def_msg;

while(1)
{
$MySocket->recv($action,128);
if ($action eq 'zonestats')
{
print "\nAction: $text\n";
print "Running ....\n\n";
$zonestats="/ztools/scripts/zonestat -l";
system($zonestats);
}
elsif ($action eq 'zonesize')
{
print "\nAction: $text\n";
print "Running zonesize....\n\n";
$zonesize="/ztools/scripts/zonesize -a";
system($zonesize);
}
# If action is empty, exit
else
{
print "No request made. Exiting\n";
exit 1;
}
}


Again, TIA.
Ajay
 
A few small things:

1) There's a way of using IO::Socket and IO::Select so that your server can create socket objects for each client that connects. With these sockets, you can send data back to the client, in a similar way that the client sends data to the server.

Code:
my $server_sock = IO::Socket::INET->new (LocalPort => ..., ...);
my $select = IO::Select->new($server_sock);

# infinite loop to accept new connections
# and respond to data
while (1) {
   my @ready = $select->can_read(); # get ready clients
   next unless @ready;

   foreach my $client_sock (@ready) {
      if ($client_sock == $server_sock) {
         # This is a new client connecting
         my $new_client = $server_sock->accept();
         $select->add($new_client);

         # Say hi to the client
         $new_client->send("Hello there!\n");
      }
      else {
         # This client was connected before, read their data
         my $buffer;
         $client_sock->recv($buffer,2048);

         # do whatever with $buffer

         # send a response
         $client_sock->send("My reply: $reply\n");
      }
   }
}

2) If you want to capture the output of a system command, don't use system(), use backticks.

Code:
my $tracert = `tracert google.com`;
print "Traceroute results: $tracert\n";

3) If you want to capture STDERR on your system commands in addition to STDOUT (since some utilities, usually in Unix, use STDERR a lot), redirect STDERR to STDOUT in your command.

Code:
my $tracert = `tracert google.com 2>&1`;

more on I/O redirection.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top