0

I need to connect to a remote server using a (non-python) script from terminal.

$./myscript <parameters>

Normally, I would need to enter the password. I have the following question, assuming a python script will run myscript:

  1. How do I get the password from the user
  2. How do I feed it into myscript?
1
  • 1
    If the script is running locally, then you can get the password from the user the same way you would for any local program. Commented Aug 6, 2014 at 20:22

1 Answer 1

1

If I understand the question correctly you would probably use the getpass function.

import getpass
password = getpass.getpass()
print 'You entered:', password

The major advantage is that the password will not be visible on the screen as the user enters it.

If you simply want to pass in arguments to your application you can use sys.argv.

import sys

if len(sys.argv) > 1:
    print "First argument:", sys.argv[1]

If you need to pass on a password to a script executed by Python you can use subprocess call.

import getpass
import subprocess

password = getpass.getpass()
subprocess.call(["myscript", password ])
Sign up to request clarification or add additional context in comments.

7 Comments

will there be a conflict with the command python is running? and how do I feed it back to the script run underneath?
Not sure if I understood your problem. Could you expand on the questions?
no, python is calling "myscript" which requires password
@Bob I updated my answer, was it something like that you were looking for?
@Bob I tested it with a realm simple example and it worked fine. Can you be more specific on what isn't working? Is it not getting the password?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.