且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过 SSH 将密码作为参数传递给 Python 脚本?

更新时间:2023-11-07 13:56:46

您可以在 SSH 会话期间添加 -t,这将强制执行无回声终端.在您的客户端上:

You can add -t during SSH session, that will enforce an echo free terminal. On your client:

#!/usr/bin/env python

import subprocess

subprocess.call(["ssh", "-t", remote_host, "./add_user.py", "test_user"])

然后您应该能够在您的服务器上使用 getpass 获取远程密码.也可以直接调用或者通过其他脚本调用:

Then you should be able to use getpass on your server for a remote password. You can also call it directly or invoking through another script:

#!/usr/bin/env python

import getpass, argparse

parser = argparse.ArgumentParser()
parser.add_argument("username")
args = parser.parse_args()
username = args.username
password = getpass.getpass()