zzzbbx
zzzbbx

Reputation: 10141

How to get input from user when connecting to remote servers? [Python]

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?

Upvotes: 0

Views: 449

Answers (1)

eandersson
eandersson

Reputation: 26352

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 ])

Upvotes: 1

Related Questions