class Cli(object): def __init__(self, module): self.module = module self.shell = None def connect(self, **kwargs): host = self.module.params['host'] port = self.module.params['port'] or 22 username = self.module.params['username'] password = self.module.params['password'] key_filename = self.module.params['ssh_keyfile'] try: self.shell = Shell(kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE) self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename) except ShellError: e = get_exception() msg = 'failed to connect to %s:%s - %s' % (host, port, str(e)) self.module.fail_json(msg=msg) def send(self, commands, encoding='text'): try: return self.shell.send(commands) except ShellError: e = get_exception() self.module.fail_json(msg=e.message, commands=commands)
class Cli(object): def __init__(self, module): self.module = module self.shell = None def connect(self, **kwargs): host = self.module.params['host'] port = self.module.params['port'] or 22 username = self.module.params['username'] password = self.module.params['password'] key_filename = self.module.params['ssh_keyfile'] try: self.shell = Shell(kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE) self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename) except ShellError: e = get_exception() msg = 'failed to connect to %s:%s - %s' % (host, port, str(e)) self.module.fail_json(msg=msg) def send(self, commands): try: return self.shell.send(commands) except ShellError: e = get_exception() self.module.fail_json(msg=e.message, commands=commands)
class Cli(object): def __init__(self, module): self.module = module self.shell = None def connect(self, **kwargs): host = self.module.params['host'] port = self.module.params['port'] or 22 username = self.module.params['username'] password = self.module.params['password'] key_filename = self.module.params['ssh_keyfile'] try: self.shell = Shell(prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE) self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename) except ShellError: e = get_exception() msg = 'failed to connect to %s:%s - %s' % (host, port, str(e)) self.module.fail_json(msg=msg) def authorize(self): passwd = self.module.params['auth_pass'] self.send(Command('enable', prompt=NET_PASSWD_RE, response=passwd)) def send(self, commands): try: return self.shell.send(commands) except ShellError: e = get_exception() self.module.fail_json(msg=e.message, commands=commands)
class NetCli(object): """Basic paramiko-based ssh transport any NetworkModule can use.""" def __init__(self): if not HAS_PARAMIKO: raise NetworkError( msg='paramiko is required but does not appear to be installed. ' 'It can be installed using `pip install paramiko`') self.shell = None self._connected = False self.default_output = 'text' def connect(self, params, kickstart=True, **kwargs): host = params['host'] port = params.get('port') or 22 username = params['username'] password = params.get('password') key_file = params.get('ssh_keyfile') timeout = params['timeout'] try: self.shell = Shell( kickstart=kickstart, prompts_re=self.CLI_PROMPTS_RE, errors_re=self.CLI_ERRORS_RE, ) self.shell.open( host, port=port, username=username, password=password, key_filename=key_file, timeout=timeout, ) except ShellError: exc = get_exception() raise NetworkError(msg='failed to connect to %s:%s' % (host, port), exc=str(exc)) self._connected = True def disconnect(self, **kwargs): self.shell.close() self._connected = False def authorize(self, params, **kwargs): passwd = params['auth_pass'] self.execute( Command('enable', prompt=self.NET_PASSWD_RE, response=passwd)) def execute(self, commands, **kwargs): try: return self.shell.send(commands) except ShellError: exc = get_exception() raise NetworkError(exc.message, commands=commands)
class Cli(object): def __init__(self, module): self.module = module self.shell = None def connect(self, **kwargs): host = self.module.params['host'] port = self.module.params['port'] or 22 username = self.module.params['username'] password = self.module.params['password'] key_filename = self.module.params['ssh_keyfile'] allow_agent = (key_filename is not None) or (key_filename is None and password is None) try: self.shell = Shell() self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename, allow_agent=allow_agent) except ShellError: e = get_exception() msg = 'failed to connect to %s:%s - %s' % (host, port, str(e)) self.module.fail_json(msg=msg) if self.shell._matched_prompt.strip().endswith('%'): self.shell.send('cli') self.shell.send('set cli screen-length 0') def run_commands(self, commands, **kwargs): try: return self.shell.send(commands) except ShellError: e = get_exception() self.module.fail_json(msg=e.message, commands=commands) def configure(self, commands, **kwargs): commands = to_list(commands) commands.insert(0, 'configure') if kwargs.get('comment'): commands.append('commit and-quit comment "%s"' % kwargs.get('comment')) else: commands.append('commit and-quit') responses = self.shell.send(commands) responses.pop(0) responses.pop() return responses def disconnect(self): self.shell.close()
class NetCli(object): """Basic paramiko-based ssh transport any NetworkModule can use.""" def __init__(self): if not HAS_PARAMIKO: raise NetworkError( msg='paramiko is required but does not appear to be installed. ' 'It can be installed using `pip install paramiko`' ) self.shell = None self._connected = False self.default_output = 'text' def connect(self, params, kickstart, **kwargs): host = params['host'] port = params.get('port') or 22 username = params['username'] password = params.get('password') key_file = params.get('ssh_keyfile') timeout = params['timeout'] try: self.shell = Shell( kickstart=kickstart, prompts_re=self.CLI_PROMPTS_RE, errors_re=self.CLI_ERRORS_RE, ) self.shell.open( host, port=port, username=username, password=password, key_filename=key_file, timeout=timeout, ) except ShellError: exc = get_exception() raise NetworkError( msg='failed to connect to %s:%s' % (host, port), exc=str(exc) ) def disconnect(self, **kwargs): self._connected = False self.shell.close() def execute(self, commands, **kwargs): try: return self.shell.send(commands) except ShellError: exc = get_exception() raise NetworkError(exc.message, commands=commands)
class Cli(object): def __init__(self, module): self.module = module self.shell = None def connect(self, **kwargs): host = self.module.params['host'] port = self.module.params['port'] or 22 username = self.module.params['username'] password = self.module.params['password'] self.shell = Shell() self.shell.open(host, port=port, username=username, password=password) def send(self, commands, encoding='text'): return self.shell.send(commands)
class Cli(object): def __init__(self, module): self.module = module self.shell = None def connect(self, **kwargs): host = self.module.params['host'] port = self.module.params['port'] or 22 username = self.module.params['username'] password = self.module.params['password'] key_filename = self.module.params['ssh_keyfile'] timeout = self.module.params['timeout'] try: self.shell = Shell(kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE) self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename, timeout=timeout) except ShellError: e = get_exception() msg = 'failed to connect to %s:%s - %s' % (host, port, str(e)) self.module.fail_json(msg=msg) def change_context(self): context = self.module.params['command'] if context == 'system': command = 'changeto system' else: command = 'changeto context %s' % context self.send(Command(command)) def authorize(self): passwd = self.module.params['auth_pass'] self.send(Command('enable', prompt=NET_PASSWD_RE, response=passwd)) def send(self, commands): return self.shell.send(commands)
class Cli(object): def __init__(self, module): self.module = module self.shell = None def connect(self, **kwargs): host = self.module.params['host'] port = self.module.params['port'] or 22 username = self.module.params['username'] password = self.module.params['password'] key_filename = self.module.params['ssh_keyfile'] timeout = self.module.params['timeout'] allow_agent = (key_filename is not None) or (key_filename is None and password is None) try: self.shell = Shell(kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE) self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename, allow_agent=allow_agent, timeout=timeout) except ShellError: e = get_exception() msg = 'failed to connect to %s:%s - %s' % (host, port, str(e)) self.module.fail_json(msg=msg) def authorize(self): passwd = self.module.params['auth_pass'] self.send(Command('enable', prompt=NET_PASSWD_RE, response=passwd)) def send(self, commands): try: return self.shell.send(commands) except ShellError: e = get_exception() self.module.fail_json(msg=e.message, commands=commands)