<?xml version="1.0" encoding="windows-1252"?>
<node id="997666" title="Sniffing a telnet session into a file ..." created="2012-10-07 03:32:59" updated="2012-10-07 03:32:59">
<type id="115">
perlquestion</type>
<author id="997643">
RedTussock</author>
<data>
<field name="doctext">
I operate a home made 1 wire weather station which I monitor using a very good app called OWW. OWW sends a txt line of data out on port 8891 on the localhost which can be read by telnet 'ing the localhost on that port. What I would love to do is establish a telnet session, take the 1 line result and write it to a file and eventually upload onto the net .. Has anyone here had a go at something like this .. below is the code I am working on ... and code I use to grab data from the MySQL server which OWW keeps fed as well.
&lt;code&gt;
#!/usr/bin/perl -w
#Telnet Script to read the OWW tcp port and write the output to a file for upload to the net
#
#
use Net::Telnet ();
use Cwd;

open my $fh, '&gt;', '/home/Smitty/scripts/perl/WLStickers.txt' or die $!;


{
    $t = new Net::Telnet (Timeout =&gt; 10,
                   Output_log =&gt; $ofile);

    
    $t-&gt;open("127.0.0.1 8891");
    
    $t-&gt;close;
}


exit 0
&lt;/code&gt;
I acknowledge this is not working ... just not perl savvy enough to get it to go easily ... maybe the pixies will fix it overnite :-)
Following is a script which was working uploading the required file, in the format requested by a local weather net group.

&lt;code&gt;
#!/usr/bin/perl -w
 use Net::FTP;
 use DBI;
 
 $dbh = DBI-&gt;connect('dbi:mysql:database','user','pass')
 or die "Connection Error: $DBI::errstr\n";
 $sql = "select concat_ws(',' , TIME_FORMAT(NZST,'%h:%i')  ,DATE_FORMAT(NZST,'%d/%m/%y'),t2,'',wdchill,rh,dp,bp,'',wdspd,ROUND(wddeg),rn,'','','','',wdgst,'c|kmh|mb|mm') FROM tbl_owwdata ORDER BY wx_idx DESC LIMIT 1";
 $sth = $dbh-&gt;prepare($sql);
 $sth-&gt;execute
 or die "SQL Error: $DBI::errstr\n";
 while (@row = $sth-&gt;fetchrow_array) {
 open (MYFILE, '&gt;WL_stickertags.txt');
 print MYFILE "@row\n";
 close (MYFILE); 
 }
 

    $ftp = Net::FTP-&gt;new("some.where.com", Debug =&gt; 0)
      or die "Cannot connect to Simplehost: $@";

    $ftp-&gt;login("******",'*******')
      or die "Cannot login ", $ftp-&gt;message;

    $ftp-&gt;cwd("/a.b.c/folder")
      or die "Cannot change working directory ", $ftp-&gt;message;

    $ftp-&gt;put("WL_stickertags.txt")
      or die "put failed ", $ftp-&gt;message;

    $ftp-&gt;quit;
 
 exit 0

&lt;/code&gt;

Of course it would be much more fun to snif the 1 wire net directly and format a web page with results and graphs etc. But small steps grasshopper ... 

 </field>
</data>
</node>
