From: Eric Wong Date: 2010-04-01T16:03:51+09:00 Subject: Re: socket programming...lsof? Derek Smith wrote: > Hi All, > > Is there a ruby substitute for lsof's functionality? Hi Derek, I was wondering that today, too. lsof is highly system-specific, so it might not be readily implemented. lsof and netstat (at least on my Linux system) just parses various text files in /proc/. **Linux only** Reading lsof strace output and the Linux kernel sources gave me enough info to get what I needed without repeatedly invoking lsof. I found Documentation/networking/proc_net_tcp.txt of the Linux kernel source useful for describing /proc/net/tcp. For /proc/net/unix, I had to read the unix_seq_show() function in net/unix/af_unix.c A better solution might be to use netlink (which I still want to do for the project I'm working on), but that involves more work than writing a simple text file parser in Ruby :) > Meaning I want to > listen on certain ports/sockets such as identd's port 113 to see if any > process is using this port. Is this possible in Ruby w/out using lsof? What Roger said about trying to bind that given port (or connecting to it, use Socket#connect_nonblock in case identd is slow to respond). > Here is my test code, on my ubuntu personal laptop with a port from > netstat -a. > host = 'localhost' You may also want to give '127.0.0.1' a try if you're sure that port is listening. IIRC, some newer Linux systems favor IPv6 addresses over IPv4 ones. -- Eric Wong