def cli(self, commands):
cli_output = {}<EOL>def _count(txt, none):  <EOL><INDENT>"""<STR_LIT>"""<EOL>count = len(txt.splitlines())<EOL>return "<STR_LIT>".format(count=count)<EOL><DEDENT>def _trim(txt, length):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>newlines = []<EOL>for line in txt.splitlines():<EOL><INDENT>newlines.append(line[int(length) :])<EOL><DEDENT>return "<STR_LIT:\n>".join(newlines)<EOL><DEDENT>except ValueError:<EOL><INDENT>return txt<EOL><DEDENT><DEDENT>def _except(txt, pattern):<EOL><INDENT>"""<STR_LIT>"""<EOL>rgx = "<STR_LIT>".format(pattern=pattern)<EOL>unmatched = [<EOL>line for line in txt.splitlines() if not re.search(rgx, line, re.I)<EOL>]<EOL>return "<STR_LIT:\n>".join(unmatched)<EOL><DEDENT>def _last(txt, length):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>return "<STR_LIT:\n>".join(txt.splitlines()[(-<NUM_LIT:1>) * int(length) :])<EOL><DEDENT>except ValueError:<EOL><INDENT>return txt<EOL><DEDENT><DEDENT>def _match(txt, pattern):<EOL><INDENT>"""<STR_LIT>"""<EOL>rgx = "<STR_LIT>".format(pattern=pattern)<EOL>matched = [line for line in txt.splitlines() if re.search(rgx, line, re.I)]<EOL>return "<STR_LIT:\n>".join(matched)<EOL><DEDENT>def _find(txt, pattern):<EOL><INDENT>"""<STR_LIT>"""<EOL>rgx = "<STR_LIT>".format(pattern=pattern)<EOL>match = re.search(rgx, txt, re.I | re.M | re.DOTALL)<EOL>if match:<EOL><INDENT>return "<STR_LIT>".format(pattern=pattern, rest=match.group(<NUM_LIT:2>))<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT>"<EOL><DEDENT><DEDENT>def _process_pipe(cmd, txt):<EOL><INDENT>"""<STR_LIT>"""<EOL>if txt is None:<EOL><INDENT>return txt<EOL><DEDENT>_OF_MAP = OrderedDict()<EOL>_OF_MAP["<STR_LIT>"] = _except<EOL>_OF_MAP["<STR_LIT>"] = _match<EOL>_OF_MAP["<STR_LIT>"] = _last<EOL>_OF_MAP["<STR_LIT>"] = _trim<EOL>_OF_MAP["<STR_LIT:count>"] = _count<EOL>_OF_MAP["<STR_LIT>"] = _find<EOL>exploded_cmd = cmd.split("<STR_LIT:|>")<EOL>pipe_oper_args = {}<EOL>for pipe in exploded_cmd[<NUM_LIT:1>:]:<EOL><INDENT>exploded_pipe = pipe.split()<EOL>pipe_oper = exploded_pipe[<NUM_LIT:0>]  <EOL>pipe_args = "<STR_LIT>".join(exploded_pipe[<NUM_LIT:1>:<NUM_LIT:2>])<EOL>pipe_oper_args[pipe_oper] = pipe_args<EOL><DEDENT>for oper in _OF_MAP.keys():<EOL><INDENT>if oper not in pipe_oper_args.keys():<EOL><INDENT>continue<EOL><DEDENT>txt = _OF_MAP[oper](txt, pipe_oper_args[oper])<EOL><DEDENT>return txt<EOL><DEDENT>if not isinstance(commands, list):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>_PIPE_BLACKLIST = ["<STR_LIT>"]<EOL>for command in commands:<EOL><INDENT>exploded_cmd = command.split("<STR_LIT:|>")<EOL>command_safe_parts = []<EOL>for pipe in exploded_cmd[<NUM_LIT:1>:]:<EOL><INDENT>exploded_pipe = pipe.split()<EOL>pipe_oper = exploded_pipe[<NUM_LIT:0>]  <EOL>if pipe_oper in _PIPE_BLACKLIST:<EOL><INDENT>continue<EOL><DEDENT>pipe_args = "<STR_LIT>".join(exploded_pipe[<NUM_LIT:1>:<NUM_LIT:2>])<EOL>safe_pipe = (<EOL>pipe_oper<EOL>if not pipe_args<EOL>else "<STR_LIT>".format(fun=pipe_oper, args=pipe_args)<EOL>)<EOL>command_safe_parts.append(safe_pipe)<EOL><DEDENT>safe_command = (<EOL>exploded_cmd[<NUM_LIT:0>]<EOL>if not command_safe_parts<EOL>else "<STR_LIT>".format(<EOL>base=exploded_cmd[<NUM_LIT:0>], pipes="<STR_LIT>".join(command_safe_parts)<EOL>)<EOL>)<EOL>raw_txt = self.device.cli(safe_command, warning=False)<EOL>cli_output[py23_compat.text_type(command)] = py23_compat.text_type(<EOL>_process_pipe(command, raw_txt)<EOL>)<EOL><DEDENT>return cli_output<EOL>
Execute raw CLI commands and returns their output.