I'm trying to get the output of a subprocess to prompt for a login then continue with the rest of the script once logged in. I can get it to prompt for the login, but after entering my username and password I get the following error:
Traceback (most recent call last):
File "./login.py", line 20, in <module>
login()
File "./login.py", line 13, in login
output = login_cmd.stderr.read(1)
ValueError: read of closed file.
Below is the script:
import subprocess, sys
raidcom = '/usr/bin/raidcom'
def login(cmd=raidcom):
login_cmd = subprocess.Popen('{cmd} -login'.format(cmd=cmd),
stderr=subprocess.PIPE, shell=True)
(output, err) = login_cmd.communicate()
while True:
output = login_cmd.stderr.read(1)
if output == '' and login_cmd.poll() != None:
break
if output != '':
sys.stdout.write(output)
sys.stdout.flush()
login()
When I normally execute the command from the shell it returns nothing after logging in.
I found another way around this situation. Instead of passing the real-time output I prompted for the the user's username and password information and inserted that information into the command. The below function works:
import subprocess
import getpass
raidcom = '/usr/bin/raidcom'
def raidcom_login(cmd=raidcom):
"""Login to CCI and return its output."""
horcm_user = input("Please enter your username for logging into CCI: ")
horcm_pass = getpass.getpass("Please enter your password: ")
try:
login_cmd = subprocess.check_output('{cmd} -login {} {}'.format(horcm_user, horcm_pass, cmd=cmd),
stderr=subprocess.PIPE, shell=True)
except subprocess.CalledProcessError:
print('Login failed')
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.