0

I have some sample code I can execute for our Nexpose server and I need to do some mass asset tagging. Here is an example of the code.

    nsc = Nexpose::Connection.new('your_nexpose_instance', 'username', 'password', 3780)
    nsc.login
    criterion = Nexpose::Tag::Criterion.new('IP_RANGE', 'IN', ['ip1', 'ip2'])
    criteria = Nexpose::Tag::Criteria.new(criterion)
    tag = Nexpose::Tag.new("tagname", Nexpose::Tag::Type::Generic::CUSTOM)
    tag.search_criteria = criteria
    tag.save(nsc)

I have a file called with the following data.

ip1,ip2,tagname
192.168.1.1,192.168.1.255,Workstations

How would I go about running a for loop and using the CSV to quickly process the above code? I have no experiance with Ruby and tried to follow some example but I'm confused at this point.

1
  • The CSV documentation is very clear and contains a number of examples showing how to read from a file. What about that documentation didn't explain the solution to the question? What did you try? Your example code shows no use of the CSV class so it looks like you're asking us to write your code for you, which we don't do. Please fill in the blanks for us using a minimal example of what you tried. I'd recommend reading "How to Ask" including the links at the bottom of the page. Commented Feb 4, 2016 at 17:31

3 Answers 3

1

There's a CSV library in Ruby's standard lib collection that you can use.

Basic example based on your code example and data, not tested:

require 'csv'

nsc = Nexpose::Connection.new('your_nexpose_instance', 'username', 'password', 3780)
nsc.login

CSV.foreach("path/to/file.csv", headers: true) do |row|
  criterion = Nexpose::Tag::Criterion.new('IP_RANGE', 'IN', [row['ip1'], row['ip2'])
  criteria = Nexpose::Tag::Criteria.new(criterion)
  tag = Nexpose::Tag.new(row['tagname'], Nexpose::Tag::Type::Generic::CUSTOM)
  tag.search_criteria = criteria
  tag.save(nsc)
end
Sign up to request clarification or add additional context in comments.

3 Comments

This seems simple and I understand how to use csv.foreach but i don't get how you break up each entry and use them variables. I don't think your example here states that. Does it do it based on what the header of the column is? I dont think so but I may not understand. I'm new to ruby.
Read the CSV documentation, especially spending time on the examples. Try them in IRB. meta.stackoverflow.com/q/261592/128421
I did. Thank you. This was very helpful.
0

I made a directory with input.csv and main.rb

input.csv

ip1,ip2,tagname
192.168.1.1,192.168.1.255,Workstations

main.rb

require "csv"

CSV.foreach("input.csv", headers: true) do |row|
  puts "ip1: #{row['ip1']}"
  puts "ip2: #{row['ip2']}"
  puts "tagname: #{row['tagname']}"
end

the output is

ip1: 192.168.1.1
ip2: 192.168.1.255
tagname: Workstations

I hope this can help. If you have questions I'm here :)

Comments

0

If you just need to loop through each line of the file and fire that chunk of code for each line, you could do something like this:

file = Net::HTTP.get(URI(<whatever_your_file_name_is>))
index = 0

file.each_line do |line|
  next if index == 0
  index += 1
  
  split_line = line.split(',')
  ip1 = split_line[0]
  ip2 = split_line[1]
  tagname = split_line[2]

  nsc = Nexpose::Connection.new('your_nexpose_instance', 'username', 'password', 3780)
  nsc.login
  criterion = Nexpose::Tag::Criterion.new('IP_RANGE', 'IN', [ip1, ip2])
  criteria = Nexpose::Tag::Criteria.new(criterion)
  tag = Nexpose::Tag.new(tagname, Nexpose::Tag::Type::Generic::CUSTOM)
  tag.search_criteria = criteria
  tag.save(nsc)
end

NOTE: This code example is assuming that the CSV file is stored remotely, not locally.

ALSO: In case you're wondering, the next if index == 0 is there to skip your header record.

UPDATE

To use this approach for a local file, you can use File.open() instead of Net::HTTP.get(), like so:

file = File.open(<whatever_your_file_name_is>).read

Two things to note:

  1. Make sure you use the fully-qualified name of the file - i.e. ~/folder/folder/filename.csv instead of just filename.csv.
  2. If the files you're going to be loading are enormous, this might not be an ideal approach because it's actually reading the whole file into memory. But considering your file only has 3 columns, you'd have to have an extreme number of rows in the file for this to be an issue.

10 Comments

The file is sitting in the same directory as the ruby script. What's the difference between the way you are doing it and @zwippie?
Why not use 'csv' in this case? Also, when it comes to passing variables do i need to remove the ' ' when using the ip1,ip2,tagname? In some languages you're still required to leave them in quotes since its a string.
Yep, you could use the csv library as well - just another option. The csv library is just a wrapper for File so, at it's core, it's doing the same things. Can you specify where you want the pass in the ip1, ip2, and tagname variables? I'll update my answer accordingly to show you exactly how to use them.
Sure, its simple. Do you see 'ip1', 'ip2' and "tagname" - thats where they need to be passed.
Don't be naive and assume that split_line = line.split(',') is all it takes to parse a CSV record. The CSV format is much more complex than that; A simple split can bite you badly when encountering embedded commas.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.