5

Running lsof -i (with no arguments to -i) "selects the listing of all Internet and x.25 (HP-UX) network files." I'm looking for a reliable way to do the inverse: show a listing of everything EXCEPT Internet files. By reliable, I mean a way that would only require lsof to be run once. Two subsequent runs (i.e., a normal lsof followed by a lsof -i in order to compare) is not reliable because of the strong possibility of churn.

Thanks in advance!

3 Answers 3

2

how about:

lsof | awk '$5 !~ /IPv[46]/{print}'
2
  • is it safe to assume that COMMAND, PID, USER, FD, and TYPE will always consist of 1 token each? i.e., they'll never contain two spaces and they'll never be missing? Apr 25, 2012 at 13:26
  • @user112358132134: it will always be 1 token. If yo rename "wget" to "wg et", then lsof will display in column COMMAND only first token "wg" Apr 25, 2012 at 14:37
0

sudo lsof | grep -v IPv4 | grep -v IPv6

would this work for you? this would exclude only what -i lists.

2
  • but what if someone had the file IPv4.txt open? i guess to generalize this, you're suggesting that i filter out records where the TYPE column contains either IPv4 or IPv6? Apr 24, 2012 at 19:37
  • grep can limit to word only with -w. well what if the file name is IPv4? you can use awk to filter and make sure that only that field (type) value is IPv4, in that case.
    – johnshen64
    Apr 24, 2012 at 21:36
0

I quickly through this together, so the quality may not be up to par.

This Python script reads each line returned from the "lsof" command and checks to make sure that the "TYPE" column specifically does not include "IPv4" or "IPv6".

#!/usr/bin/env python

from sys import stdin
from re  import search

def main():
    lsof = stdin.read().strip()

    final_files = [lsof.split('\n')[0]]

    for line in lsof.split('\n')[1:]:
        if not search(r'.*(IPv4|IPv6).*', line.split()[4]):
            final_files.append(line)

    print '\n'.join(final_files)

if __name__ == '__main__':
    main()

You may execute it like so:

lsof | ./inverse_lsof.py

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .