Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:26:13+00:00 2026-06-17T21:26:13+00:00

I have been going crazy trying to make this work. I want to collect

  • 0

I have been going crazy trying to make this work. I want to collect a telnet log from python. On plain telnet I would just open telnet and use:

set logfile test.xml

The problem is that with python I cannot figure out how to pass this command first. when I run this

try:
    tn = telnetlib.Telnet()
    tn.write(b"set logfile test.xml" + b"\r")
    tn.open(IP, 223, 8192)
except socket.timeout:
    print ("socket timeout")
    sulk()

I get this error:

Traceback (most recent call last):
  File "C:xxxxxxxx.py", line 18, in <module>
    tn.write(b"set logfile test.xml" + b"\r")
  File "C:\Python33\lib\telnetlib.py", line 282, in write
    self.sock.sendall(buffer)
AttributeError: 'NoneType' object has no attribute 'sendall'

The script runs fine if I open the IP/port without setting the log file. And to solve the issue I write to a file using python. but for some reason this takes a very long time 5+ minutes. If I just run the commands through telnet it takes under 10 seconds…

telnet
set logfile test.xml
o xxx.xxx.xxx.xxx xxx
>>commandx
>>commandy

All the data I need is here and in the log file 

So is there anything I can do to set the logfile before opening the IP/port?
Thanks in Advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:26:14+00:00Added an answer on June 17, 2026 at 9:26 pm

    You’re going about this backward. I assume you’re trying to log it to a file so that you can then read it into your Python script and do something with it. What you should be doing is capturing the the output from the telnet session in a variable. Then you can process it further, even write it to a file if you want to, and so on.

    import telnetlib
    
    host    = "host.example.com"
    port    = 223
    timeout = 9999
    
    try:
        session = telnetlib.Telnet(host, port, timeout)
    except socket.timeout:
        print ("socket timeout")
    else:
        session.write("command1\n")
        session.write("command2\n")
        output = session.read_all()
        # output now contains the text you want
        # so now you can do something with it:
        with open("test.xml", "w") as logfile:
            logfile.write(output)
    

    If the output could be large, you can read it in chunks:

    import time, telnetlib
    
    host    = "host.example.com"
    port    = 223
    timeout = 9999
    
    try:
        session = telnetlib.Telnet(host, port, timeout)
    except socket.timeout:
        print ("socket timeout")
    else:
        session.write("command1\n")
        session.write("command2\n")
        with open("test.xml", "w") as logfile:
            output = session.read_some()
            while output:
                logfile.write(output)
                time.sleep(0.1)  # let the buffer fill up a bit
                output = session.read_some()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been going crazy over a seemingly easy question with Python: I want
I am going crazy trying to figure this out. I am trying to make
I have been going crazy trying to get the selected option to align left
I have been going crazy trying to figure out how to query a php
Ok, I'm going crazy. I've been trying to make a simple .net assembly visible
I have been going crazy over this! I have an Air (2.6) app which,
I've been going crazy trying to figure out why this is happening. The problem
I have been going crazy on this program im writing. My decks dont seem
I am going crazy with this search bar in storyboards and have been trough
I'm literately going crazy with this one. I have been searching the answer and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.