Using Ruby ?Function? to Ping an IP Range

Help coding a Ruby Script to ping a Windows network IP range and outpu

Page 1 of 1

9 Replies - 17856 Views - Last Post: 11 February 2011 - 03:24 PM Rate Topic: -----

#1 Daywalker44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 07

Using Ruby ?Function? to Ping an IP Range

Post icon  Posted 29 November 2007 - 03:44 PM

I'm looking to write a short Ruby script to ping all of my Windows network IP addresses and output the results of whether or not the computers responded into a text file. I am new to Ruby :crazy: and would very much appreciate all the help I can get. I am looking to achieve this on a Windows based network with ruby installed on the server that executes the script I don't see any reason why this would not be possible. Please let me know otherwise. Thank You very much in advance. :D

Is This A Good Question/Topic? 0
  • +

Replies To: Using Ruby ?Function? to Ping an IP Range

#2 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Using Ruby ?Function? to Ping an IP Range

Posted 29 November 2007 - 06:18 PM

well you can use the pingecho method in the ruby library.
Here is a small snippet of code that will help:
require 'ping'

puts Ping.pingecho('localhost', 5) # should print true if it can ping the server


Was This Post Helpful? 0
  • +
  • -

#3 Daywalker44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 07

Re: Using Ruby ?Function? to Ping an IP Range

Posted 29 November 2007 - 10:11 PM

Ok, this gives me a some ideas. Thank You. I'm having trouble finding good references when I'm looking for information on the syntax for pinging multiple computers with an IP range Ping. Is it possible to Ping a range (I.E. 192.168.1.1-250) of IP addresses with Ruby code? Any further elaboration and code examples of how to execute this would be much appreciated. Thanks so much in advance. :D
Was This Post Helpful? 0
  • +
  • -

#4 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Re: Using Ruby ?Function? to Ping an IP Range

Posted 30 November 2007 - 01:28 AM

Well, the simplest way could be to store "192.168.1." in a string. Then create a loop with an upper limit of 250. Inside the loop, concatenate the above string with the loop index. The resultant string, which is effectively the machine IP, can then be pinged inside the same loop creating an effect of a range scan.
Was This Post Helpful? 0
  • +
  • -

#5 Daywalker44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 07

Re: Using Ruby ?Function? to Ping an IP Range

Posted 02 December 2007 - 08:04 PM

Ok that makes good sense. First off all let me say that I am a NOOB :angry: :crazy: when it comes to programming in general except when dealing with Windows batch files (if you even want to call that programming) I'm trying to get better and learn how to program with ruby. This is the code that I have so far, keep in mind I haven't even tried to attempt the whole loop storing '192.168.1.' in a string etc. etc. The code I have is VERY simple and unorthodox I'm looking for someone to give me some assistance in cleaning this mess up. I would like the output in my output file to be on the same line. For example:

Noah: True
Gideon: True

This is what I have because I don't know the syntax on how to accomplish the same line output, im thinking its something simple.
I'm getting this:

Noah:
false

Gideon:
false

etc. etc.

If anyone could give me some reference code on how to accomplish the loop way of pinging the entire networks IP range I would be very grateful as I would like to ping all my machines and not just my servers. Thanks So Much in advance. :D

#This will ping the specified IP address and return to a file if they are up or down.
require 'ping'
require 'resolv-replace'

f = File.new("PingServers.txt", "a")
f.puts Time.now

f.puts "If True, Server is UP, YAY!. If False, Server is DOWN! FIX IT!."
f.puts " "

f.puts "Noah: "
f.puts Ping.pingecho('192.168.1.6, 200')
f.puts " "

f.puts "David: "
f.puts Ping.pingecho('192.168.1.7, 200')
f.puts " "

f.puts "Moses: "
f.puts Ping.pingecho('192.168.1.18, 200')
f.puts " "

f.puts "Gideon: "
f.puts Ping.pingecho('192.168.1.23, 200')
f.puts " "
f.puts " "
f.puts " "
f.close


Was This Post Helpful? 0
  • +
  • -

#6 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Re: Using Ruby ?Function? to Ping an IP Range

Posted 04 December 2007 - 07:12 AM

Here's a basic code of what I meant. It'll just print out the IP from 1 to 20. You have to ping them instead of printing them simply. Modify the code a bit and you'll have your result.

machine_IP = '192.168.1.'
 i=1

for i in 1..20
  @IP_addr_pinged = machine_IP + i.to_s
  puts @IP_addr_pinged
  i=i+1
end

Was This Post Helpful? 0
  • +
  • -

#7 Daywalker44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 07

Re: Using Ruby ?Function? to Ping an IP Range

Posted 06 December 2007 - 08:47 PM

Hi, Thanks so much, I was wondering if you could possibly go through what this code is doing line by line. I'm really new to Ruby and I'm not sure exactly whats going on here. Also, if someone could tell me if there is a better way to output a blank line that would be great. Also, How would I get the result of
Ping.pingecho('192.168.1.18, 200')
to output on the same line as "Moses: " ??
Was This Post Helpful? 0
  • +
  • -

#8 spullen  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Using Ruby ?Function? to Ping an IP Range

Posted 09 December 2007 - 11:16 AM

So in ruby there are actually a few ways to print to the screen. The first way is with the puts method, which prints the text with a newline. Another way to print is with the print method, which prints to the screen, except it doesn't add that newline like puts does, to make a newline you can use \n. There are also other functions for printing as well, which I suggest looking up in the API.

Here is an example of what I am talking about:
print "Hello,"
print " world"
print "\n"
puts "hello, "
puts "world"


Was This Post Helpful? 0
  • +
  • -

#9 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Re: Using Ruby ?Function? to Ping an IP Range

Posted 09 December 2007 - 10:12 PM

I'll take you through my last code. The first line assigns "192.168.1." to a variable called machine_IP since this is a common part in all IP's that you want to ping. Next we set a counter and call it 'i'.

We open up a loop for 'i' and then join the two strings machine_IP and 'i' converted to a string. This gives us the first IP address we want to ping "192.168.1.1". We increment 'i' and the loop is checked again. Since this value will be less than 20, the loop is run again, though the value of 'i' now is 2, so the IP address becomes "192.168.1.2" (and so on and so forth). You could ping the machine in the loop and get the result printed.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Joe Mazzotti*


Reputation:

Re: Using Ruby ?Function? to Ping an IP Range

Posted 11 February 2011 - 03:24 PM

View PostDaywalker44, on 29 November 2007 - 03:44 PM, said:

I'm looking to write a short Ruby script to ping all of my Windows network IP addresses and output the results of whether or not the computers responded into a text file. I am new to Ruby :crazy: and would very much appreciate all the help I can get. I am looking to achieve this on a Windows based network with ruby installed on the server that executes the script I don't see any reason why this would not be possible. Please let me know otherwise. Thank You very much in advance. :D


I would do it as follows:

require 'ping'
iprange = Range.new (1,254)
iprange.each {|x|
                        address = "192.168.0.#{x}"
                        if Ping.pingecho(address) then puts "Reply from #{address}"
                        else puts "#{address} timed out"
                        end}
 


you can replace the puts commands with output to a file or dump into a hash or whatever you need to do with it.
Was This Post Helpful? 0

Page 1 of 1