Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Telnet to Device

Status
Not open for further replies.

sthames

Technical User
Apr 2, 2004
9
0
0
US
I am trying to write a simple script that just telnets to a device that spits out data but this device doesn't take in any input.

So I just need to telnet to the device on port 2000, and then read in line by line the data as it gets spit out. Each value is on a seperate line, and it spits out 5 of these value every once in a while.

This is an example of the data that is spit out every 5 minutes or so:

The Readings:
183
192
Blue
98223

I would like to read in each of these values and store them into variables, so that I could later put them in a file or into a database.

All I have so far is this:

use Net::Telnet ();
$instant = new Net::Telnet (Telnetmode => 0);
$instant->open(Host => '10.0.0.1',
Port => 2000);
$x = 0;
while ($x =<50) *Looping forever*
{
$instant->$waitfor('/[\n]*/');

I don't know how to read in a value and put it into a variable.

Thanks for any help.




 
And I don't think you should go for an infinite loop as it will possibly hog the system a lot. I'd go for reading maybe a few thousand lines, then storing them.

Is it possible to run the Perl script as a cron job (assuming you work on some kind of unix), if the time intervals between each info dump is the same?

And, by the way, what kind of device is it? I am very curious.
 
Well, I'm spamming away here, but anyway:

$x = 0;
while ($x == 1) {
@lines = $instant->getlines([Timeout => 180],
[All => 3]);
#something that stores @lines somewhere.
sleep(300);
}


This might get you started, I don't know which of the parameters that are required, but I think 'all', describing how many lines should be read, and 'timeout', describing how long it will keep listening before timing out of the listening, are required as they are an fundamental part of the methods operation.

And sleep, so it won't sit and listen all the time. That will hog lest of the processor than just listening?
 
The device I am talking to is an iseries Microserver that can be seen at the following address:


This device is connected to some sort of industrial scale that measures flour output for a bakery. The scale only spits out the data at inconsistent times depending on usage so I can't really use any timer(cron job). I can have one machine collecting the data and not doing anything else. Do you know a statement I could use that will take one line of output up to the return character?

Thanks for all your help.
Shawn
 
One line of output? Then that should be getline?

I would have dropped the sleep(300) on the example I posted, and just turned the timeout up really high, so it would just listen until it gets something.

It might be an ugly fix, but, well well....
 
I vaguely recall modifying this from a camel-like book.
It works. You will have to add your own logic.

#!/usr/bin/perl -w

require 5.002;
use strict;
use Socket;

my ($remote,$port, $iaddr, $paddr, $proto, $line);

$remote = shift || '172.16.3.20';
$port = shift || '42'; # nameserver
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr = inet_aton($remote) || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);

$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";
warn "Connected";
while ($line = <SOCK>) {
warn "Got data from remote";
print "\n[$line]\n";
}

close (SOCK) || die "failed close: $!";
warn "Exiting ";
exit;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top