Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question

3 Answers 3

up vote 2 down vote accepted

how about:

lsof | awk '$5 !~ /IPv[46]/{print}'
share|improve this answer
    
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? –  Christopher Neylan Apr 25 '12 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" –  Michał Šrajer Apr 25 '12 at 14:37

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

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

share|improve this answer
    
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? –  Christopher Neylan Apr 24 '12 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 '12 at 21:36

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.