rahrahruby
rahrahruby

Reputation: 693

ruby output format

I have the following code, which works fine. The problem is that I get the following output:

Device ID: SEP1C17D3415659

IP address: 10.2.3.101

I would like to get:

SEP1C17D3415659 10.2.3.101

Thanks for your help


require 'net/telnet'
require '/apps/dateformat'


@tdate = Formatdate.new.mydate

hosts = %w"SW3"


  hosts.each do |hostname|

        tn = Net::Telnet::new("Host" => "#{hostname}",
                                     "Timeout" => 10000,
                                     "Prompt" => /[$%#>] \z/n)

        tn.cmd('String' =>'user' , 'Match'=>/Password:/) { |c| puts c }
        tn.cmd('String' =>'password', 'Match'=>/#/) { |c| puts c }
        tn.cmd('String' =>'terminal length 0', 'Match'=>/#/) { |c| puts c }


        File.open("/agents/#{hostname}-#{@tdate}-agents.txt",'w') do |o|
            run=tn.cmd('String' =>'sh cd ne de | inc Device ID: | IP address:', 'Match'=>/#/) { |c| puts c }
                        run.each_line do |re|
                        mac = re.match /Device ID: ([\S]+)/
                        #ip = re.match /([\S]+) address/
                        ip = re.match /IP address: ([\S]+)/
                        o.puts mac
                        o.puts ip
                    end




        end

end

Upvotes: 3

Views: 524

Answers (2)

matt
matt

Reputation: 79723

You're correctly specify the grouping you want for your regular expression, but you're not using it when printing it out.

Your regular expression is:

mac = re.match /Device ID: ([\S]+)/

and this matches the whole line Device ID: SEP1C17D3415659, putting the part you want (SEP1C17D3415659) into a group so you can get at it later. However you then print it out using

o.puts mac

This gives the whole match, not just the group. What you want is this instead:

o.puts mac[1]

which specifies just the group.

Also, as Cody says, puts adds a newline on each call. If you want both matches to print on the same line try something like:

o.print "#{mac[1]} #{ip[1]}\n"

instead.


Edit:

I'm not familiar with net/telnet or the command you're running, but it looks like you're running a certain command, then filtering for certain lines and then printing some info from those lines. If this is the case, then any lines that don't match will give you nil for mac and ip so you'll get "undefind method []" on those lines.

If this is the case you can simply use:

o.print "#{mac[1]} #{ip[1]}\n" unless mac.nil?

You might find it worth while to restructure your code slightly to better express what you're doing, by combining the two regular expressions into one. Without knowing the structure of the actual lines I can't create the real expression you'll need, but it would look something like this:

run.each_line do |line|
  match = line.match /^Device ID: ([\S]+), IP address: ([\S]+)$/ #these are the lines we want
  next unless match #skip lines we don't want
  o.print "#{match[1]} #{match[2]}\n" #print the details we're interested in
end

Upvotes: 1

Cody
Cody

Reputation: 3764

puts automatically prints a new line after each output. Simply put both on the same line.

outputString = mac + " " + ip
o.puts outputString

If you have other output functions available such as print, it will function without putting a newline between the two statements.

Upvotes: 3

Related Questions