Wednesday, December 22, 2010

"RUBY" here_and_there_2- SSH and ping Script



A script to get different outputs from ESX servers, using ruby.

Added error handling too



The "serverlist" is the File from which I read into an array then iterate through the array to do a ping check and SSH

#!/usr/bin/ruby

require 'rubygems'
require 'net/ssh'

USER='xxxxxxxx'
PASSWORD='xxxxxxx'
SERVER_LIST=File.readlines('esxserverlist')

SERVER_LIST.each do |server|

begin
Net::SSH.start(server.chomp, USER, :password=>PASSWORD, :timeout=>10) do |ssh|
host_name=ssh.exec!('hostname')
build_name=ssh.exec!('cat /etc/redhat-release')
vm_build=ssh.exec!('vmware -v')
print host_name.chomp + ",".chomp
print build_name.chomp + ",".chomp
print vm_build
end
rescue Net::SSH::AuthenticationFailed
print server.chomp + "," + "is not having standard credentials\n"
rescue Timeout::Error
print server.chomp + "," + "can not be reached\n"
rescue Errno::EHOSTUNREACH
print server.chomp + "," + "can not be reached\n"
end
end



No comments:

Post a Comment