From: Derek Smith on
Hi All,

Is there a ruby substitute for lsof's functionality? 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?

Here is my test code, on my ubuntu personal laptop with a port from
netstat -a.

#!/usr/bin/ruby -w

require 'rubygems'
require 'socket'

#TCPSocket.open('localhost', '34814') do |socket|
# socket.puts "gibberish"
# socket.each_line do |line|
# p line
# end
#end

require 'socket'

host = 'localhost'
port = 47408

s = TCPSocket.open(host, port)

while line = s.gets
puts line.chop
end
s.close

In both cases, I get the error:

derek(a)vaio-ubuntu:~$ sudo ruby port_tst.rb
port_tst.rb:18:in `initialize': Connection refused - connect(2)
(Errno::ECONNREFUSED)
from port_tst.rb:18:in `open'
from port_tst.rb:18


thank you!
--
Posted via http://www.ruby-forum.com/.

From: P YH on

> Hi All,
>
> Is there a ruby substitute for lsof's functionality? 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?
>
> Here is my test code, on my ubuntu personal laptop with a port from
> netstat -a.
>
> #!/usr/bin/ruby -w
>
> require 'rubygems'

Can I ask the rubygems module is used for what purpose?

Thanks.


From: Eric Wong on
Derek Smith <derekbellnersmith(a)yahoo.com> 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.

<snip>

> 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

From: Rajeswar reddy Gaulla on

require 'socket'

host = 'localhost'
port = 47408

begin
Timeout::timeout(10){TCPSocket.open(host , port).puts"'#{port}'
port opened"}
rescue
puts "'#{host}' :: '#{port}' port not opened\n "
end
--
Posted via http://www.ruby-forum.com/.

From: Roger Pack on
Derek Smith wrote:
> Hi All,
>
> Is there a ruby substitute for lsof's functionality? 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?

You could try binding to that port and rescue the error (error meaning
it's in use). Dunno if that helps.
-rp
--
Posted via http://www.ruby-forum.com/.