script for collecting outputs from rex commads to csv via hammer
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 'json' | |
require 'csv' | |
unless ARGV.first | |
puts "Usage: HAMMER_USER=admin HAMMER_PASSWORD=changeme rex_outputs.rb JOB_INVOCATION_ID" | |
exit 1 | |
else | |
@job_invocation_id = ARGV.first | |
end | |
@hammer_user = ENV['HAMMER_USER'] || 'admin' | |
@hammer_password = ENV['HAMMER_PASSWORD'] || 'changeme' | |
def hammer(args) | |
command = "hammer -u #{@hammer_user} -p #{@hammer_password} #{args}" | |
`#{command}` | |
end | |
def hammer_yaml(args) | |
data = hammer("--output json #{args}") | |
if $?.success? | |
JSON.parse(data) | |
else | |
puts data | |
exit 2 | |
end | |
end | |
data = hammer_yaml("job-invocation info --id #{@job_invocation_id}") | |
hosts = data['Hosts'].lines.map { |l| l[/\S+$/] }.compact | |
out = CSV.generate do |csv| | |
hosts.each do |host| | |
csv << [host, hammer("job-invocation output --id #{@job_invocation_id} --host #{host}")] | |
end | |
end | |
puts out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment