Last active
August 10, 2017 20:27
-
-
Save cotocisternas/53ba7c31c1e00ad1939a4f7c73bed3ae to your computer and use it in GitHub Desktop.
try_ssh
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
| #!/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