# Reliable ping method from Windows?

**URL:** <https://www.ruby-forum.com/t/reliable-ping-method-from-windows/148984>\
**Category:** Ruby\
**Created:** [September 18, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/reliable-ping-method-from-windows/148984 "2008-09-18T22:17:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [September 18, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/reliable-ping-method-from-windows/148984/1 "2008-09-18T22:17:25Z")

</div>

Is there a reliable “Ruby” method for pinging/checking whether hosts are  
up?

I have been using Net::PingExternal as I’m pinging Windows machines and  
not  
all of them have port 7 open. However when I try to check 900 or so  
machines I get a SocketError after a while. It complains that there is  
no  
such service echo/tcp. When I try similar code in irb I get the socket  
error after a few minutes then if I try the loop again I get the socket  
error immediately.

I’ve tried a generic ICMP packet but that doesn’t work under Windows  
with  
the standard Ruby build and I don’t have the tools/time to build it my  
self  
under Windows.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [September 18, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/reliable-ping-method-from-windows/148984/2 "2008-09-18T23:01:59Z")

</div>

On Thu, Sep 18, 2008 at 2:09 PM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’ve tried a generic ICMP packet but that doesn’t work under Windows with  
> the standard Ruby build and I don’t have the tools/time to build it my self  
> under Windows.
> 
> –  
> “Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

Here is the full method if anyone wants more context:

# Lets find MAC addresses for the machines in the list

def find\_macs  
# This technique will only give MAC addresses for the machines on  
our  
subnet.  
# However since those are the only ones we can wake it doesn’t  
really  
matter.  
# It is nasty slow and a better way should be found some day.

```
DB[:computers].each do |computer|
  begin
    Net::PingExternal.new(computer[:name]).ping
  rescue SocketError => exception
    next
  end
end

arp_table = `arp -a`.split("\n")[2..-1]

arp_table.each do |address|
  full_name = Socket.gethostbyname(address.split(' ')[0])

  comp = Computer.find :name => full_name.split('.')[0]
  comp.mac = address.split(' ')[1].gsub!('-', ':')
  comp.save
end
close()

```

end # find\_macs

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [September 24, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/reliable-ping-method-from-windows/148984/3 "2008-09-24T15:59:22Z")

</div>

On Thu, Sep 18, 2008 at 9:08 PM, Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > –  
> > > def find\_macs  
> > > Net::PingExternal.new(computer[:name]).ping  
> > > comp = Computer.find :name =\> full\_name.split(‘.’)[0]  
> > > I assume you will have no qualms about doing the  
> > > You’ll need WinPCap as well (the packet capture lib)
> 
> [http://www.winpcap.org/](http://www.winpcap.org/)
> 
> ## djief
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Thanks Jeff,

Although it looks like both parsing arp and the External ping are  
causing me  
other problems. With over 900 machines to check the app keeps borking  
with  
too many open files. So it would appear I need to find another way to  
acquire MAC addresses.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [September 19, 2008, 5:16am UTC](https://www.ruby-forum.com/t/reliable-ping-method-from-windows/148984/4 "2008-09-19T05:16:37Z")

</div>

Glen H. wrote:

> On Thu, Sep 18, 2008 at 2:09 PM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > I’ve tried a generic ICMP packet but that doesn’t work under Windows with  
> > the standard Ruby build and I don’t have the tools/time to build it my self  
> > under Windows.
> > 
> > –  
> > “Hey brother Christian with your high and mighty errand, Your actions speak  
> > so loud, I can’t hear a word you’re saying.”
> > 
> > -Greg Graffin (Bad Religion)
> 
> Here is the full method if anyone wants more context:
> 
> # Lets find MAC addresses for the machines in the list
> 
> def find\_macs  
> # This technique will only give MAC addresses for the machines on  
> our  
> subnet.  
> # However since those are the only ones we can wake it doesn’t  
> really  
> matter.  
> # It is nasty slow and a better way should be found some day.
> 
> ```
> DB[:computers].each do |computer|
> begin
> Net::PingExternal.new(computer[:name]).ping
> rescue SocketError => exception
> next
> end
> end
> 
> arp_table = `arp -a`.split("\n")[2..-1]
> 
> arp_table.each do |address|
> full_name = Socket.gethostbyname(address.split(' ')[0])
> 
> comp = Computer.find :name => full_name.split('.')[0]
> comp.mac = address.split(' ')[1].gsub!('-', ':')
> comp.save
> end
> close()
> 
> ```
> 
> end # find\_macs

NMap is very fast and comes in a WinDoze flavor.

Since you’re already mashing up the arp output,  
I assume you will have no qualms about doing the  
same for NMap output.

Not a pure Ruby solution but then NMap is open source  
so if you’re really into purity you could transcribe it’s  
approach to WinDoze discovery (maybe…possibly…)

But if expedience is the order of the day…

> **[Nmap: the Network Mapper - Free Security Scanner](https://nmap.org/)**
>
> Nmap Free Security Scanner, Port Scanner, & Network Exploration Tool. Download open source software for Linux, Windows, UNIX, FreeBSD, etc.

You’ll need WinPCap as well (the packet capture lib)

[http://www.winpcap.org/](http://www.winpcap.org/)

djief
