1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

fixes handle_prompt in shell module (#20982)

* transforms command objects to dict
* transforms string commands to dict
This commit is contained in:
Peter Sprygada 2017-02-02 14:27:55 -05:00 committed by GitHub
parent 4f117df6ec
commit 76ccfe4627

View file

@ -147,7 +147,7 @@ class Shell(object):
window = self.strip(recv.read().decode('utf8')) window = self.strip(recv.read().decode('utf8'))
if cmd: if cmd:
if 'prompt' in cmd and not handled: if cmd.get('prompt') and not handled:
handled = self.handle_prompt(window, cmd) handled = self.handle_prompt(window, cmd)
try: try:
@ -195,13 +195,10 @@ class Shell(object):
self.shell.close() self.shell.close()
def handle_prompt(self, resp, cmd): def handle_prompt(self, resp, cmd):
prompt = to_list(cmd['prompt']) for prompt in to_list(cmd['prompt']):
response = to_list(cmd['response']) match = re.search(prompt, resp)
for pr, ans in zip(prompt, response):
match = pr.search(resp)
if match: if match:
answer = '%s\r' % ans answer = '%s\r' % cmd['response']
self.shell.sendall(answer) self.shell.sendall(answer)
return True return True
@ -246,7 +243,7 @@ class CliBase(object):
username = params['username'] username = params['username']
password = params.get('password') password = params.get('password')
key_file = params.get('ssh_keyfile') key_file = params.get('ssh_keyfile')
timeout = params['timeout'] timeout = params['timeout'] or 10
try: try:
self.shell = Shell( self.shell = Shell(
@ -273,8 +270,19 @@ class CliBase(object):
def authorize(self, params, **kwargs): def authorize(self, params, **kwargs):
pass pass
def to_command(self, obj):
if isinstance(command, Command):
cmdobj = dict()
cmdobj['command'] = obj.command
cmdobj['response'] = obj.response
cmdobj['prompt'] = [p.pattern for p in to_list(obj.prompt)]
return cmdobj
return obj
def execute(self, commands): def execute(self, commands):
try: try:
for index, item in enumerate(commands):
commands[index] = to_command(item)
return self.shell.send(commands) return self.shell.send(commands)
except ShellError: except ShellError:
exc = get_exception() exc = get_exception()
@ -289,12 +297,10 @@ class CliBase(object):
response=dict() response=dict()
)) ))
try: if not isinstance(command, dict):
cmdobj = json.loads(command) command = transform(command)
except ValueError:
cmdobj = transform(command)
rc, out, err = self.shell.send_command(cmdobj) rc, out, err = self.shell.send_command(command)
return rc, out, err return rc, out, err