develooper Front page | perl.beginners | Postings from December 2002

Hooking a perl script to xinetd

Thread Next
Michael Weber
December 27, 2002 10:18
Hooking a perl script to xinetd
Message ID:
I have a script that listens on a port for a connection, receives some
data over that connection, and writes it to a file.

This works, but it takes up CPU cycles, RAM etc. when it waits.  I am
trying to get the script to use xinetd instead.  That way the tcp daemon
will launch it when needed and let it die when it's done.

What I get when xinetd runs it,however, is "bind: Address already in

Here's the script.  (Fervent readers of Ellie Quigley will recognize it
right off as a modified copy of Example 18.21."

(My /etc/services file contains the entry (nukem  2222/tcp".)



#!/usr/bin/perl -T

# Program name:  nukem_listner,
# opens a Rendezvous Socket on port 2222
# and waits for a client to connect.
# When the client connects, this server receives the IP
# address of an offending host, blocks it with iptables,
# and adds it to the list of blocked hosts.
# Usage: IP_Address
use strict;
use warnings;
my($port, $AF_INET, $SOCK_STREAM, $sockaddr, # Variable declarations
                $name, $aliases, $proto, $this, $now);

$sockaddr = 'S n a4 x8';
if($port !~ /^\d+$/){
    ($name, $aliases, $port)=getservbyport($port,'tcp');
print "Port = $port\n";
$this = pack($sockaddr, $AF_INET, $port, "\0\0\0\0");
select(COMM_SOCK); $| = 1; select(STDOUT);

# Create R_SOCKET, the rendezvous socket descriptor
socket(R_SOCKET, $AF_INET, $SOCK_STREAM, $proto ) ||
    die "socket: $!\n";

# Bind R_SOCKET to my address, $this
bind(R_SOCKET, $this) || die "bind: $!\n";
listen(R_SOCKET, 5) || die "connect: $!\n";

# Infinite loop - wait until client connects, then serve the client
my $ip_addr = "";

accept(COMM_SOCK, R_SOCKET) || die "$!\n";
print COMM_SOCK "Got it!";

close (COMM_SOCK);

chomp $ip_addr;

open(LOGFILE, ">/var/log/nukem.log");
print LOGFILE "Got IP address of $ip_addr\n";


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About