Skip to content

Instantly share code, notes, and snippets.

@iNecas
Created August 10, 2017 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iNecas/b7a76bb9e7a7c1df3153b26d26023139 to your computer and use it in GitHub Desktop.
Save iNecas/b7a76bb9e7a7c1df3153b26d26023139 to your computer and use it in GitHub Desktop.
script for collecting outputs from rex commads to csv via hammer
#!/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