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?
Related
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!
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
I trying to make a connection with following ruby code
server
require 'socket'
puts "server"
socket = TCPServer.open(1000)
client = socket.accept
puts "connected"
client
require 'socket'
puts "client"
server= TCPSocket.open("172.30.1.2",1000)
puts "connected"
but client side always says target machine refused it.
no "connected" printing on both side
when I change "172.30.1.2" to "localhost", it works.
I wonder why I can't connect to my self
"172.30.1.2" is my IPv4 address
when I type ipconfig on cmd,
it says "172.30.1.2" is my ip.
I configure DMZ and port-forwarding too.
and all firewall is off.
I tried on windows xp and windows 7.
can someone help me?
(I want to connect to my computer from outside. and that's why I tried "172.30.1.2")
thanks.
In order to create server use TCPServer.open("172.30.1.2",1000)
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
}
I want to write a simple server socket in Ruby, which, when a client connects to it, prints a message and closes the client connection. I came up with:
require 'socket'
server = TCPServer.open('localhost',8800)
loop {
client = server.accept
Thread.start do
s = client
s.puts "Closing the connection. Bye!"
s.close
end
}
However, when I access "localhost:8800" in my browser, I am not getting that message, instead, it says page not found.. What am I doing wrong here?
It is quite likely that your browser is expecting something on the remote end that talks Http.
This is dependant upon your browser and also the exact URI you typed in. It is also possible that your browser is connecting getting the connection close and then displaying an error page.
If you want to see the server working then use telnet from a command prompt. So in one window type ruby ./myfilename.rb and then in another type telnet localhost 8800