From b360278f8218205b70c48d21368f4fd7b46a6707 Mon Sep 17 00:00:00 2001 From: Steve Noyes Date: Thu, 15 Oct 2015 17:47:41 -0400 Subject: [PATCH] raise exception if password prompt encountered by run cmd --- kollacli/utils.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/kollacli/utils.py b/kollacli/utils.py index 76d99fb..479bbe0 100644 --- a/kollacli/utils.py +++ b/kollacli/utils.py @@ -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