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 6791323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:49:56+00:00 2026-05-26T17:49:56+00:00

I am attempting to use Popen to automate a simple telnet session. In python

  • 0

I am attempting to use Popen to automate a simple telnet session. In python 2.6.5 the following code works:

openCmd = subprocess.Popen("telnet 192.168.1.1", shell=True, stdout=PIPE, stdin=PIPE)
time.sleep(1)
openCmd.stdin.write("username\r")
time.sleep(1)
openCmd.stdin.write("password\r")
time.sleep(1)
openCmd.stdin.write("some command\r")
openCmd.terminate()

In python 3 it complained of a type error, so I figured I just had to add .encode() to the end of each str object (as shown below). Adding the .encode() did fix the type error, and I don’t get any exceptions, but the command I am trying to run on the remote machine doesn’t get run.

openCmd = subprocess.Popen("telnet 192.168.1.1", shell=True, stdout=PIPE, stdin=PIPE)
time.sleep(1)
openCmd.stdin.write("username\r".encode())
time.sleep(1)
openCmd.stdin.write("password\r".encode())
time.sleep(1)
openCmd.stdin.write("some command\r".encode())
openCmd.terminate()

I also tried .encode(“ascii”) and .encode(“UTF-8”). What am I doing incorrectly? I figure the issue is with the encoding, but I do not know for sure… I am running this program on a machine running Ubuntu 10.04.

  • 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-05-26T17:49:57+00:00Added an answer on May 26, 2026 at 5:49 pm

    Apparently all I needed to do was sleep on this one. It turns out that in Python 2.6.5 every

    Popen.stdin.write()
    

    Had a shorter delay before the buffer was flushed than Python 3 did! Here is the final working program:

    def writeImmeadiatelyToPopen(openCmd, textToWrite):
        openCmd.write(textToWrite.encode())
        openCmd.flush()
    
    openCmd = Popen("telnet 192.168.1.1", shell=True, stdout=PIPE, stdin=PIPE)
    time.sleep(1)
    writeImmeadiatelyToPopen(openCmd, "username\n")
    time.sleep(1)
    writeImmeadiatelyToPopen(openCmd, "password\n")
    time.sleep(1)
    writeImmeadiatelyToPopen(openCmd, "some command\n")
    openCmd.terminate()
    

    In case anyone wondered, I figured out how it worked by running Popen against ‘cat’ and carefully watching the output in Python 2.6.5 and Python 3 ^_^ .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use the following code to serialize an anonymous type to JSON:
Im attempting to use a C++ extension for Python called PySndObj. and getting an
Im attempting to use Silverlight and MVC together. After creating a simple Silverlight application
I'm attempting to use MIB files in PySNMP. The code is fairly straightforward. Nothing
I'm attempting to use Python to convert a multi-page PDF into a series of
I am attempting to use the hatching feature in matplotlib, which works fine when
I am attempting to use coffeescript/jQuery to do the following: 1) Retrieve all the
I'm attempting to use Python's logging module to send emails containing logs. The problem
I am attempting to use the MediaStore.ACTION_VIDEO_CAPTURE in my app, see code below. However,
I'm attempting to use JUnit to test some database code. I'm pretty new to

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.