Created
October 25, 2012 17:46
-
-
Save makevoid/3954279 to your computer and use it in GitHub Desktop.
Ping and SSH test all hosts in a given subnet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'net/ssh' | |
| require 'ipaddr' | |
| USER = "makevoid" | |
| PASS = File.read File.expand_path("~/.password") | |
| IPS = "192.168.0.0/24" | |
| def pings?(host) | |
| `ping -q -t 1 -c 1 #{host}`; | |
| $?.exitstatus == 0 | |
| end | |
| def can_ssh?(host) | |
| output = false | |
| begin | |
| Net::SSH.start(host, USER, password: PASS) do |ssh| | |
| output = ssh.exec! "hostname" | |
| end | |
| rescue SocketError | |
| puts "host not good" | |
| rescue Errno::ECONNREFUSED | |
| puts "no ssh here" | |
| rescue Net::SSH::AuthenticationFailed | |
| puts "can't authenticate here" | |
| end | |
| output | |
| end | |
| IPAddr.new(IPS).to_range.each do |host| | |
| print "." | |
| if pings? host | |
| puts "\npinged #{host}" | |
| if can_ssh? host.to_s | |
| puts "sshed into #{host}!!!" | |
| end | |
| end | |
| end | |
| __END__ | |
| Sample output: | |
| .. | |
| pinged 192.168.0.1 | |
| no ssh here | |
| . | |
| pinged 192.168.0.2 | |
| no ssh here | |
| . | |
| pinged 192.168.0.3 | |
| no ssh here | |
| .. | |
| pinged 192.168.0.5 | |
| no ssh here | |
| ... | |
| pinged 192.168.0.8 | |
| sshed into 192.168.0.8!!! | |
| .. | |
| pinged 192.168.0.10 | |
| sshed into 192.168.0.10!!! | |
| ................................ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment