简体   繁体   English

如何在subprocess.call()之后输入密码

[英]How do I enter a password after subprocess.call()

I am trying to build a python script that I have run every day to fetch that days DNS requests from OpenDNS from the previous 2 days. 我正在尝试构建一个我每天都运行的python脚本来获取前两天来自OpenDNS的DNS请求。 This is what I have so far. 这就是我到目前为止所拥有的。

import subprocess
import datetime
two = datetime.date.today() - datetime.timedelta(2)
one = datetime.date.today() - datetime.timedelta(1)
path = '~/Downloads/opendns-fetchstats-master'
dateRange = str(two) + ' ' + str(one)
dateRange2 = str(two) + 'to' +str(one)
username = 'email@email.com'
password = 'password'
outputFile = dateRange2 + '.csv'
print dateRange2
subprocess.call(
    ['cd ' + str(path) + ' && echo '+ str(password) + ' | bash fetchstats ' +
    str(username) + ' home ' + str(dateRange) + ' > ' + str(outputFile)],
    shell=True
    )

The problem is that after it runs: 问题是它运行后:

bash fetchstats ' + str(username) + ' <network_id> ' + str(dateRange) + ' > ' + str(outputFile)

it then asks for the password to the account. 然后它要求输入帐户的密码。 I can not figure out how to input the password. 我无法弄清楚如何输入密码。 My attempt at using 我尝试使用

echo password |

works partially but the process returns the error below and the process ends before all the data can be downloaded 部分工作但该过程返回下面的错误,并且该过程在所有数据都可以下载之前结束

stty: standard input: Inappropriate ioctl for device stty: standard input: Inappropriate ioctl for device stty:标准输入:设备stty的不适当的ioctl:标准输入:设备的不适当的ioctl

Is there a way I can write this so the process enters the password when prompted and then waits until the download is complete before ending the process? 有没有办法可以写这个,以便进程在提示时输入密码,然后等到下载完成后再结束进程?

You should consider using subprocess.Popen which allows you to control stdin/stdout/stderr 您应该考虑使用subprocess.Popen,它允许您控制stdin / stdout / stderr

There is also this related question 还有这个相关的问题

I found that using: 我发现使用:

echo password

works and there errors can be ignored. 工作和错误可以忽略。 The reason why I thought it was terminating before completion was because the file size was so small. 我认为它在完成之前终止的原因是因为文件大小太小。 After running it manually in the terminal for the same date range the file sizes match and it was due to the small amount of data available for that date range. 在终端中手动运行相同日期范围后,文件大小匹配,这是由于该日期范围可用的少量数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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