1

i have a telnet-server running here (from a vendor) which is implemented in a barcode reader. to get the data, i simply need to connect to the telnetserver port 23 with my telnetclient from commandline.

What i want to do is doing this with ruby or python and write the output to a file.

so whats work until now: i can connect to the telnet-server.

import sys
import telnetlib

tn = telnetlib.Telnet("10.0.0.138")
tn.close
output = tn.read_all()

# write to a file
with open("logging.txt", "wb") as f:
    f.write(output)

# Check that the file wrote correctly.
with open("logging.txt", "rb") as f:
    print(f.read())

What is not working: writing the output from the telnetserver to a textfile. it doesnt matter for me if i do it with python or ruby, both languages are fine. my codesample is python here. just for trying.

thanks for reading.

Peter Core
  • 193
  • 1
  • 2
  • 16
  • 3
    does the variable named "output" contain any data? did you try printing to stdout first? also, how come there's a call to "tn.close", and after that a "read_all", does not make sense to read a closed connection, you may want to move the "close" call after the "read_all"... – manitu Jul 05 '18 at 14:10
  • 1
    Try this: https://stackoverflow.com/a/7122064/125816 – Sergio Tulentsev Jul 05 '18 at 14:10
  • i added print(output) to the script and removed the tn.close completly. no output on screen. – Peter Core Jul 05 '18 at 15:17
  • Don't you normally need to *send* text in a telnet session to get text back? Even if it's just a bare newline? You can test this on the command line with your `telnet` command. – Keith Bennett Jul 08 '18 at 18:53

0 Answers0