Exemplo n.º 1
0
def _to_commands(commands):
    """Converts a command or list of commands to a list of Command objects"""
    commands = to_list(commands)
    _loc = []
    for _cmd in commands:
        if not isinstance(_cmd, Command):
            if re.search("^(!|#)", _cmd) or re.search("^\s*$", _cmd):
                continue
            _cmd = Command(_cmd.strip())
        _loc.append(_cmd)
    return _loc
Exemplo n.º 2
0
    def authorize(self, password, username=None):
        """Authorize the session"""
        command = Command('enable', prompt=self._password_re, answer=password)

        try:
            response = self._send(command)
        except ExecuteFailed as exc:
            raise AuthorizationFailed(str(exc))
Exemplo n.º 3
0
    def connect(self, host, creds, **kwargs):
        """Connect to a host and invoke the shell.  Returns nothing """

        options = kwargs

        timeout = options.get('timeout')
        if timeout:
            self._timeout = timeout

        port = options.get('port') or self._port

        self._ssh = paramiko.SSHClient()
        self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        try:
            self._ssh.connect(host,
                              port,
                              username=creds.username,
                              password=creds.password,
                              timeout=timeout,
                              look_for_keys=False)

        except paramiko.AuthenticationException as exc:
            raise AuthenticationFailed(str(exc))
        except (socket.timeout, SSHException, IOError) as exc:
            raise ConnectFailed(str(exc))

        # we must invoke a shell, otherwise session commands like 'enable',
        # 'terminal width', etc. won't stick
        channel = self._ssh.invoke_shell()
        channel.settimeout(self._timeout)
        self._channel = channel

        # capture login banner and clear any login messages
        self._banner = self._send(Command('\r'))

        # don't die if these commands aren't available
        try:
            self.send(
                [Command('terminal length 0'),
                 Command('terminal dont-ask')])
        except ExecuteFailed:
            pass
Exemplo n.º 4
0
    def connect(self, host, creds, **kwargs):

        transport = kwargs.get('transport', None) or 'http'

        port = kwargs.get('port')

        verify_ssl = kwargs.get('verify', False)

        self._conn = self._transports[transport](host,
                                                 creds,
                                                 port=port,
                                                 verify=verify_ssl)
        #self._conn.connect()

        try:
            # test the connection
            self.send([Command('show version')])
        except ExecuteFailed as exc:
            if '401 Client Error' in exc.message:
                raise AuthenticationFailed(exc.message)
            else:
                raise ConnectFailed(exc.message)
Exemplo n.º 5
0
 def authorize(self, password, username=None):
     self._authorize = Command({"cmd": "enable", "input": password})
Exemplo n.º 6
0
 def authorize(self, password, username):
     self._authorize = Command({'cmd': 'enable', 'input': password})