Advertisement
dalgeek

Multicast testing script

Feb 27th, 2015
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.43 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use IO::Socket::Multicast;
  5.  
  6. # Basic IO::Socket script to send and receive multicast messages
  7. # If successful, you should see the following output:
  8. # $ ./mcast.pl send 239.195.1.10 16384
  9. # Sending to 239.195.1.10:16384
  10. # Test packet 0 Fri Feb 27 16:24:48 2015
  11. # Test packet 1 Fri Feb 27 16:24:49 2015
  12. # Test packet 2 Fri Feb 27 16:24:50 2015
  13. # Test packet 3 Fri Feb 27 16:24:51 2015
  14. # Test packet 4 Fri Feb 27 16:24:52 2015
  15. #
  16. # $ ./mcast.pl recv 239.195.1.10 16384
  17. # Test packet 0 Fri Feb 27 16:24:48 2015
  18. # Test packet 1 Fri Feb 27 16:24:49 2015
  19. # Test packet 2 Fri Feb 27 16:24:50 2015
  20. # Test packet 3 Fri Feb 27 16:24:51 2015
  21. # Test packet 4 Fri Feb 27 16:24:52 2015
  22.  
  23. sub msend {
  24.         my $group = shift;
  25.         my $port = shift;
  26.         my $dest = join(":", $group, $port);
  27.  
  28.         # Subtract 1 from the local port so we don't interfere if we try to listen locally
  29.         my $s = IO::Socket::Multicast->new(LocalPort=>$port-1) || die "$!";
  30.         $s->mcast_if('eth0');
  31.         $s->mcast_ttl(16);
  32.         $s->mcast_loopback(1);
  33.         print "Sending to $dest\n";
  34.         my $ct = 0;
  35.         while (1) {
  36.                 my $message = "Test packet ".$ct++." ".localtime();
  37.                 print "$message\n";
  38.                 $s->mcast_send($message, $dest);
  39.                 sleep 1;
  40.         }
  41.         return 0;
  42. }
  43.  
  44. sub mrecv {
  45.         my $group = shift;
  46.         my $port = shift;
  47.  
  48.         my $s = IO::Socket::Multicast->new(LocalPort=>$port) || die "$!";
  49.         $s->mcast_add($group, 'eth0');
  50.         while (1) {
  51.                 my $data;
  52.                 $s->recv($data, 1024);
  53.                 print $data."\n";
  54.         }
  55.         return 0;
  56. }
  57.  
  58. sub usage {
  59.         print "Usage: $0 [send|recv] <group> <port>\n";
  60.         print "Valid multicast groups are in the range 224.0.0.0 to 239.255.255.255\n";
  61.         print "Valid ports are 1025-65535\n";
  62.         exit 0;
  63. }
  64.  
  65. my ($mode, $group, $port);
  66. $mode = $ARGV[0];
  67. $group = $ARGV[1];
  68. $port = $ARGV[2];
  69.  
  70. # Basic checks on variables
  71. if (!defined($mode) || $mode !~ /send|recv/) { usage() }
  72. if (!defined($group) || $group !~ /\d{3}(\.\d{1,3}){3}/) { usage() }
  73. # Don't use ports less than 1025 so we don't have to worry about root privileges
  74. if (!defined($port) || ($port < 1025 || $port > 65535)) { usage() }
  75.  
  76. if ($mode eq "send") { msend($group, $port) }
  77. elsif ($mode eq "recv") { mrecv($group, $port) }
  78. else { usage(); }
  79.  
  80. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement