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?
Bob
  • 10,741
  • 27
  • 89
  • 143
  • 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. – merlin2011 Aug 06 '14 at 20:22

1 Answers1

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 ])
eandersson
  • 25,781
  • 8
  • 89
  • 110