Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Sniffing a telnet session into a file ...

by RedTussock (Acolyte)
on Oct 07, 2012 at 07:32 UTC ( #997666=perlquestion: print w/ replies, xml ) Need Help??
RedTussock has asked for the wisdom of the Perl Monks concerning the following question:

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.
#!/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, '>', '/home/Smitty/scripts/perl/WLStickers.txt' or die $! +; { $t = new Net::Telnet (Timeout => 10, Output_log => $ofile); $t->open("127.0.0.1 8891"); $t->close; } exit 0
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.
#!/usr/bin/perl -w use Net::FTP; use DBI; $dbh = DBI->connect('dbi:mysql:database','user','pass') or die "Connection Error: $DBI::errstr\n"; $sql = "select concat_ws(',' , TIME_FORMAT(NZST,'%h:%i') ,DATE_FORMA +T(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->prepare($sql); $sth->execute or die "SQL Error: $DBI::errstr\n"; while (@row = $sth->fetchrow_array) { open (MYFILE, '>WL_stickertags.txt'); print MYFILE "@row\n"; close (MYFILE); } $ftp = Net::FTP->new("some.where.com", Debug => 0) or die "Cannot connect to Simplehost: $@"; $ftp->login("******",'*******') or die "Cannot login ", $ftp->message; $ftp->cwd("/a.b.c/folder") or die "Cannot change working directory ", $ftp->message; $ftp->put("WL_stickertags.txt") or die "put failed ", $ftp->message; $ftp->quit; exit 0
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 ...

Comment on Sniffing a telnet session into a file ...
Select or Download Code
Re: Sniffing a telnet session into a file ...
by zentara (Archbishop) on Oct 07, 2012 at 11:24 UTC
Re: Sniffing a telnet session into a file ...
by aitap (Deacon) on Oct 07, 2012 at 13:24 UTC
    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
    You don't need to telnet to it. Just use IO::Socket::INET to connect to the host, read a text line from it and write it anywhere.
    Sorry if my advice was wrong.
      Even better ... :-)
Re: Sniffing a telnet session into a file ...
by NetWallah (Abbot) on Oct 07, 2012 at 15:37 UTC
    If this is a simple "fetch one line" job, you can just use a bash command reading /dev/tcp to accomplish it.

    See Using Bash's Built-in /dev/tcp File (TCP/IP).

    In case you need it, there is also a /dev/udp.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Yes in fact OWW does do udp as well ... Is there actually a perl 1 wire tool ?.
        A Google search for "perl 1 wire" leads to many hits including DigiTemp which is "is a simple to use program for reading values from 1-wire devices".

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Re: Sniffing a telnet session into a file ...
by zentara (Archbishop) on Oct 08, 2012 at 11:03 UTC
      Yep that is all fine and dandy ... but what I have coming in of the 1 wire network is multiple 1 wire devices with uynique ID's ... what is coming out the tcp port on 8891 from oww looks like
      Smitty@smittytech:~> telnet localhost 8891 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 11/10/12 , 18:36:27 , ,�C , 6.87, KPH, South ,180.0 + , 8 , 0.00, mm 11/10/12 , 18:36:27 , ,�C , 6.00, KPH, SE ,135.0 + , 6 , 0.00, mm 11/10/12 , 18:36:30 , ,�C , 5.91, KPH, SSE ,157.5 + , 7 , 0.00, mm 11/10/12 , 18:36:32 , ,�C , 4.31, KPH, South ,180.0 + , 8 , 0.00, mm 11/10/12 , 18:36:34 , ,�C , 4.26, KPH, South ,180.0 + , 8 , 0.00, mm 11/10/12 , 18:36:37 , ,�C , 4.25, KPH, South ,180.0 + , 8 , 0.00, mm 11/10/12 , 18:36:39 , ,�C , 5.09, KPH, SW ,225.0 + , 10 , 0.00, mm 11/10/12 , 18:36:41 , ,�C , 4.16, KPH, SE ,135.0 + , 6 , 0.00, mm 11/10/12 , 18:36:44 , ,�C , 3.35, KPH, SW ,225.0 + , 10 , 0.00, mm 11/10/12 , 18:36:46 , ,�C , 4.21, KPH, SW ,225.0 + , 10 , 0.00, mm 11/10/12 , 18:36:48 , ,�C , 4.28, KPH, SSW ,202.5 + , 9 , 0.00, mm ^C^C
      So I either need to kill oww and read the device of the net and do all the 1W net comms using a perl script and hand the result into your suggested send script. Clever idea ... but I need to go deeper into the network comms, which means reinventing a wheel.
        I don't know what oww is exactly, a c program? If my assumption is correct, oww reads the 1w net and reports it out on port 8891. Maybe you should see if oww has options to just print out to STDOUT instead of port 8891, if so you could run it with some Perl IPC, like a piped open or Open3, then let Perl send it out on a regular socket connection.

        It may also be a McGiver hack, but you could have another Perl script on the oww machine to read port 8891, and relay it out a normal socket.

        Can you get the source code to oww? You could hack it to wtite to STDOUT.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-07-30 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls