Welcome to Dream.In.Code
Become an Expert!

Join 136,938 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,852 people online right now. Registration is fast and FREE... Join Now!




Using Ruby ?Function? to Ping an IP Range

 
Reply to this topicStart new topic

Using Ruby ?Function? to Ping an IP Range, Help coding a Ruby Script to ping a Windows network IP range and outpu

Daywalker44
29 Nov, 2007 - 02:44 PM
Post #1

New D.I.C Head
*

Joined: 29 Nov, 2007
Posts: 4


My Contributions
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.gif 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. biggrin.gif
User is offlineProfile CardPM
+Quote Post

spullen
RE: Using Ruby ?Function? To Ping An IP Range
29 Nov, 2007 - 05:18 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
well you can use the pingecho method in the ruby library.
Here is a small snippet of code that will help:
CODE

require 'ping'

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


User is offlineProfile CardPM
+Quote Post

Daywalker44
RE: Using Ruby ?Function? To Ping An IP Range
29 Nov, 2007 - 09:11 PM
Post #3

New D.I.C Head
*

Joined: 29 Nov, 2007
Posts: 4


My Contributions
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. biggrin.gif
User is offlineProfile CardPM
+Quote Post

rahulbatra
RE: Using Ruby ?Function? To Ping An IP Range
30 Nov, 2007 - 12:28 AM
Post #4

D.I.C Head
Group Icon

Joined: 28 Dec, 2005
Posts: 162


Dream Kudos: 275
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Daywalker44
RE: Using Ruby ?Function? To Ping An IP Range
2 Dec, 2007 - 07:04 PM
Post #5

New D.I.C Head
*

Joined: 29 Nov, 2007
Posts: 4


My Contributions
Ok that makes good sense. First off all let me say that I am a NOOB mad.gif crazy.gif 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. biggrin.gif

CODE

#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

User is offlineProfile CardPM
+Quote Post

rahulbatra
RE: Using Ruby ?Function? To Ping An IP Range
4 Dec, 2007 - 06:12 AM
Post #6

D.I.C Head
Group Icon

Joined: 28 Dec, 2005
Posts: 162


Dream Kudos: 275
My Contributions
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.

CODE
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

User is offlineProfile CardPM
+Quote Post

Daywalker44
RE: Using Ruby ?Function? To Ping An IP Range
6 Dec, 2007 - 07:47 PM
Post #7

New D.I.C Head
*

Joined: 29 Nov, 2007
Posts: 4


My Contributions
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
CODE
Ping.pingecho('192.168.1.18, 200')
to output on the same line as "Moses: " ??
User is offlineProfile CardPM
+Quote Post

spullen
RE: Using Ruby ?Function? To Ping An IP Range
9 Dec, 2007 - 10:16 AM
Post #8

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
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:
CODE

print "Hello,"
print " world"
print "\n"
puts "hello, "
puts "world"

User is offlineProfile CardPM
+Quote Post

rahulbatra
RE: Using Ruby ?Function? To Ping An IP Range
9 Dec, 2007 - 09:12 PM
Post #9

D.I.C Head
Group Icon

Joined: 28 Dec, 2005
Posts: 162


Dream Kudos: 275
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:49PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month