Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 protected]'
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?

like image 832
Cole Avatar asked May 29 '26 18:05

Cole


1 Answers

You should consider using subprocess.Popen which allows you to control stdin/stdout/stderr

There is also this related question

like image 152
Eric Avatar answered May 31 '26 07:05

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!