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

I would like to make a telnet client for a chat (jdkchat).
I have yet posted something similar in 'Snippets' (something like 'telnet bot with Net::Telnet')
node_id=356760
So in this version things work different:

A writer.pl script server that make a connection with the telnet server and create a 'sock' connection.
A sender.pl that take arguments and send messages to the server.
This script can also read in different way the log_file.
Next step is: make it a web plug-in that can connect at the telnet server with the bot


WRITER.PL
#!/usr/bin/perl # #################################### # # JDKCHAT WRITER SERVER # # (Studing sockets) #################################### use strict; use warnings; use IO::Socket; use Net::Telnet; ##################################### #telnet connection my $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die', Port=>2002, Prompt => '/bash\$ $/' ); #socket my $sock = new IO::Socket::INET (LocalHost => 'oni-nux', LocalPort => 8000, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "could not connect: $!" unless $sock; die "can't connect to the chat server: $!" unless $telnet; $telnet->open('localhost'); my @welcome = $telnet->waitfor('/Your Name:$/i'); my $new_sock; my $buf; my $name = 'recipientbot'; chomp $name; $telnet->print($name); my @buffer_in; $telnet->waitfor('/>$/i'); while ($new_sock = $sock->accept()) { while (defined ($buf = <$new_sock>)) { $telnet->print($buf); @buffer_in = $telnet->waitfor('/ >$/i'); print @buffer_in,"\n"; } } close ($sock);


SENDER.PL
#!/usr/bin/perl # # SOCKET JDKCLIENT SEND MSG TO THE SERVER use strict; use warnings; use IO::Socket; my $chat_path='/usr/local/src/jdkchat-1.5/'; my $log_file='log_file'; my $mode; my $opt; my $msg; if (defined $ARGV[0]) {$mode = $ARGV[0]} else {$mode=0;} if (defined $ARGV[1]) {$opt = $ARGV[1]} else {$opt='tail';} my $sock = new IO::Socket::INET (PeerAddr => 'oni-nux', PeerPort => 8000, Proto => 'tcp', ); die "Socket could not be created. Reason is $!" unless $sock; if ($mode eq 'who') { print $sock "/who"; } if ($mode eq 'priv_msg') { #print $sock "/who"; print "send a message to ... [connection number]"; print "connection number:\n>"; my $user = <STDIN>; chomp ($user); print "msg?"; $msg = <STDIN>; chomp ($msg); print $sock '/',$user,"$msg","\n>"; } if ($mode eq 'read') { _read_log($opt, $chat_path, $log_file); } if ($mode eq 'msg') { $msg=<STDIN>; print $sock "$msg"; } close ($sock); ####################################################### # SUB ####################################################### sub _read_log { my $opt=$_[0]; my $chat_path=$_[1]; my $log_file=$_[2]; if ($opt eq "tail") { _tail ($chat_path,$log_file); } elsif ($opt eq "all") { _all ($chat_path,$log_file); } else { die "unrecognized command: $!" } }; sub _tail { my $line; my $log_file = $_[0].$_[1]; print $log_file,"\n"; system ("tail $log_file > tail.log"); open (TAIL, 'tail.log'); while ( $line = <TAIL> ) { if ($line=~/JDKCHAT:/) { next; } else { print $line; } } close (TAIL); }; sub _all { my $line; my $log_file = $_[0].$_[1]; open (LOGFILE, "$log_file"); while ($line = <LOGFILE>) { if ($line =~ /JDKCHAT:/) { next; } else { print $line; } } close (LOGFILE); };

It is possible to tranform it to a CGI?
What about call it with a php script?
My task is not use bin, but use the system calls of php (NO CGI ON THE SERVER).

Replies are listed 'Best First'.
Re: Perl Telnet Client
by Joost (Canon) on Jun 01, 2004 at 19:15 UTC
    It is possible to tranform it to a CGI?

    everything is possible, what are you trying to achieve?

    What about call it with a php script?

    See above

    My task is not use bin, but use the system calls of php (NO CGI ON THE SERVER).

    I (and judging from the lack of responses, most people) can't figure out what you are talking about. Please explain yourself.

      I try to explain:
      1. I have a chat server at telnet port 2002
      2. techies connect to the chat with a telnet client
      3. i want to make a client for the web page (http:recipient.cc)
      4. In the apache server cgi is NOT active.
      5. So I try to use php to call a perl script
      6. The php script send the information to a flash movie
      7. for a something similar you can search for Node 356769 (Flash and Perl)