LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-26-2009, 06:18 AM   #1
MissEileen
LQ Newbie
 
Registered: Mar 2005
Location: Santiago - Chile
Distribution: ubuntu
Posts: 10

Rep: Reputation: 0
reading input from telnet into php


hi all,
I need help with a little project:
I have a sensor whose data must be shown in a web page. up to now, the only way to acquire data is by using:

$ telnet xxx.xxx.xxx.xxx 10001

which results in an infinite loop of data written (1 line per second), with the following format:

30 lines of: HELLO "foo"
1 line of: GOODBYE "the data i care about (numbers)"

what i need is to read the numbers following the keyword GOODBYE. so, i first tried outputting the lines i need into a file, to let php do its work from there.

from the shell, if i do:

$ telnet xxx.xxx.xxx.xxx 10001 |grep GOODBYE

i get only the lines i need. if i do:

$ telnet xxx.xxx.xxx.xxx 10001 > file.txt

i get all the lines written into the file; but if i do

$ telnet xxx.xxx.xxx.xxx 10001 |grep GOODBYE >file.txt

nothing is written into the file!

so, my questions are:
- why isn't the last command working as i expect? or
- do you have any other ideas on how to approach this project?


thank you very much!
 
Old 01-26-2009, 08:32 AM   #2
sydney-troz
Member
 
Registered: Feb 2007
Distribution: Kubuntu, it's obese barely-usable sibling, Ubuntu
Posts: 142

Rep: Reputation: 15
Not sure why it wouldn't work off the top of my head, but what happens if you do

Code:
telnet xxx.xxx.xxx.xxx 10001 | grep GOODBYE | tee file.txt
 
Old 01-26-2009, 08:39 AM   #3
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,012

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
I don't know why it's not working. bash can be awkward sometimes
I'd try
telnet xxx.xxx.xxx.xxx 10001 > file1
grep GOODBYE file1 > file2
cat file2
 
Old 01-26-2009, 09:19 AM   #4
MissEileen
LQ Newbie
 
Registered: Mar 2005
Location: Santiago - Chile
Distribution: ubuntu
Posts: 10

Original Poster
Rep: Reputation: 0
correction:

Code:
$ telnet xxx 10001 |grep GOODBYE > file.txt
writes GOODBYE lines to file, but only after killing the telnet process (which is not useful); same happens

Code:
$ telnet xxx 10001 |grep GOODBYE |tee file.txt
any ideas how to fix this?
 
Old 01-26-2009, 09:23 AM   #5
MissEileen
LQ Newbie
 
Registered: Mar 2005
Location: Santiago - Chile
Distribution: ubuntu
Posts: 10

Original Poster
Rep: Reputation: 0
tredegar: thanks; i'd rather process "file1" with php directly (which is what I'm currently doing), but I really don't want the file growing unnecessarily big. I know I can rename/delete it on a periodic basis with a cronjob, but I'm looking for an "elegant" solution.
 
Old 01-26-2009, 09:50 AM   #6
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,012

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Quote:
writes GOODBYE lines to file, but only after killing the telnet process (which is not useful)
Ah Ha! This is because your telnet to port 10001 is continually reading lines, and never finishes.
There must be a better way to get the data you need.
Maybe have the remote host write the sensor data to a file and then just
scp the file from the remote host?

What sort of process do you have running on port 10001 on the remote host?

Can you make it send the sensor data, and then terminate the connection?

Last edited by tredegar; 01-26-2009 at 09:57 AM.
 
Old 01-26-2009, 11:49 AM   #7
MissEileen
LQ Newbie
 
Registered: Mar 2005
Location: Santiago - Chile
Distribution: ubuntu
Posts: 10

Original Poster
Rep: Reputation: 0
I don't mean to be rude, but I already said in the first post that the telnet produces an infinite loop and it's the only way to acquire data. source machine is given as a black box; no chance for scp. thanks.
 
Old 01-26-2009, 12:31 PM   #8
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,012

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Quote:
I don't mean to be rude, but I already said in the first post that the telnet produces an infinite loop and it's the only way to acquire data.
I don't mean to be rude either, but you omitted the part I have underlined

Maybe write a script along the lines of:
Code:
#!/bin/bash
# WARNING pseudocode follows
do
  read foo
  if $foo is what you are interested in then do_something_with_it
until you_are_bored
Call it like this:
Code:
telnet xxx.xxx.xxx.xxx 10001 | /path/to/script
 
Old 01-26-2009, 01:48 PM   #9
MissEileen
LQ Newbie
 
Registered: Mar 2005
Location: Santiago - Chile
Distribution: ubuntu
Posts: 10

Original Poster
Rep: Reputation: 0
Looks like a good idea, but I have almost no experience with bash. I don't know if the condition
"you_are_bored" might be a timestamp or a period of time, or a certain amount of lines in the
file, etc. So, it's gonna take a while for me to test, but I'll try it; thank you.


by the way, the exact phrase "the only way to acquire data is by using: $ telnet ..." is on my
first post indeed.
 
Old 01-26-2009, 04:57 PM   #10
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,012

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Quote:
Looks like a good idea, but I have almost no experience with bash
I'm also still a beginner with bash, but not quite a "newbie".

The point I am making is that you are receiving an (it seems now, an endless) data-stream from your telnet command.

You can pipe (the "|" in my previous post) this data to a script that will read each line one at a time and assign each line to the variable $foo

You can then use bash scripting to deal with each line (as assigned to the variable $foo) in turn: If it is saying HELLO foo, then you can safely discard it (but why does it need to do this THIRTY times?)

If it is saying "GOODBYE "the data i care about (numbers)"" then you can filter this out ( maybe with grep and cut ?) and then process it as you see fit.

Quote:
"you_are_bored" might be a timestamp or a period of time, or a certain amount of lines in the file, etc.
Well, that was a kind of a joke. Sorry. You could define you_are_bored=false and it would continue forever, which is maybe what you need. It was pseudocode anyway. Cheer up!

Seriously, I am alerting one of LQ's bash-gurus to this thread (Tinkster, are you listening? HELP!), and I hope he'll be able to help you better than I can. He may be busy though.

Quote:
by the way, the exact phrase "the only way to acquire data is by using: $ telnet ..." is on my first post indeed.
Ok. I'm just trying to help you out. I'm busy too. My apologies if I mis-read.
 
Old 01-26-2009, 08:42 PM   #11
MissEileen
LQ Newbie
 
Registered: Mar 2005
Location: Santiago - Chile
Distribution: ubuntu
Posts: 10

Original Poster
Rep: Reputation: 0
[OFF TOPIC!]

hey tredegar,
I might not know a lot of bash scripting, but I do understand pseudocode; so when I said your idea looks good and I'm gonna try it, I really meant that (well, maybe next week, 'cause my work shift ends tomorrow); no need to further explain! Also, I did get the joke and appreciate your humor and help, 'cause in most forums people are jerks and treat you badly just for not knowing stuff. Actually, this is the only place where I have found nice, patient answers; so, thank you very much.

Just to satisfy your curiosity: the output strings aren't actually "HELLO foo" and "GOODBYE foo"; they're just 2 different strings followed by numbers. I changed them to explain easily. The HELLO lines contain current data for one sensor (every second) while the GOODBYE lines contain average values for all sensors every 30 secs, which are the data I need to work with. And the source data machine is a weather station with an IP, not a computer, hence the unfeasibility of any command other than telnet through port 10001.

***sorry if I broke any rules by going off topic ***
 
Old 01-26-2009, 09:23 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,903

Rep: Reputation: 746Reputation: 746Reputation: 746Reputation: 746Reputation: 746Reputation: 746Reputation: 746
Just to throw out something else to try would be the netcat command.
 
Old 01-27-2009, 03:01 AM   #13
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,012

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
Tinkster is indeed busy, but he recommends you take a look at the telnet man-page, searching for "flush", and also suggests you may need to use expect to get it working.

netcat looks a good tool too (thanks michaelk)

So, give it a go.

If / when you come back here with some code that is partly working, it would be useful if you gave us a real sample of the data stream you are trying to process (within [CODE] tags please, so we can see exactly how it is formatted)

Good luck.
 
Old 01-29-2009, 10:01 AM   #14
humandynamo
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Rep: Reputation: 0
I believe this is on topic as it is actually concerning netcat. I am currently having some struggles with the the processing of the <STDIN> from my executable.

netcat -v -v IPaddress port -e ./myprogram.pl

Within "myprogram.pl" I do the following to get the user input:
$sLine = "";
$sLine = <>;
print = "Received: $sLine\n\r";

I receive the warning "Use of uninitialized value in concatenation (.) at ./myprogram.pl"

I expected the program to wait till there was information to the <STDIN> before processing. Does anybody know why the program does not wait for user input?
 
Old 01-29-2009, 12:31 PM   #15
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,012

Rep: Reputation: 367Reputation: 367Reputation: 367Reputation: 367
I hope michaelk can help you with this as I have not used netcat or written perl programs.
It might be a better idea to start a new thread "netcat and my perl program"
 
  


Reply

Tags
file, input, php, telnet


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SHell Script Help! When reading input from a user sfmadmax Linux - Newbie 3 12-12-2007 12:03 AM
input into bash script(reading path) dalicros Linux - General 3 05-30-2007 06:54 PM
dd: reading `/dev/cdrom': Input/output error jimdaworm Linux - Software 46 10-25-2006 08:43 PM
reading from standard input in java spank Programming 3 10-17-2006 06:58 PM
Input/output error when reading from CD-RW drive vrooje Linux - Newbie 2 02-17-2004 11:17 PM


All times are GMT -5. The time now is 07:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration