Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

A Perl Server Daemon

by emcb (Beadle)
on Dec 07, 2001 at 07:16 UTC ( #130144=perlquestion: print w/ replies, xml ) Need Help??
emcb has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im designing a server-daemon which will allow me to telnet to my server and run admin tasks with a special syntax, where certain commands are run against certain programs. This is the perl-code so far:
######################################################### # Emcb Remote Access Administration Server (RAAS(iserver-0.1)) ######################################################### # All you need to do to get the server up and running... # use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr # TCP Port $PORT = 9000; # pick something not in use # Create Socket Instance $client2 = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); # Cant Start Daemon die "can't setup server" unless $client2; # Daemon Up And Running print "[Server $0 accepting clients]\015\012"; while ($client2 = $client2->accept()) { # Flush The Client's STDOUT $client2->autoflush(1); # Welcome The User To The System print $client2 "Welcome to $0\015\012Type help for command list.\015 +\012"; $hostinfo = gethostbyaddr($client2->peeraddr); printf "[Connect from %s]\015\012", $hostinfo->name || $client2->pee +rhost; print $client2 "root\@iserver \$ "; while ( <$client2>) { next unless /\S/; # blank line if (/quit|exit|kill/i) { last; + } elsif (/env/i) { print $client2 `set`; } elsif (/motd/i ) { print $client2 `cat /etc/motd 2>&1`; + } elsif (/date|time/i) { printf $client2 "%s\015\012", scalar loc +altime; } elsif (/who/i ) { print $client2 `who 2>&1`; + } elsif (/cookie/i ) { print $client2 `/usr/games/fortune 2>&1 +`; } elsif (/motd/i ) { print $client2 `cat /etc/motd 2>&1`; + } else { $usage = "\015\012\015\012Usage: [exit|quit|kill] - Quit The Client [date|time] - Display The Current Date/Time [env|set] - Display The Curfrent Enviroment Variables\015\012\015\012" +; print($usage); } } continue { print $client2 "root\@iserver \$ "; } close $client2; }
But i have a few problems. When i run a command that outputs a lot of data the line breaks make it look like this: abnfjfjfjxjsjede jhcvdavchdahvdhavhadhv chdchehchaehcahec Instead of going to the beginning of a new line. And the output isnt outputed to the client, instead it comes up in my logs ( i run the cmd like: server > "/log.file" &). Can anyone help? Cheers, Elfyn

Comment on A Perl Server Daemon
Download Code
Re: A Perl Server Daemon
by perrin (Chancellor) on Dec 07, 2001 at 08:56 UTC
Re: A Perl Server Daemon
by gloryhack (Deacon) on Dec 07, 2001 at 11:34 UTC
    Save yourself a bucket of grief, and just use Charlie Pope's NetServer::Generic package.
Re: A Perl Server Daemon
by busunsl (Vicar) on Dec 07, 2001 at 12:56 UTC
    It looks like not all output is going to the log. Just the 'usage' output.
    You have to change the line print($usage); to print $client2 $usage;.

    The output gets garbled if I run the telnet client on windows, but is correct if I run it on Unix.
    This is due to the line ending. You can do something like:

    elsif (/env/i) {my $out = `set`; $out =~ s/\012/\015\012/g; print $cli +ent2 $out;}
    Your server crashes when the first connection is closed because you reuse the $client2 variable.
    Changed your Socket object to $socket. Check whether the socket is created and accept from it:
    my $socket = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); # Cant Start Daemon die "can't setup server" unless $socket; # Daemon Up And Running print "[Server $0 accepting clients]\015\012"; while ($client2 = $socket->accept()) { . . .
    And get rid of the close at the end of the program.

    And last but not least go to your favourite bookstore and buy Lincoln Stein's 'Network Programming'. It will be a real eye-opener.

Re: A Perl Server Daemon
by MZSanford (Curate) on Dec 07, 2001 at 14:55 UTC
    I know this does not help your actual error, but in the interest of security, there are a few things ...
    • dont trust the user
    • dont trust the user
    • and, dont trust the user

    I would suggest doing a few things. For each of the m//i's in the if/else, i suggest using lc(), and then using eq ... this prevents evil hacker code man motd from matching m/motd/i. Also, on the non-trusted user front, having the command coded in is very good, but i would think that the environment could have some very usefull information in it, were the wrong person to be pokiing around. This best thing to do, in the short term, is design a protocal for logging in (something as simple as FTP's login procedure).
    Lastlky, on the error, i do not believe programs like motd use \r\n for newlines, so it is possible that is causing some issues.
    $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
Re: A Perl Server Daemon
by fuzzysteve (Beadle) on Dec 07, 2001 at 17:10 UTC
    suggestion for you, hoe about using xinetd (or inetd) to handle the server stuff. then your program only deal iwth stdio.
    It also means you don't have thew server sitting and eating resources constantly. it will be started when its needed. (disadvantage: perl has to be started each time.) you could always have xinetd start your sever program and let that fork out individual instances as required, and colse itself after a set time of no connections.
    look at this node for some basic details.
Re: A Perl Server Daemon
by merlyn (Sage) on Dec 07, 2001 at 20:06 UTC
    In addition to the other suggestions, why not just construct a mini-web-server with HTTP::Daemon, and then you can use basic-AUTH and SSL from any web browser to activate your programs and see the results? Use proven protocols: stop reinventing that wheel unless you're prepared to put in the literally man-years of effort to make it better.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://130144]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-09-16 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (156 votes), past polls