Saturday, May 14, 2011

Inetd and perl

Just a quick simple trick that you can help you set up servers quick and easy. You don't have to know alot about programming either but it helps to know what Inetd is in linux.

Inetd, on its manpages is known as a internet superserver. All those big words aside, it can basically listen on a given port for you and when a connection comes in, it calls the appropriate application to handle them. It so turns out that you can use Inetd's sockets for network communication instead of programming your own. What that means is that inetd can listen on port 80, and when a connection comes in on that port, we can run a shell script that simply sends back some text or html tags. Inetd's output is piped to the calling program or script's standard input and that program's output is redirected to Inetd's standard input.

Lets quickly demonstrate this with a bash script.

echo "Hello World"

Now save that script to a file called and give the file executable permissions.
# chmod 555

Now configure /etc/inetd.conf as follows

http-alt stream tcp4 nowait root /root/

Now save the file.

Run the inetd daemon
# /etc/init.d/inetutils-inetd start

now netcat to port 8080 (which is what http-alt) service is and you should revecieve a response:

root@bt~#: nc 8080
Hello World

All should work well if done right. Now to get a lil bit more fancy, i've put together a perl script that takes an input and returns the MD5 hash of that input (an MD5 hashing service if you will).
#!/usr/bin/perl -w

# A simple inetd socket server.

use strict;

my $old_fh = select(STDOUT);
$| = 1;
print "++ MD5 pass generator ++\n\n";
print "Type \'exit\' at anytime to quit\n";
print "Enter string to be hashed: ";

while( my $line = )
$line =~ s/\r?\n$//;
if ($line =~ /^exit$/)
die "shutting down\n";
# do your processing here!
$line = `echo -n $line | openssl md5`;
print "$line\n";
print "Enter string to be hashed: ";
Save the perl script to a file like and chmod 555 your file.
Start the inetd daemon as shown above and use netcat to connect to the service :)

No comments:

Post a Comment