LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2016, 09:39 AM   #1
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Rep: Reputation: Disabled
Python Telent not returning data


Hi All,

So i have a python script in which i wish to telnet locally to a port and print the data to start with.

Im opening the port however the print doesnt seem to work and i can see im buffering the input

Code:
import sys
import telnetlib

tn = telnetlib.Telnet("127.0.0.1",17051)
data = tn.read_all()
print "Data: " + data
Any idea why its not processing the data and then printing ?
 
Old 11-03-2016, 10:30 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Code:
import sys
import telnetlib
tn=telnetlib.Telnet("towel.blinkenlights.nl",23)
data=tn.read_until("Simon")
print "Data: " + data
The difference between read_all and read_until:

Telnet.read_until(expected[, timeout])
Read until a given string, expected, is encountered or until timeout seconds have passed.
When no match is found, return whatever is available instead, possibly the empty string. Raise EOFError if the connection is closed and no cooked data is available.

Telnet.read_all()
Read all data until EOF; block until connection closed.
 
Old 11-04-2016, 03:17 AM   #3
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Code:
import sys
import telnetlib
tn=telnetlib.Telnet("towel.blinkenlights.nl",23)
data=tn.read_until("Simon")
print "Data: " + data
The difference between read_all and read_until:

Telnet.read_until(expected[, timeout])
Read until a given string, expected, is encountered or until timeout seconds have passed.
When no match is found, return whatever is available instead, possibly the empty string. Raise EOFError if the connection is closed and no cooked data is available.

Telnet.read_all()
Read all data until EOF; block until connection closed.
Hey,

I want it to read whatever is being sent so thought the read_all would be best.

i tried the read_until as a test however didnt work.

$ python telnetTest2.py
Traceback (most recent call last):
File "telnetTest2.py", line 9, in ?
data = tn.read_until("Simon")
File "/usr/lib64/python2.4/telnetlib.py", line 317, in read_until
self.process_rawq()
File "/usr/lib64/python2.4/telnetlib.py", line 488, in process_rawq
self.cookedq = self.cookedq + buf[0]
KeyboardInterrupt

^^^^ i killed once it didnt output.

If i do a debug level i see what it should be doing

Telnet(127.0.0.1,17051): recv '\x01|\x00\x00\x00\x10Pp\x11\xc2\x8b\x91\x01\x96\x00\x00\x01\x01\x01\x00\x01|\x01\xf5\x00\x10Pp\x1b\ x86s\x8d\x00j\x00\x07\x002\x00\x8c\x00\x10\x06\x92\x00z\xfe\xbc\x01\xc6'

safe to say im pretty confused lol
 
Old 11-04-2016, 08:07 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Code:
tn=telnetlib.Telnet("towel.blinkenlights.nl",23)
This line is telling python to telnet to the site towel.blinkenlights.nl port 23 as a test. It shows a telnet version of star wars written by some guys, one of whom is named 'Simon'

Code:
data=tn.read_until("Simon")
This line says put all data from telnet stream into 'data' variable up to the word 'Simon'

If you ran the program on your telnet destination - and you do not have the word 'Simon' in the output - then it would never stop reading. So take my EXAMPLE and modify it to work for your setup.
 
Old 11-04-2016, 08:18 AM   #5
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Code:
tn=telnetlib.Telnet("towel.blinkenlights.nl",23)
This line is telling python to telnet to the site towel.blinkenlights.nl port 23 as a test. It shows a telnet version of star wars written by some guys, one of whom is named 'Simon'

Code:
data=tn.read_until("Simon")
This line says put all data from telnet stream into 'data' variable up to the word 'Simon'

If you ran the program on your telnet destination - and you do not have the word 'Simon' in the output - then it would never stop reading. So take my EXAMPLE and modify it to work for your setup.
Ahhh yes perfect i have it outputting what i want when i use the read_until

only problem i have is from what i understand read_all will read the output regardless, however if i change it to read all i dont output anything
 
Old 11-04-2016, 02:38 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Again:

Telnet.read_all()
Read all data until EOF; block until connection closed.

"read_all" will continue reading FOREVER until either an EOF(end of file marker) comes through telnet or the telnet connection closes. Your telnet connection doesn't appear to do either, staying open forever, so "read_all" will continue reading FOREVER without ever going to the next line "print data."
 
Old 11-07-2016, 04:41 AM   #7
MrLinuxDonnelly
Member
 
Registered: May 2015
Location: London, United Kingdom
Distribution: Redhat 6.5, Centos, Fedora
Posts: 33

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Again:

Telnet.read_all()
Read all data until EOF; block until connection closed.

"read_all" will continue reading FOREVER until either an EOF(end of file marker) comes through telnet or the telnet connection closes. Your telnet connection doesn't appear to do either, staying open forever, so "read_all" will continue reading FOREVER without ever going to the next line "print data."
Sorry read it wrong, was having a friday moment.

Thanks so much for your help
 
  


Reply



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
[SOLVED] CDC-ACM driver returning garbage data streamholder Linux - Hardware 4 08-25-2016 03:15 AM
[SOLVED] Windows/Visual Studio -- Returning data from a DLL dwhitney67 Programming 10 05-08-2015 10:55 AM
[SOLVED] read function call returning more amount of data than wiritten using write function chakka.lokesh Programming 1 09-11-2014 03:14 AM
Python - How to get text file data into an array on python. golmschenk Programming 4 11-11-2013 09:15 AM
python: return not returning bulliver Programming 1 01-09-2003 06:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration