从命令行运行程序,提示输入密码并自动为其提供密码(cmd.exe,python) [英] Run program from command line what prompts password and automatically provide password for it (cmd.exe, python)

查看:1045
本文介绍了从命令行运行程序,提示输入密码并自动为其提供密码(cmd.exe,python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有命令行程序,提示输入密码:

I have command line program what prompts password:

> cwrsync root@NN.NN.NN.NN:/src /cygdrive/c/dst

输出(当我从cmd.exe命令行运行它时):

Output (when i run it from cmd.exe command line):

root@NN.NN.NN.NN's password:

当我手动输入密码时,一切正常。输出:

When i input password manually, all OK. Output:

skipping directory src

我想通过命令行或python脚本自动提供密码。

I want to provide password for it from command line or python script automatically.

我尝试过:

一个。从命令行:

> echo pass|cwrsync -r root@NN.NN.NN.NN:/src /cygdrive/c/dst

不起作用。输出:

root@NN.NN.NN.NN's password:

两个。从python脚本开始。 test.py:

Two. From python script. test.py:

import subprocess
cmd = "cwrsync -r root@NN.NN.NN.NN:/src /cygdrive/c/dst"
proc = subprocess.Popen(cmd1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
std1, std2 = proc.communicate("pass")
print std1print std2

不起作用。输出:

Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(235) [Receiver=3.1.1]


推荐答案

面向安全的程序通常在Direct io上要求输入密码而不是在io上输入密码阅读标准输入。和:

It is common that security oriented programs ask for password on direct io instead of reading stdin. And as :

echo pass|cwrsync -r root@NN.NN.NN.NN:/src /cygdrive/c/dst

需要输入密码,我认为csrsync直接从控制台读取。

did ask password, I presume that csrsync directly reads from console.

在这种情况下,如果不进行一些工作和低级编程,就无法使其自动化,因为您将不得不模拟键盘操作。相反,您应该搜索文档,因为看起来像使用基础 ssh 一样,它很可能接受公钥对。如果它接受一个没有密码短语的密码,那么您应该可以使其自动化。

In that case you cannot automate it without some work and low level programming, because you will have to simulate keyboard actions. You should instead search the documentations, because as it looks like it uses an underlying ssh, it is likely to accept a public key pair. If it accept one without passphrase, you should be able to automate it.

这篇关于从命令行运行程序,提示输入密码并自动为其提供密码(cmd.exe,python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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