Exemplo n.º 1
0
def service(search=None, protocol=None):
    """Perform a service lookup.

    To lookup all services::

        >>> for item in service():
        ...

    To lookup one service by port number::

        >>> http = service(0, 'tcp')
        >>> print http.port
        80

    Or by service name::

        >>> smtp = service('smtp', 'tcp')
        >>> print smtp.port
        25

    Or by short notation::

        >>> snmp = service('udp/snmp')
        >>> print snmp.port
        161

    """
    if search is None:
        setservent()
        srv = True
        res = []
        while srv:
            srv = getservent()
            if srv:
                res.append(Service(srv))
        endservent()
        return res

    else:
        search = str(search)
        if not protocol and '/' in search:
            protocol, search = search.split('/')
        if proto not in ['tcp', 'udp']:
            raise ValueError('Unsupported protocol "%s"' % (str(protocol),))
        if search.isdigit():
            srv = getservbyport(uid_t(int(search)), c_char_p(protocol))
        else:
            srv = getservbyname(c_char_p(search), c_char_p(protocol))

        if bool(srv):
            return Service(srv)
Exemplo n.º 2
0
def passwd(search=None):
    """Perform a passwd lookup.

    To lookup all passwd entries::

        >>> for item in passwd():
        ...

    To lookup one user by user id (uid)::

        >>> root = passwd(0)
        >>> print root.name
        'root'

    To lookup one user by name::

        >>> root = passwd('root')
        >>> print root.uid
        0

    """
    if not callable(setpwent):
        raise NotImplementedError

    # Iterate over all passwd entries
    if search is None:
        setpwent()
        pwd = True
        res = []
        while pwd:
            pwd = getpwent()
            if pwd:
                res.append(Passwd(pwd))
        endpwent()
        return res

    else:
        search = str(search)
        if search.isdigit():
            pwd = getpwuid(uid_t(int(search)))
        else:
            pwd = getpwnam(c_char_p(search))

        if bool(pwd):
            return Passwd(pwd)
Exemplo n.º 3
0
def rpc(search=None):
    """Perform a remote procedure call lookup.

    To lookup all rpc services::

        >>> for item in rpc():
        ...

    To lookup one rpc service by name::

        >>> nfs = rpc('nfs')
        >>> print nfs.number
        100003
        >>> print nfs.aliases
        ['portmap', 'sunrpc']

    """
    if not callable(setrpcent):
        raise NotImplementedError

    if search is None:
        setrpcent()
        ent = True
        res = []
        while ent:
            ent = getrpcent()
            if ent:
                res.append(RPC(ent))
        endrpcent()
        return res

    else:
        search = str(search)
        if search.isdigit():
            ent = getrpcbynumber(uid_t(int(search)))
        else:
            ent = getrpcbyname(c_char_p(search))

        if bool(ent):
            return RPC(ent)
Exemplo n.º 4
0
def proto(search=None):
    """Perform a protocol lookup.

    To lookup all protocols::

        >>> for item in proto():
        ...

    To lookup a single protocol number::

        >>> tcp = proto('tcp')
        >>> print tcp.proto
        6
    """
    if not callable(setprotoent):
        raise NotImplementedError

    if search is None:
        setprotoent()
        prt = True
        res = []
        while prt:
            prt = getprotoent()
            if prt:
                res.append(Proto(prt))
        endprotoent()
        return res

    else:
        search = str(search)
        if search.isdigit():
            prt = getprotobynumber(uid_t(int(search)))
        else:
            prt = getprotobyname(c_char_p(search))

        if bool(prt):
            return Proto(prt)