Run program from command line what prompts passwor

2019-08-08 17:54发布

I have command line program what prompts password:

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

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

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

I tried:

One. From command line:

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

Not working. Output:

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

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

Not workin. Output:

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]

2条回答
Ridiculous、
2楼-- · 2019-08-08 18:21

Try sending a newline in your stdin string communicate call like so:

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

You should also see if it works with shell=False (from subprocess docs):

Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.

查看更多
放荡不羁爱自由
3楼-- · 2019-08-08 18:23

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

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

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.

查看更多
登录 后发表回答