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

View File

@ -103,6 +103,10 @@ def run_cmd(cmd, print_output=True):
output = [] output = []
try: try:
child = pexpect.spawn(cmd) 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.maxsize = 1
child.timeout = 86400 child.timeout = 86400
for line in child: for line in child:
@ -110,12 +114,15 @@ def run_cmd(cmd, print_output=True):
output.append(outline) output.append(outline)
if print_output: if print_output:
log.info(outline) log.info(outline)
child.close()
if child.exitstatus != 0:
err_flag = True
except Exception as e: except Exception as e:
err_flag = True err_flag = True
output.append('%s' % e) output.append('%s' % e)
finally:
if child:
child.close()
if child.exitstatus != 0:
err_flag = True
return err_flag, output return err_flag, output