Ejemplo n.º 1
0
def get_hosts_from_file(filename,
                        default_protocol='telnet',
                        default_domain='',
                        remove_duplicates=False,
                        encoding='utf-8'):
    """
    Reads a list of hostnames from the file with the given name.

    @type  filename: string
    @param filename: A full filename.
    @type  default_protocol: str
    @param default_protocol: Passed to the Host constructor.
    @type  default_domain: str
    @param default_domain: Appended to each hostname that has no domain.
    @type  remove_duplicates: bool
    @param remove_duplicates: Whether duplicates are removed.
    @type  encoding: str
    @param encoding: The encoding of the file.
    @rtype:  list[Host]
    @return: The newly created host instances.
    """
    # Open the file.
    if not os.path.exists(filename):
        raise IOError('No such file: %s' % filename)
    file_handle = codecs.open(filename, 'r', encoding)

    # Read the hostnames.
    have = set()
    hosts = []
    for line in file_handle:
        hostname = line.split('#')[0].strip()
        if hostname == '':
            continue
        if remove_duplicates and hostname in have:
            continue
        have.add(hostname)
        hosts.append(to_host(hostname, default_protocol, default_domain))

    file_handle.close()
    return hosts
Ejemplo n.º 2
0
def get_hosts_from_file(filename,
                        default_protocol  = 'telnet',
                        default_domain    = '',
                        remove_duplicates = False,
                        encoding          = 'utf-8'):
    """
    Reads a list of hostnames from the file with the given name.

    @type  filename: string
    @param filename: A full filename.
    @type  default_protocol: str
    @param default_protocol: Passed to the Host constructor.
    @type  default_domain: str
    @param default_domain: Appended to each hostname that has no domain.
    @type  remove_duplicates: bool
    @param remove_duplicates: Whether duplicates are removed.
    @type  encoding: str
    @param encoding: The encoding of the file.
    @rtype:  list[Host]
    @return: The newly created host instances.
    """
    # Open the file.
    if not os.path.exists(filename):
        raise IOError('No such file: %s' % filename)
    file_handle = codecs.open(filename, 'r', encoding)

    # Read the hostnames.
    have  = set()
    hosts = []
    for line in file_handle:
        hostname = line.split('#')[0].strip()
        if hostname == '':
            continue
        if remove_duplicates and hostname in have:
            continue
        have.add(hostname)
        hosts.append(to_host(hostname, default_protocol, default_domain))

    file_handle.close()
    return hosts
Ejemplo n.º 3
0
def connect(host, default_protocol = 'telnet', **kwargs):
    """
    Like L{prepare()}, but also connects to the host by calling
    L{Protocol.connect()}. If the URL or host contain any login info, this
    function also logs into the host using L{Protocol.login()}.

    @type  host: str or Host
    @param host: A URL-formatted hostname or a L{Exscript.Host} object.
    @type  default_protocol: str
    @param default_protocol: Protocol that is used if the URL specifies none.
    @type  kwargs: dict
    @param kwargs: Passed to the protocol constructor.
    @rtype:  Protocol
    @return: An instance of the protocol.
    """
    host    = to_host(host)
    conn    = prepare(host, default_protocol, **kwargs)
    account = host.get_account()
    conn.connect(host.get_address(), host.get_tcp_port())
    if account is not None:
        conn.login(account)
    return conn
Ejemplo n.º 4
0
def prepare(host, default_protocol = 'telnet', **kwargs):
    """
    Creates an instance of the protocol by either parsing the given
    URL-formatted hostname using L{Exscript.util.url}, or according to
    the options of the given L{Exscript.Host}.

    @type  host: str or Host
    @param host: A URL-formatted hostname or a L{Exscript.Host} instance.
    @type  default_protocol: str
    @param default_protocol: Protocol that is used if the URL specifies none.
    @type  kwargs: dict
    @param kwargs: Passed to the protocol constructor.
    @rtype:  Protocol
    @return: An instance of the protocol.
    """
    host     = to_host(host, default_protocol = default_protocol)
    protocol = host.get_protocol()
    conn     = create_protocol(protocol, **kwargs)
    if protocol == 'pseudo':
        filename = host.get_address()
        conn.device.add_commands_from_file(filename)
    return conn
Ejemplo n.º 5
0
def connect(host, default_protocol='telnet', **kwargs):
    """
    Like L{prepare()}, but also connects to the host by calling
    L{Protocol.connect()}. If the URL or host contain any login info, this
    function also logs into the host using L{Protocol.login()}.

    @type  host: str or Host
    @param host: A URL-formatted hostname or a L{Exscript.Host} object.
    @type  default_protocol: str
    @param default_protocol: Protocol that is used if the URL specifies none.
    @type  kwargs: dict
    @param kwargs: Passed to the protocol constructor.
    @rtype:  Protocol
    @return: An instance of the protocol.
    """
    host = to_host(host)
    conn = prepare(host, default_protocol, **kwargs)
    account = host.get_account()
    conn.connect(host.get_address(), host.get_tcp_port())
    if account is not None:
        conn.login(account)
    return conn
Ejemplo n.º 6
0
def prepare(host, default_protocol='telnet', **kwargs):
    """
    Creates an instance of the protocol by either parsing the given
    URL-formatted hostname using L{Exscript.util.url}, or according to
    the options of the given L{Exscript.Host}.

    @type  host: str or Host
    @param host: A URL-formatted hostname or a L{Exscript.Host} instance.
    @type  default_protocol: str
    @param default_protocol: Protocol that is used if the URL specifies none.
    @type  kwargs: dict
    @param kwargs: Passed to the protocol constructor.
    @rtype:  Protocol
    @return: An instance of the protocol.
    """
    host = to_host(host, default_protocol=default_protocol)
    protocol = host.get_protocol()
    conn = create_protocol(protocol, **kwargs)
    if protocol == 'pseudo':
        filename = host.get_address()
        conn.device.add_commands_from_file(filename)
    return conn
Ejemplo n.º 7
0
def get_hosts_from_csv(filename,
                       default_protocol='telnet',
                       default_domain='',
                       encoding='utf-8'):
    """
    Reads a list of hostnames and variables from the tab-separated .csv file
    with the given name. The first line of the file must contain the column
    names, e.g.::

        address	testvar1	testvar2
        10.0.0.1	value1	othervalue
        10.0.0.1	value2	othervalue2
        10.0.0.2	foo	bar

    For the above example, the function returns *two* host objects, where
    the 'testvar1' variable of the first host holds a list containing two
    entries ('value1' and 'value2'), and the 'testvar1' variable of the
    second host contains a list with a single entry ('foo').

    Both, the address and the hostname of each host are set to the address
    given in the first column. If you want the hostname set to another value,
    you may add a second column containing the hostname::

        address	hostname	testvar
        10.0.0.1	myhost	value
        10.0.0.2	otherhost	othervalue

    :type  filename: string
    :param filename: A full filename.
    :type  default_protocol: str
    :param default_protocol: Passed to the Host constructor.
    :type  default_domain: str
    :param default_domain: Appended to each hostname that has no domain.
    :type  encoding: str
    :param encoding: The encoding of the file.
    :rtype:  list[Host]
    :return: The newly created host instances.
    """
    # Open the file.
    if not os.path.exists(filename):
        raise IOError('No such file: %s' % filename)

    with codecs.open(filename, 'r', encoding) as file_handle:
        # Read and check the header.
        header = file_handle.readline().rstrip()
        if re.search(r'^(?:hostname|address)\b', header) is None:
            msg = 'Syntax error in CSV file header:'
            msg += ' File does not start with "hostname" or "address".'
            raise Exception(msg)
        if re.search(r'^(?:hostname|address)(?:\t[^\t]+)*$', header) is None:
            msg = 'Syntax error in CSV file header:'
            msg += ' Make sure to separate columns by tabs.'
            raise Exception(msg)
        varnames = [str(v) for v in header.split('\t')]
        varnames.pop(0)

        # Walk through all lines and create a map that maps hostname to
        # definitions.
        last_uri = ''
        line_re = re.compile(r'[\r\n]*$')
        hosts = []
        for line in file_handle:
            if line.strip() == '':
                continue

            line = line_re.sub('', line)
            values = line.split('\t')
            uri = values.pop(0).strip()

            # Add the hostname to our list.
            if uri != last_uri:
                #print "Reading hostname", hostname_url, "from csv."
                host = to_host(uri, default_protocol, default_domain)
                last_uri = uri
                hosts.append(host)

            # Define variables according to the definition.
            for i, varname in enumerate(varnames):
                try:
                    value = values[i]
                except IndexError:
                    value = ''
                if varname == 'hostname':
                    host.set_name(value)
                else:
                    host.append(varname, value)

    return hosts
Ejemplo n.º 8
0
 def testToHost(self):
     from Exscript.util.cast import to_host
     self.assertIsInstance(to_host('localhost'), Host)
     self.assertIsInstance(to_host(Host('localhost')), Host)
     self.assertRaises(TypeError, to_host, None)
Ejemplo n.º 9
0
 def testToHost(self):
     from Exscript.util.cast import to_host
     self.assertTrue(isinstance(to_host('localhost'),       Host))
     self.assertTrue(isinstance(to_host(Host('localhost')), Host))
     self.assertRaises(TypeError, to_host, None)
Ejemplo n.º 10
0
def get_hosts_from_csv(filename,
                       default_protocol = 'telnet',
                       default_domain   = '',
                       encoding         = 'utf-8'):
    """
    Reads a list of hostnames and variables from the tab-separated .csv file
    with the given name. The first line of the file must contain the column
    names, e.g.::

        address	testvar1	testvar2
        10.0.0.1	value1	othervalue
        10.0.0.1	value2	othervalue2
        10.0.0.2	foo	bar

    For the above example, the function returns *two* host objects, where
    the 'testvar1' variable of the first host holds a list containing two
    entries ('value1' and 'value2'), and the 'testvar1' variable of the
    second host contains a list with a single entry ('foo').

    Both, the address and the hostname of each host are set to the address
    given in the first column. If you want the hostname set to another value,
    you may add a second column containing the hostname::

        address	hostname	testvar
        10.0.0.1	myhost	value
        10.0.0.2	otherhost	othervalue

    @type  filename: string
    @param filename: A full filename.
    @type  default_protocol: str
    @param default_protocol: Passed to the Host constructor.
    @type  default_domain: str
    @param default_domain: Appended to each hostname that has no domain.
    @type  encoding: str
    @param encoding: The encoding of the file.
    @rtype:  list[Host]
    @return: The newly created host instances.
    """
    # Open the file.
    if not os.path.exists(filename):
        raise IOError('No such file: %s' % filename)
    file_handle = codecs.open(filename, 'r', encoding)

    # Read the header.
    header = file_handle.readline().rstrip()
    if re.search(r'^(?:hostname|address)\b', header) is None:
        msg  = 'Syntax error in CSV file header:'
        msg += ' File does not start with "hostname" or "address".'
        raise Exception(msg)
    if re.search(r'^(?:hostname|address)(?:\t[^\t]+)*$', header) is None:
        msg  = 'Syntax error in CSV file header:'
        msg += ' Make sure to separate columns by tabs.'
        raise Exception(msg)
    varnames = [str(v) for v in header.split('\t')]
    varnames.pop(0)

    # Walk through all lines and create a map that maps hostname to
    # definitions.
    last_uri = ''
    line_re  = re.compile(r'[\r\n]*$')
    hosts    = []
    for line in file_handle:
        if line.strip() == '':
            continue

        line   = line_re.sub('', line)
        values = line.split('\t')
        uri    = values.pop(0).strip()

        # Add the hostname to our list.
        if uri != last_uri:
            #print "Reading hostname", hostname_url, "from csv."
            host     = to_host(uri, default_protocol, default_domain)
            last_uri = uri
            hosts.append(host)

        # Define variables according to the definition.
        for i, varname in enumerate(varnames):
            try:
                value = values[i]
            except IndexError:
                value = ''
            if varname == 'hostname':
                host.set_name(value)
            else:
                host.append(varname, value)

    file_handle.close()
    return hosts