连接到远程服务器时如何从用户获取输入?[Python] [英] How to get input from user when connecting to remote servers? [Python]

查看:31
本文介绍了连接到远程服务器时如何从用户获取输入?[Python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从终端使用(非 Python)脚本连接到远程服务器.

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

$./myscript <parameters>

通常,我需要输入密码.我有以下问题,假设 python 脚本将运行 myscript:

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

  1. 我如何从用户那里获取密码
  2. 如何将其输入 myscript?

推荐答案

如果我理解正确,你可能会使用 getpass 函数.

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.

如果您只是想将参数传递给您的应用程序,您可以使用 sys.argv.

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]

如果您需要将密码传递给 Python 执行的脚本,您可以使用子进程调用.

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

这篇关于连接到远程服务器时如何从用户获取输入?[Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆