示例#1
0
def background(endpoints, commands, **kwargs):
    """Similar to batch, but the :class:`Pool` opject is returned before
    reading the results, non-blocking

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'
    :return: :class:`Pool <Pool>` object
    :rtype: arcomm.async.Pool

    Usage:
        >>> with arcomm.background('veos1', 'show version') as bg:
        ...     # do other things...
        ...
        >>> for res in bg:
        ...     print(res.to_yaml())
        ...
        host: vswitch1
        status: ok
        commands:
          - command: show version
            output: |
              Arista vEOS
              Hardware version:
              Serial number:
              System MAC address:  0800.2776.48c5
              [ ...output omitted... ]
    """

    endpoints = to_list(endpoints)
    return Pool(endpoints, commands, **kwargs)
示例#2
0
文件: async.py 项目: vatsan62/arcomm
    def __init__(self, hosts, commands, **kwargs):

        #
        self._hosts = to_list(hosts)

        #
        self._commands = commands

        #
        self._pool_size = kwargs.pop('pool_size', None) or 10


        #
        self.background = kwargs.pop('background', False)

        #
        self._worker_kwargs = kwargs

        #
        self._results = IterQueue()

        #
        #self._async_result = None

        # initialize pool
        self._pool = multiprocessing.Pool(self._pool_size, _initialize_worker)
示例#3
0
def background(endpoints, commands, **kwargs):
    """Similar to batch, but the :class:`Pool` opject is returned before
    reading the results, non-blocking

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'
    :return: :class:`Pool <Pool>` object
    :rtype: arcomm.async.Pool

    Usage:
        >>> with arcomm.background('veos1', 'show version') as bg:
        ...     # do other things...
        ...
        >>> for res in bg:
        ...     print(res.to_yaml())
        ...
        host: vswitch1
        status: ok
        commands:
          - command: show version
            output: |
              Arista vEOS
              Hardware version:
              Serial number:
              System MAC address:  0800.2776.48c5
              [ ...output omitted... ]
    """

    endpoints = to_list(endpoints)
    return Pool(endpoints, commands, **kwargs)
示例#4
0
文件: api.py 项目: vatsan62/arcomm
def configure(uri, commands,  **kwargs):

    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")

    execute(uri, commands,  **kwargs)
示例#5
0
    def __init__(self, cmd, prompt=None, answer=None):
        if isinstance(cmd, dict):
            prompt = cmd.get("prompt")
            answer = cmd.get("answer", cmd.get('input', ''))
            cmd = cmd.get("cmd", cmd.get('command'))
        assert isinstance(cmd, basestring), "cmd must be a string"

        self._store = dict()
        self.update(dict(cmd=cmd, prompt=to_list(prompt), answer=answer))
示例#6
0
    def __init__(self, cmd, prompt=None, answer=None):
        if isinstance(cmd, dict):
            prompt = cmd.get("prompt")
            answer = cmd.get("answer", cmd.get('input', ''))
            cmd = cmd.get("cmd", cmd.get('command'))
        assert isinstance(cmd, basestring), "cmd must be a string"

        self._store = dict()
        self.update(dict(cmd=cmd, prompt=to_list(prompt), answer=answer))
示例#7
0
文件: api.py 项目: vatsan62/arcomm
def batch(endpoints, commands, **kwargs):
    endpoints = to_list(endpoints)

    #pool_size = kwargs.pop('pool_size', None) or 10
    delay = kwargs.pop('delay', None) or 0

    pool = Pool(endpoints, commands, **kwargs)
    pool.run(delay=delay)

    for item in pool.results:
        yield item
示例#8
0
def commands_from_list(commands):
    """Converts a command or list of commands to a list of Command objects"""
    commands = to_list(commands)
    cmdlist = []
    for cmd in commands:
        if not isinstance(cmd, Command):
            if re.search("^(!|#)", cmd) or re.search("^\s*$", cmd):
                continue
            cmd = Command(cmd.strip())
        cmdlist.append(cmd)
    return cmdlist
示例#9
0
def commands_from_list(commands):
    """Converts a command or list of commands to a list of Command objects"""
    commands = to_list(commands)
    cmdlist = []
    for cmd in commands:
        if not isinstance(cmd, Command):
            if re.search("^(!|#)", cmd) or re.search("^\s*$", cmd):
                continue
            cmd = Command(cmd.strip())
        cmdlist.append(cmd)
    return cmdlist
示例#10
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
示例#11
0
    def _handle_input(self, response, prompt, answer):
        """look for interactive prompts and send answer"""
        if prompt is None or answer is None:
            return

        re_type = type(re.compile(r'^$'))

        prompt = to_list(prompt)
        answer = to_list(answer)

        if len(prompt) != len(answer):
            raise ValueError(("Lists of prompts and answers have different "
                              "lengths"))

        for _prompt, _answer in zip(prompt, answer):
            if not isinstance(_prompt, re_type):
                _prompt = re.compile(_prompt)

            match = _prompt.search(response)
            if match:
                self._channel.send(_answer + '\r')
示例#12
0
    def _handle_input(self, response, prompt, answer):
        """look for interactive prompts and send answer"""
        if prompt is None or answer is None:
            return

        re_type = type(re.compile(r'^$'))

        prompt = to_list(prompt)
        answer = to_list(answer)

        if len(prompt) != len(answer):
            raise ValueError(("Lists of prompts and answers have different "
                              "lengths"))

        for _prompt, _answer in zip(prompt, answer):
            if not isinstance(_prompt, re_type):
                _prompt = re.compile(_prompt)

            match = _prompt.search(response)
            if match:
                self._channel.send(_answer + '\r')
示例#13
0
def configure(endpoint, commands, **kwargs):
    """Make configuration changes to the switch

    :param endpoint: remote host or URI to connect to
    :param commands: configuration mode command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> arcomm.configure('eapi://veos',
        ...                  ['interface Ethernet1', 'no shutdown'])
        <ResponseStore [ok]>
    """
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")

    execute(endpoint, commands, **kwargs)
示例#14
0
def configure(endpoint, commands,  **kwargs):
    """Make configuration changes to the switch

    :param endpoint: remote host or URI to connect to
    :param commands: configuration mode command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> arcomm.configure('eapi://veos',
        ...                  ['interface Ethernet1', 'no shutdown'])
        <ResponseStore [ok]>
    """
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")

    execute(endpoint, commands,  **kwargs)
示例#15
0
    def __init__(self, endpoints, commands, size=10, delay=0, **kwargs):

        #
        self._endpoints = to_list(endpoints)

        #
        self._commands = commands

        #
        self._pool_size = size

        #
        self._delay = delay

        #
        self._worker_kwargs = kwargs

        #
        self._results = []

        # initialize pool
        self._pool = multiprocessing.Pool(self._pool_size, _prep_worker)
示例#16
0
def batch(endpoints, commands, **kwargs):
    """Send commands to multiple endpoints

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> pool = arcomm.batch(['veos1', 'veos2'], ['show version'])
        >>> for res in pool:
        ...     print(res.to_yaml())
    """

    endpoints = to_list(endpoints)
    with Pool(endpoints, commands, **kwargs) as pool:
        try:
            for item in pool.results:
                yield item.get()
        except KeyboardInterrupt:
            print('Caught interrupt')
            pool.kill()
            raise
示例#17
0
def batch(endpoints, commands, **kwargs):
    """Send commands to multiple endpoints

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> pool = arcomm.batch(['veos1', 'veos2'], ['show version'])
        >>> for res in pool:
        ...     print(res.to_yaml())
    """

    endpoints = to_list(endpoints)
    with Pool(endpoints, commands, **kwargs) as pool:
        try:
            for item in pool.results:
                yield item.get()
        except KeyboardInterrupt:
            print('Caught interrupt')
            pool.kill()
            raise
示例#18
0
def unsubscribe(callback):
    callback = to_list(callback)
    global _SUBSCRIBERS
    _SUBSCRIBERS = [cb for cb in _SUBSCRIBERS if cb not in callback]
示例#19
0
 def errors(self):
     return to_list(self.error)
示例#20
0
def configure(connection, commands, *args, **kwargs):
    """Similar to execute, but wraps the commands in a configure/end block"""
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")
    return connection.send(commands, **kwargs)
示例#21
0
def configure(connection, commands, *args, **kwargs):
    """Similar to execute, but wraps the commands in a configure/end block"""
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")
    return connection.send(commands, **kwargs)
示例#22
0
def unsubscribe(callback):
    callback = to_list(callback)
    global _SUBSCRIBERS
    _SUBSCRIBERS = [cb for cb in _SUBSCRIBERS if cb not in callback]