Skip to content

Instantly share code, notes, and snippets.

@nico01f
Forked from cotocisternas/try_ssh.rb
Created August 10, 2017 20:27
Show Gist options
  • Save nico01f/704ee0790350ffb1af5dd2e9c6b6a0b9 to your computer and use it in GitHub Desktop.
Save nico01f/704ee0790350ffb1af5dd2e9c6b6a0b9 to your computer and use it in GitHub Desktop.
try_ssh
#!/usr/bin/env ruby
require 'net/ssh'
require 'timeout'
require 'unicode'
require 'colorize'
hosts = ['127.0.0.1']
ports = ['22']
users = ['root', 'admin', 'ec2-user']
passs = ['root', 'password', 'p4ssw0rd']
keys = Dir["/home/coto/.ssh/*.pem"]
@n_combi = 0
def try_connect(host, port, user, pass=nil, key=nil)
begin
if pass
method = 'password'
Net::SSH.start(host.to_s, user.to_s, port: port.to_i, password: pass.to_s, number_of_password_prompts: 0, auth_methods: [method], timeout: 5) do |ssh|
ssh.loop { true }
end
elsif key
method = 'publickey'
Net::SSH.start(host.to_s, user.to_s, port: port.to_i, number_of_password_prompts: 0, auth_methods: [method], keys: [key], keys_only: true, timeout: 5) do |ssh|
ssh.loop { true }
end
end
rescue SocketError => e
puts "[#{@n_combi}] SOCKET ERROR".red+" #{host} | #{user} | #{port} | #{method} - #{pass} #{key}".blue
rescue Net::SSH::AuthenticationFailed => e
puts "[#{@n_combi}] AUTH ERROR".red+" #{host} | #{user} | #{port} | #{method} - #{pass} #{key}".blue
rescue Net::SSH::ConnectionTimeout => e
puts "[#{@n_combi}] TIMEOUT ERROR".red+" #{host} | #{user} | #{port} | #{method} - #{pass} #{key}".blue
rescue Errno::EHOSTUNREACH => e
puts "[#{@n_combi}] HOST UNREACHABLE".red+" #{host} | #{user} | #{port} | #{method} - #{pass} #{key}".blue
rescue Errno::ECONNREFUSED => e
puts "[#{@n_combi}] CONNECTION REFUSED".red+" #{host} | #{user} | #{port} | #{method} - #{pass} #{key}".blue
rescue Exception => e
puts "[#{@n_combi}] SUCCESS".green+" #{host} | #{user} | #{port} | #{method} - #{pass} #{key}".blue
Kernel.abort(e.message)
end
end
hosts.each do |host|
ports.each do |port|
users.each do |user|
passs.each do |pass|
@n_combi+=1
end
keys.each do |key|
@n_combi+=1
end
end
end
end
puts Unicode::capitalize(@n_combi.to_s).green+' [Potential Combinations]'.blue
hosts.each do |host|
ports.each do |port|
users.each do |user|
passs.each do |pass|
@n_combi-=1
try_connect(host, port, user, pass)
end
keys.each do |key|
@n_combi-=1
try_connect(host, port, user, nil, key)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment