简体   繁体   中英

Run program from command line what prompts password and automatically provide password for it (cmd.exe, python)

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]

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM