Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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