Advertisement
Guest User

Untitled

a guest
May 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.70 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. # udpqotd - UDP message server
  3. use strict;
  4. use IO::Socket;
  5. my(@pids, $i, $sock, $replymsg, $newmsg, $hisaddr, $hishost, $MAXLEN, $PORTNO);
  6.  
  7. $MAXLEN = 1024;
  8. $PORTNO = 9595;
  9.  
  10. $sock = IO::Socket::INET->new(
  11.         LocalPort => $PORTNO,
  12.         Proto => 'udp',
  13. ) or die "socket: $@";
  14.  
  15. print "Awaiting UDP messages on port $PORTNO\n";
  16.  
  17. while ($sock->recv($newmsg, $MAXLEN)) {
  18.         if($newmsg =~ /failure$/){
  19.                 my($port, $ipaddr) = sockaddr_in($sock->peername);
  20.                 $hishost = gethostbyaddr($ipaddr, AF_INET);
  21.                 print "Client on $hishost discovered irc Service $newmsg\n";
  22.                 $replymsg = "Services Starting";
  23.                 $sock->send($replymsg);
  24.                 system('~/services/services.pl');
  25.         }
  26.         elsif($newmsg =~ /started$/){
  27.                 my($port, $ipaddr) = sockaddr_in($sock->peername);
  28.                 $hishost = gethostbyaddr($ipaddr, AF_INET);
  29.                 print "IRC Services started on $hishost, killing local services.pl\n";
  30.                 open(PIDOF_PIPE,"pidof -x services.pl|");
  31.                 while (<PIDOF_PIPE>){
  32.                         chomp;
  33.                         my $count = scalar(@pids);
  34.                         @pids = split(' ', $_);
  35.                         $i = 0;
  36.                         foreach $count (@pids){
  37.                                 system("kill","$pids[$i]");
  38.                                 print "[KILLED] $pids[$i]\n";
  39.                                 $i++;
  40.                         }
  41.                 }
  42.         }
  43.         else{
  44.                 $replymsg = "Unrecognized Command Recived";
  45.                 $sock->send($replymsg);
  46.         }
  47. }
  48. die "recv: $!";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement