Created
August 12, 2016 15:30
-
-
Save adkinss/050be9ca46baaebe80c43987e9a10e10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
## This script listens for a UDP message on a port containing the text "hello". | |
## The listener will respond back to the client with its own "hello" string. | |
## Clients can send messages to this listener using netcat. The following works: | |
## echo hello | nc -4u -w1 <listener_hostname> <listener_port> | |
## Substitute the hostname and port for where this listener is running from. | |
## Keep in mind that ports less than 1024 will require the listener to run as root. | |
## | |
## The listener does not yet take any command line options. | |
## Update $PORT for the port the udp server will listen on. | |
use strict; | |
use IO::Socket; | |
use Sys::Hostname; | |
my $localhost = hostname(); | |
my $PORT = 1162; | |
my $sock = IO::Socket::INET->new(LocalPort => $PORT, Proto => 'udp') or die "socket: $@"; | |
my $message; | |
while ($sock->recv($message, 1024)) { | |
my($port, $ipaddr) = sockaddr_in($sock->peername); | |
my $remotehost = gethostbyaddr($ipaddr, AF_INET); | |
my $datestring = localtime(); | |
if ($message =~ /hello/) { | |
chomp $message; | |
print "[$datestring] $remotehost: $message\n"; | |
$sock->send("HELLO $remotehost from $localhost\n"); | |
next; | |
} | |
} | |
die "recv: $!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment