Ruby: binding a listening socket to a specific interface

I want to create a TCP socket that only listens on a specific interface (say eth0). How can i do this? I tried looking at the Socket API, but I may not understand things properly.

Here is my listening method:

def listen
  socket = TCPServer.open($port)
  while $looping do
    Thread.start(socket.accept) do |server|
      response = server.read
      puts "Command received: #{response}"
      if sanitize(response)
        execute(response)
      end
    end
  end
end

Thanks for the help.

+3
source share
1 answer

You will need to get the IP address of the network interface that you want to listen to and pass it as the first parameter to TCPServer.new. I don't know how to specify an interface by name other than parsing the output %x(ifconfig <interface> | grep inet).

+3
source

Source: https://habr.com/ru/post/1755840/


All Articles