简体   繁体   中英

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. 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

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

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.

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