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).

In reply to Perl Telnet Client by tucano

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.