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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:16:58+00:00 2026-05-20T14:16:58+00:00

I want to write a Python script that runs another program, reading the output

  • 0

I want to write a Python script that runs another program, reading the output of the other program and manipulating it. The problem is that this program prompts for a password, and I cannot figure out how to supply it automatically. (For the purposes of this script, it really does not matter if the password is stored in plain-text in the script itself.) What I want to do is something like:

os.system('echo someinput | /var/local/bin/someprogram') 

Which results in someprogram giving me the unwanted password prompt, and also doesn’t give me the program’s output as the return value. Tragically, the program does not have a way to bypass this prompt.

Unfortunately, I also have some restrictions as to how I can go about solving this problem. First, I’m stuck with Python 2.3 (so I cannot use the subprocess module). Second, I cannot install any new modules, (so no pexpect). Fortunately, it doesn’t have to be particularly portable, so a Linux-only solution is fine.

I’ve been trying to figure out the pty module, since it looks like it offers what I need, but after spending hours wrestling with it, I just cannot figure out how to get it to work the way I need it to.

  • 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-20T14:16:59+00:00Added an answer on May 20, 2026 at 2:16 pm

    I had some similar problems with terminal-based interprocess communication that didn’t seem to be solvable using popen (et al.). I ended up learning how to use pty by reading the source of pexpect, which contains examples of how (and comments of why) to get pty to jump through the necessary hoops.

    Depending on your needs, of course, you could also just use pexpect!

    Here’s the meat of what I used in my own project. Note that I’m not checking to see whether the child process terminates; the script was intended to run as a daemon managing a long-running Java process, so I never had to deal with status codes. Hopefully, this will get you most of what you need, however.

    import os
    import pty
    import select
    import termios
    
    child_pid, child_fd = pty.fork()
    
    if not child_pid: # child process
        os.execv("/path/to/command", ["command", "arg1", "arg2"])
    
    # disable echo
    attr = termios.tcgetattr(child_fd)
    attr[3] = attr[3] & ~termios.ECHO
    termios.tcsetattr(child_fd, termios.TCSANOW, attr)
    
    while True:
        # check whether child terminal has output to read
        ready, _, _ = select.select([child_fd], [], [])
    
        if child_fd in ready:
            output = []
    
            try:
                while True:
                    s = os.read(child_fd, 1)
    
                    # EOF or EOL
                    if not s or s == "\n":
                        break
    
                    # don't store carriage returns (no universal line endings)
                    if not s == "\r":
                        output.append(s)
            except OSError: # this signals EOF on some platforms
                pass
    
            if output.find("Enter password:") > -1:
                os.write(child_fd, "password")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a python script that populates a database with some information.
I want to write a function in Python that returns different fixed values based
Greetings, I want to write a script that handles simple http requests from Google
I am trying to write a Python script that finds combinations of armor items
Just as the title says. I want to write a script that behaves differently
I wonder if you can help. I want to write a script in python
I have a Python list, say l = [1,5,8] I want to write a
I want to write a command that specifies the word under the cursor in
I want to write a function that takes an array of letters as an
I want to write a word addin that does some computations and updates some

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.