raise exception if password prompt encountered by run cmd

This commit is contained in:
Steve Noyes 2015-10-15 17:47:41 -04:00
parent 5b2784c15d
commit b360278f82
1 changed files with 10 additions and 3 deletions

View File

@ -103,6 +103,10 @@ def run_cmd(cmd, print_output=True):
output = []
try:
child = pexpect.spawn(cmd)
index = child.expect([pexpect.EOF, '[sudo] password'])
if index == 1:
raise Exception(
'Insufficient permissions to run command "%s"' % cmd)
child.maxsize = 1
child.timeout = 86400
for line in child:
@ -110,12 +114,15 @@ def run_cmd(cmd, print_output=True):
output.append(outline)
if print_output:
log.info(outline)
child.close()
if child.exitstatus != 0:
err_flag = True
except Exception as e:
err_flag = True
output.append('%s' % e)
finally:
if child:
child.close()
if child.exitstatus != 0:
err_flag = True
return err_flag, output