required net-ping gem
gem install net-ping
#!/usr/bin/env ruby# KING SABRI | @KINGSABRI#require 'net/ping'​@icmp = Net::Ping::ICMP.new(ARGV[0])rtary = []pingfails = 0repeat = 5puts 'starting to ping'(1..repeat).each doif @icmp.pingrtary << @icmp.durationputs "host replied in #{@icmp.duration}"elsepingfails += 1puts "timeout"endend​avg = rtary.inject(0) {|sum, i| sum + i}/(repeat - pingfails)puts "Average round-trip is #{avg}\n"puts "#{pingfails} packets were dropped"
If you got what we've represented in Ruby Socket section, then here we wrapping up and do some application depends on it. scanner.rb
#!/usr/bin/env ruby## KING SABRI | @KINGSABRI#require 'socket'require 'thread'require 'timeout'​host = ARGV[0]​def scan(host)(0..1024).each do |port|Thread.new {begintimeout(3) do # timeout of running operations = TCPSocket.new(host, port) # Create new socketputs "[+] #{host} | Port #{port} open"s.closeendrescue Errno::ECONNREFUSED# puts "[!] #{host} | Port #{port} closed"nextrescue Timeout::Errorputs "[!] #{host} | Port #{port} timeout/filtered"nextend}.joinendend​scan host
Run it
ruby scanner.rb 45.33.32.156 # scanme.nmap.com​[+] 45.33.32.156 | Port 22 open[+] 45.33.32.156 | Port 80 open[!] 45.33.32.156 | Port 81 timeout[!] 45.33.32.156 | Port 85 timeout[!] 45.33.32.156 | Port 119 timeout[!] 45.33.32.156 | Port 655 timeout[!] 45.33.32.156 | Port 959 timeout