0

I'm trying to write a script that will take ip addresses from a host file, and username info from a config file. I'm obviously not holding a the file-name as a proper hash/value.

What should my File.new(options[:config_file], 'r').each { |params| puts params } be calling? I've tried what it is currently set too, and

File.new(config_file, 'r').each { |params| puts params }, as well as File.new(:config_file, 'r').each { |params| puts params } with no luck.

Should I be doing something different all together? Like load(filename = nil)?

options = {}

opt_parser = OptionParser.new do |opt|
  opt.banner = 'Usage: opt_parser COMMAND [OPTIONS]'
  opt.on('--host_file','I need hosts, put them here') do |host_file|
    options[:host_file] = host_file
  end
  opt.on('--config_file', 'I need config info, put it here') do |config_file|
    options[:config_file] = config_file
  end
  opt.on('-h', '--help', 'What your looking at') do |help|
    options[:help] = help
    puts opt
  end
end

opt_parser.parse!

if options[:config_file]
  File.new(options[:config_file], 'r').each { |params| puts params }
end

if options[:host_file]
  File.new(options[:host_file], 'r').each { |host| puts host }
end
insecure-IT
  • 2,068
  • 4
  • 18
  • 26
  • What format are your files in? I'd be reaching straight for YAML or CSV with so,mething like this. YAML gives you hash. From CSV you can get a hash. – Tony Hopkinson Feb 15 '14 at 11:59
  • Right now just standard txt files, but I'm open to options. Host files is just ip addresses (or host names), one per line. Config file is username=un, password=pw, one per line. I don't like storing passwords in a clear text file either, but I'm not sure what else I could do in this area. – insecure-IT Feb 15 '14 at 12:06
  • Have look at this. http://stackoverflow.com/questions/6012930/read-lines-of-a-file-in-ruby – Tony Hopkinson Feb 15 '14 at 13:22

1 Answers1

0

Parsing the hosts file

You can write your own parser or use a gem already implementing one.

Example using the "hosts" gem: (you need to install it)

require 'hosts'

hosts = Hosts::File.read('/etc/hosts')

entries = hosts.elements.select{ |element| element.is_a? Hosts::Entry }
addresses = Hash[entries.map{ |entry| [entry.name, entry.address] }]

# You should get a hash of entry names and addresses
# {"localhost"=>"127.0.0.1", "ip6-localhost"=>"::1"}

Parsing the config file

A common way to store configuration is to use YAML files.

Considering the following YAML file (in '/tmp/config.yml'):

username: foo
password: bar

You can parse this config file using the YAML module:

require 'yaml'

config = YAML.load_file('config.yml')

# You should get a hash of config values
# {"username"=>"foo", "password"=>"bar"}

If you don't want your password stored in plain text in a config file, you can:

  • ask the password at runtime if your context allow that
  • use environment variable to store the password and retrieve it at runtime

Edit:
If you only need to extract your hostnames from a text file, considering one hostname per line, you can use something like hostnames = IO.readlines("config.yml").map{ |line| line.chomp } to get an array of hostnames. You can after iterate through this array to do your operations.

www.ruby-doc.org/core-2.1.0/IO.html#method-i-readline

kurt
  • 1
  • 3
  • Thanks for the `Parsing the config file` example, that helps greatly. As for the `hosts` file part, I'm not using the `operating system's host files`, but a file (could be any name) that has ip addresses per line. I would like to use each line as `@hostname` in the following script `session = Net::SSH.start(@hostname, @username, :password => @password, :encryption => 'aes256-cbc', :host_key => 'ssh-rsa')` – insecure-IT Feb 15 '14 at 13:57