Redis -Pub\Sub - Timed out connecting to Redis - ruby

I have the the following ruby to subscribe to a channel.
When I set the host to 'localhost' I have no problems -i.e. script starts up
When I set to an IP address (where redis is running) other than localhost I get a timeout.
Timed out connecting to Redis
If I remove :timeout => 0 the script will run, however I believe this is the correct value to set for subscribers to ensure my client will never timeout.
How do I prevent timeouts occurring for a subscriber?
require 'redis'
$redis = Redis.new :host => 'IPADDRESS', :timeout => 0
$redis.subscribe('MyChannel', ) do |on|
on.message do |channel, msg|
puts "M is #{msg}"
end
end

Have you configured redis to listen on the correct IP Address? By default it only listens on localhost. In the redis.comf you will find a line that says bind 127.0.0.1. Either comment it out to have redis listen on all addresses the system has, or change the localhost address to the address you want it to listen on.
Be aware that setting it to an IP will mean it only will be available on that IP.
Once you've made the change, restart the redis service. Then verify using redis-cli as described in this answer to a similar question

Related

Ruby - UDP - How to spoof IP and Port Number

I've been messing about with some sample client and server code for sending and receiving UDP packets, here's what I'm doing for the send
require 'socket'
s = UDPSocket.new
s.send("hello", 0, 'localhost', 1234)
Is it possible to somehow use a fake IP address and port number when sending? haven't come across anything online with any examples of how to do so.
You need to bind the socket before calling send:
require 'socket'
s = UDPSocket.new
s.bind('128.100.8.6', 1253)
s.send(...)
Keep in mind that your upstream is in no way responsible for delivering "spoofed" packets and that many providers will dump these if they're not originating in their network.

How to get other computer to connect to my localhost server?

I recently started learning Ruby Sockets and decided to research the topic. I came across the ruby-doc which had some example code that ran smoothly:
This is the example code for the server:
require 'socket'
server = TCPServer.new 2000 # Server bound to port 2000
loop do
client = server.accept # Wait for a client to connect
client.puts "Hello !"
client.puts "Time is #{Time.now}"
client.close
end
And the example code for the client:
require 'socket'
s = TCPSocket.new 'localhost', 2000
while line = s.gets # Read lines from socket
puts line # and print them
end
s.close # close socket when done
So this ran well but I was wondering how I would get the client to connect if it is running from a different computer. So I attempted to replace the "'localhost'" in the client code with my public IP address courtesy of whatismyip.com, however, when I tried running the new client code on a different computer I merely got a timeout error. I even attempted running the new client code on the same machine running the server but still I got a timeout error.
Does anyone know how I can get this to work properly?
Any help would be much appreciated!
Greg Hewgill helped me figure this out:
My first problem was that I was using the wrong address. Greg suggested I check my actual address through the cmd command "ipconfig". The command gave me the actual address that the server was being hosted on. Through this I changed the "'localhost'" in the client code and changed it to the actual IP address. Upon running, I received an error that stated that the server had actively refused the connection. This was fixed by also changing the 'localhost' in the server code to the IP address of the server's machine.
Thank you Greg for the help!

Monitoring of an active SMTP service on a website

I am trying to check if network services i.e. HTTP, HTTPs,TCP, SMTP, FTP, Telnet are activated on a website and running correctly without any breaks. I wrote the following code to open the TCP connection to the remote host on remote port and check if the connection can be established with the website.
require 'timeout'
require 'socket'
def ping(host,port)
begin
Timeout::timeout(5) do
s=TCPSocket.new(host,port)
s.close
return true
end
rescue Err::ECONNREFUSED
return false
rescue Err::Timeout
return false
end
end
I also wrote code to monitor the SMTP service already running on a website.
smtp=Net::SMTP.start("mail.google.com",25).started?
But the output is false whereas gmail supports SMTP service. So the question is:
What should be done to monitor the SMTP service already running on a website?

How to open a port in Windows using Ruby?

My windows system has the firewall enabled.
I would like to allow incoming connections on a particular port (say: 4546).
Is there a ruby library that can help me do this ?
Detail:
I have a sinatra application (webserver) running on port 4546. I needed to bring down the firewall in order for it to work.
I am looking for a way to not keep the port 4546 under the firewall list.
Yes, you can do that with this:
require 'socket' # Get sockets from stdlib
server = TCPServer.open(4546) # Socket to listen on port 4546
loop { # Servers run forever
client = server.accept # Wait for a client to connect
client.puts(Time.now.ctime) # Send the time to the client
client.puts "Closing the connection. Bye!"
client.close # Disconnect from the client
}

Why am I not receiving UDP Packets sent to my own IP?

I've written this ruby program to send a UDP Packet to port 16800:
require 'socket'
sock = UDPSocket.new
sock.bind("", 47123)
sock.send("Hello!", 0, "192.168.0.100", 16800)
sock.close
where 192.168.0.100 is my own ip address. However, the packet doesn't show in Wireshark and my UDP server listening on port 16800 on the same PC (running Linux) isn't receiving anything.
When I slightly rewrite the program to
require 'socket'
sock = UDPSocket.new
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
sock.bind("", 47123)
sock.send("Hello!", 0, "<broadcast>", 16800)
sock.close
the packet does show up in Wireshark, but my server still isn't receiving anything.
Am I missing something obvious?
EDIT:
The server code, for completeness
require 'socket'
sock = UDPSocket.new
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
sock.bind(nil, 16800)
while true
packet = sock.recvfrom(1024)
puts packet
end
However, this was copied somewhere from the internet, and while editing it for stackoverflow, i found out that the server always receives packets sent to 127.0.0.1 but when I change nil to "", the server suddenly also receives the packets sent above. Is this a bug in the ruby socket implementation?
I just used your very code and the problem is you're not binding to a specific IP address. I don't know about Linux but on OS X there's an issue where sometimes the default will attach to an unused IP6 address rather than the interface you were expecting.
Changing "" to 192.168.1.255 on the server, and including "192.168.1.255" on the bind in the client made all this work for me :)
UDPSocket.BIND takes a host and a port. Try changing the nil to your IP address. From http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/; use
require 'socket'
def local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
UDPSocket.open do |s|
s.connect '64.233.187.99', 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
to get your IP
Have you tried sending to "localhost" or "127.0.0.1"? This will send directly to the local computer and will help diagnose the problem further.
Kind of speculating here, but you might want to consider something like this - when you use the computer's own IP, the packets aren't visible on the physical link b/c the tcp/ip stack doesn't have to push them that far to get them where they are going, it can turn them around internally to the computer at one of the higher layers in that seven-layer stack model (the 'transport' layer, perhaps?)
If such a connection is supposed to work, and you see the packages using Wireshark, then you should also check the software firewall on your machine. If it blocks traffic to the UDP port you want to use you can still see the package using Wireshark!

Resources