Exemple #1
0
 def retrieve(self, kind, pid=None):
     if kind not in self.tmap:
         raise ValueError("invalid %r kind argument; choose between %s"
                          % (kind, ', '.join([repr(x) for x in self.tmap])))
     if pid is not None:
         inodes = self.get_proc_inodes(pid)
         if not inodes:
             # no connections for this process
             return []
     else:
         inodes = self.get_all_inodes()
     ret = []
     for f, family, type_ in self.tmap[kind]:
         if family in (socket.AF_INET, socket.AF_INET6):
             ls = self.process_inet(
                 "/proc/net/%s" % f, family, type_, inodes, filter_pid=pid)
         else:
             ls = self.process_unix(
                 "/proc/net/%s" % f, family, inodes, filter_pid=pid)
         for fd, family, type_, laddr, raddr, status, bound_pid in ls:
             if pid:
                 conn = _common.pconn(fd, family, type_, laddr, raddr,
                                      status)
             else:
                 conn = _common.sconn(fd, family, type_, laddr, raddr,
                                      status, bound_pid)
             ret.append(conn)
     return ret
Exemple #2
0
def net_connections(kind, _pid=-1):
    """Return socket connections.  If pid == -1 return system-wide
    connections (as opposed to connections opened by one process only).
    Only INET sockets are returned (UNIX are not).
    """
    cmap = _common.conn_tmap.copy()
    if _pid == -1:
        cmap.pop('unix', 0)
    if kind not in cmap:
        raise ValueError("invalid %r kind argument; choose between %s"
                         % (kind, ', '.join([repr(x) for x in cmap])))
    families, types = _common.conn_tmap[kind]
    rawlist = cext.net_connections(_pid, families, types)
    ret = []
    for item in rawlist:
        fd, fam, type_, laddr, raddr, status, pid = item
        if fam not in families:
            continue
        if type_ not in types:
            continue
        status = TCP_STATUSES[status]
        if _pid == -1:
            nt = _common.sconn(fd, fam, type_, laddr, raddr, status, pid)
        else:
            nt = _common.pconn(fd, fam, type_, laddr, raddr, status)
        ret.append(nt)
    return ret
Exemple #3
0
def net_connections(kind, _pid=-1):
    """Return socket connections.  If pid == -1 return system-wide
    connections (as opposed to connections opened by one process only).
    Only INET sockets are returned (UNIX are not).
    """
    cmap = _common.conn_tmap.copy()
    if _pid == -1:
        cmap.pop('unix', 0)
    if kind not in cmap:
        raise ValueError("invalid %r kind argument; choose between %s" %
                         (kind, ', '.join([repr(x) for x in cmap])))
    families, types = _common.conn_tmap[kind]
    rawlist = cext.net_connections(_pid, families, types)
    ret = []
    for item in rawlist:
        fd, fam, type_, laddr, raddr, status, pid = item
        if fam not in families:
            continue
        if type_ not in types:
            continue
        status = TCP_STATUSES[status]
        if _pid == -1:
            nt = _common.sconn(fd, fam, type_, laddr, raddr, status, pid)
        else:
            nt = _common.pconn(fd, fam, type_, laddr, raddr, status)
        ret.append(nt)
    return ret
Exemple #4
0
 def retrieve(self, kind, pid=None):
     if kind not in self.tmap:
         raise ValueError("invalid %r kind argument; choose between %s" %
                          (kind, ', '.join([repr(x) for x in self.tmap])))
     if pid is not None:
         inodes = self.get_proc_inodes(pid)
         if not inodes:
             # no connections for this process
             return []
     else:
         inodes = self.get_all_inodes()
     ret = []
     for f, family, type_ in self.tmap[kind]:
         if family in (socket.AF_INET, socket.AF_INET6):
             ls = self.process_inet("/proc/net/%s" % f,
                                    family,
                                    type_,
                                    inodes,
                                    filter_pid=pid)
         else:
             ls = self.process_unix("/proc/net/%s" % f,
                                    family,
                                    inodes,
                                    filter_pid=pid)
         for fd, family, type_, laddr, raddr, status, bound_pid in ls:
             if pid:
                 conn = _common.pconn(fd, family, type_, laddr, raddr,
                                      status)
             else:
                 conn = _common.sconn(fd, family, type_, laddr, raddr,
                                      status, bound_pid)
             ret.append(conn)
     return ret
Exemple #5
0
 def connections(self, kind='inet'):
     if kind not in conn_tmap:
         raise ValueError("invalid %r kind argument; choose between %s"
                          % (kind, ', '.join([repr(x) for x in conn_tmap])))
     families, types = conn_tmap[kind]
     rawlist = cext.proc_connections(self.pid, families, types)
     ret = []
     for item in rawlist:
         fd, fam, type, laddr, raddr, status = item
         status = TCP_STATUSES[status]
         nt = _common.pconn(fd, fam, type, laddr, raddr, status)
         ret.append(nt)
     return ret
Exemple #6
0
 def connections(self, kind='inet'):
     if kind not in conn_tmap:
         raise ValueError("invalid %r kind argument; choose between %s"
                          % (kind, ', '.join([repr(x) for x in conn_tmap])))
     families, types = conn_tmap[kind]
     rawlist = cext.proc_connections(self.pid, families, types)
     ret = []
     for item in rawlist:
         fd, fam, type, laddr, raddr, status = item
         status = TCP_STATUSES[status]
         nt = _common.pconn(fd, fam, type, laddr, raddr, status)
         ret.append(nt)
     return ret
Exemple #7
0
    def connections(self, kind='inet'):
        ret = net_connections(kind, _pid=self.pid)
        # The underlying C implementation retrieves all OS connections
        # and filters them by PID.  At this point we can't tell whether
        # an empty list means there were no connections for process or
        # process is no longer active so we force NSP in case the PID
        # is no longer there.
        if not ret:
            os.stat('/proc/%s' % self.pid)  # will raise NSP if process is gone

        # UNIX sockets
        if kind in ('all', 'unix'):
            ret.extend([_common.pconn(*conn) for conn in
                        self._get_unix_sockets(self.pid)])
        return ret
Exemple #8
0
    def connections(self, kind='inet'):
        ret = net_connections(kind, _pid=self.pid)
        # The underlying C implementation retrieves all OS connections
        # and filters them by PID.  At this point we can't tell whether
        # an empty list means there were no connections for process or
        # process is no longer active so we force NSP in case the PID
        # is no longer there.
        if not ret:
            os.stat('/proc/%s' % self.pid)  # will raise NSP if process is gone

        # UNIX sockets
        if kind in ('all', 'unix'):
            ret.extend([
                _common.pconn(*conn)
                for conn in self._get_unix_sockets(self.pid)
            ])
        return ret
Exemple #9
0
def net_connections(kind, _pid=-1):
    """Return socket connections.  If pid == -1 return system-wide
    connections (as opposed to connections opened by one process only).
    """
    if kind not in conn_tmap:
        raise ValueError("invalid %r kind argument; choose between %s" %
                         (kind, ', '.join([repr(x) for x in conn_tmap])))
    families, types = conn_tmap[kind]
    rawlist = cext.net_connections(_pid, families, types)
    ret = []
    for item in rawlist:
        fd, fam, type, laddr, raddr, status, pid = item
        status = TCP_STATUSES[status]
        if _pid == -1:
            nt = _common.sconn(fd, fam, type, laddr, raddr, status, pid)
        else:
            nt = _common.pconn(fd, fam, type, laddr, raddr, status)
        ret.append(nt)
    return ret
Exemple #10
0
def net_connections(kind, _pid=-1):
    """Return socket connections.  If pid == -1 return system-wide
    connections (as opposed to connections opened by one process only).
    """
    if kind not in conn_tmap:
        raise ValueError("invalid %r kind argument; choose between %s"
                         % (kind, ', '.join([repr(x) for x in conn_tmap])))
    families, types = conn_tmap[kind]
    rawlist = cext.net_connections(_pid, families, types)
    ret = []
    for item in rawlist:
        fd, fam, type, laddr, raddr, status, pid = item
        status = TCP_STATUSES[status]
        if _pid == -1:
            nt = _common.sconn(fd, fam, type, laddr, raddr, status, pid)
        else:
            nt = _common.pconn(fd, fam, type, laddr, raddr, status)
        ret.append(nt)
    return ret