Created
December 12, 2019 16:11
-
-
Save logicminds/b3745dc313b9e3b20b81ca4f670d6aa4 to your computer and use it in GitHub Desktop.
WIP: script to create a 2FA proxy for bulk ssh connections with ease
This file contains 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 'bolt' | |
require 'pry' | |
require 'bolt/config' | |
require 'bolt/inventory' | |
require 'bolt/logger' | |
require "tty-prompt" | |
prompt = TTY::Prompt.new | |
def initialize_bolt | |
Bolt::Logger.initialize_logging | |
@logger = Logging.logger[self] | |
@config = Bolt::Config.default | |
# Bolt::Boltdir.find_boltdir(Dir.pwd) | |
# Bolt::Config.from_boltdir(boltdir, options) | |
@inventory ||= Bolt::Inventory.from_config(@config, nil) | |
@options = {} | |
end | |
# need to convert hostname to ip | |
# TODO | |
def get_ip(name) | |
name | |
end | |
def get_port(ip) | |
ip.split('.').reduce(0) {|acc, num| acc + num.to_i } + (1000 * Random.rand(2..31)) | |
end | |
def proxy_host | |
'localhost' | |
end | |
def port_map | |
@port_map ||= begin | |
m = {} | |
@inventory.node_names.each do |name| | |
p = get_port(name) | |
m[name] = { port: p} | |
end | |
m | |
end | |
end | |
initialize_bolt | |
user = prompt.ask('What is your username?', default: ENV['USER']) | |
code = prompt.mask("What is your 2fa passcode? Hurry!: ") | |
port_map.each do |name, config| | |
puts "ssh -nNT -L #{config[:port]}:#{proxy_host}:22 #{user}@#{name}" | |
end | |
#https://stackoverflow.com/questions/23115619/how-to-use-ansible-with-two-factor-authentication | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment