Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Perl as a telnet server?

by Anonymous Monk
on Dec 14, 2002 at 04:05 UTC ( [id://219812]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What would be the best way of making a telnet server in perl? Being that I could log into it on port 23, send the remote computer text commands, connect from another location to the same instance, etc.

From what I understand, when the first connection is made, port 23 would be locked in use. How would I allow for multiple concurrent connections to the same port? How would I account for multiple sessions?

Thanks.

Replies are listed 'Best First'.
Re: Perl as a telnet server?
by pg (Canon) on Dec 14, 2002 at 04:24 UTC
    No, this is a misunderstanding. On the server side, the socket listening on a particular port, can accept multiple connections.

    What you cannot do, is to have another socket listening on the same port. (Even this is not quite right, but I don't want to make things too complex, by involving address and port reuse...)

    I attached a piece of demo, not a telnet server, but it provides all the basic technics that you are looking for:
    proxy.pl: use strict; use threads; use IO::Socket::INET; $| ++; my $listener = IO::Socket::INET->new(LocalPort => 3126, Listen => 5, Reuse => 1) || die "Cannot create socket\n"; my $client; my $client_num = 0; while (1) { $client = $listener->accept; threads->create(\&start_thread, $client, ++ $client_num); } sub start_thread { threads->self->detach(); my ($client, $client_num) = @_; print "thread created for client $client_num\n"; while (1) { my $whole_req = ""; do { my $req; $client->recv($req, 700000); return if ($req eq ""); $whole_req = $whole_req . $req; } until ($whole_req =~ m/\r\n\r\n/x); print "client $client_num got req:\n$whole_req"; $whole_req =~ m/Host: ([\.|\w]*)/; my $host = $1; my $server = new IO::Socket::INET(Proto => "tcp", PeerPort => 80, PeerAddr => $host) || die "failed to connect to $host\n"; print $server $whole_req; my $whole_res = ""; do { my $res; $server->recv($res, 700000); $whole_res = $whole_res . $res; } until ($whole_res =~ m/<\/html>/); print "client $client_num got res\n"; print $client $whole_res; close($server); } return; } tester.pl: use strict; use IO::Socket; my $server = IO::Socket::INET->new(Proto => "tcp", PeerPort => 3126, PeerAddr => "localhost", Timeout => 2000) || die "failed to connect\n"; my $req = "GET / HTTP/1.1\r\nHost: $ARGV[0]\r\n\r\n"; while (1) { print $req; print $server $req; my $res; my $whole_res = ""; do { $server->recv($res, 70000); exit if ($res eq ""); $whole_res = $whole_res . $res; } until ($res =~ m/<\/html>/); print $whole_res; }
Re: Perl as a telnet server?
by Revelation (Deacon) on Dec 14, 2002 at 04:50 UTC
Re: Perl as a telnet server?
by batkins (Chaplain) on Dec 14, 2002 at 04:31 UTC
    well, you'd need to have a main loop using the listen function to accept incoming connections on port 23. when a connection is accepted, you'd fork into a child and a parent. the parent would continue to listen or new connections and the child would then handle all commands from the newly-connected client.

    writing a full-fledged telnet server could be a difficult thing to do. keep in mind that there are many freeware telnet servers that may do exactly what you need.

Re: Perl as a telnet server?
by osama (Scribe) on Dec 14, 2002 at 10:14 UTC
    why not use inetd or xinetd? they handle the sockets, sessions, concurrent connections, and host security parts pretty well...
Re: Perl as a telnet server?
by logan (Curate) on Dec 14, 2002 at 05:49 UTC
      A quick question:
      Why use Expect.pm, when you can use Net::Telnet? Could you provide some sort of cursory explenation about why Expect programming would be better for this cause?
      Gyan Kapur
      gyan.kapur@rhhllp.com
        I'm not sure what the cause is. Sure, Net::Telnet will establish the connection, but Expect.pm has all sorts of functions for looking for specific user-defined responses, and automatically recognizes a prompt. If it's a matter of telnetting in, doing an ls, and exiting, Expect is overkill, but if there's an extended conversation between client and server, Expect makes life easier.

        -Logan
        "What do I want? I'm an American. I want more."

Re: Perl as a telnet server?
by logan (Curate) on Dec 14, 2002 at 04:50 UTC
    I don't have my books with me, but I am dead sure that there's a chunk of code for a server and a corresponding client in either the Camel book or the Perl Cookbook.

    -Logan
    "What do I want? I'm an American. I want more."

Re: Perl as a telnet server?
by Avox (Sexton) on Dec 16, 2002 at 20:59 UTC
    I've just put together a system like this to automate some tests. However, I didn't write my own telnet server. Since the machines we were running were NT, I wasn't able to find a free telnet server, so I just tried out a evaluation copy of one. *if anyone knows of a free nt telnet server, please share, that would be nice to know* If I did it again, I'd just write a simple server listening for particular commands and run my tests that way. The telnet road works, but its not at nice as I'd like. If you don't need to, I'd just write a simple listener/talker system. But then again, maybe you need telnet in particular. In that case, the other's comments are on the money. Don't forget to read the telnet spec.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://219812]
Approved by tame1
Front-paged by tame1
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found