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.
#!/bin/bash
echo "Hello World"
Now save that script to a file called hello.sh and give the file executable permissions.
# chmod 555 hello.sh
Now configure /etc/inetd.conf as follows
http-alt stream tcp4 nowait root /root/hello.sh
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 127.0.0.1 8080All 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).
Hello World
root@bt~#:
#!/usr/bin/perl -wSave the perl script to a file like md5.pl and chmod 555 your file.
# A simple inetd socket server.
use strict;
my $old_fh = select(STDOUT);
$| = 1;
select($old_fh);
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$//;
#chomp($line);
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: ";
}
Start the inetd daemon as shown above and use netcat to connect to the service :)
No comments:
Post a Comment