Hi
i am trying to automate cacti graph
after i add the device, its give me a result like bellow
Success - new device-id: (18)
problem is : how will i just get number 18 ??
thanks for help
Hi
i am trying to automate cacti graph
after i add the device, its give me a result like bellow
Success - new device-id: (18)
problem is : how will i just get number 18 ??
thanks for help
On 8/15/2012 9:43 PM, Fosiul A. wrote:
thanks for help
a regular expression match of a number can match like that
/((\d))/
Regards,
Eliezer
hi
I am very new to ruby
what would be the syntax ??
i get output like this
uk-xxxxxxx-Z15A14
Adding uk-xxxxx-Z15A14 (uk-xxxx-Z15A14) as “Generic SNMP-enabled Host”
using SNMP v1 with community “public”
Success - new device-id: (26)
i will be able to parse the line by
cmd each
cmd.each do |item|
if ( item =~ /((\d))/ )
getnumber = item
end
end
will it work ??
sorry the code i wrote that does not do anything
syntax help would be really appreciate
You need to also make sure that you get the right line, not just pick
out the any digits
DATA.each do |line|
if line =~ /^Success - new device-id: ((\d+))/
puts $1
end
end
END
uk-xxxxxxx-Z15A14
Adding uk-xxxxx-Z15A14 (uk-xxxx-Z15A14) as “Generic SNMP-enabled Host”
using SNMP v1 with community “public”
Success - new device-id: (26)
The $1 is the captured value, the (\d+) part.
On 8/15/2012 10:00 PM, Fosiul A. wrote:
will it work ??
this output should be a string and is not enumerable so you can use each
but you can use “each_line” to inspect each line but “getnumber = item”
will not extract the number.
and instead of iterating over each line you can use one regex on the
whole string like:
cmd =~ /((\d+))/
you must use “\d+” to match one or more numbers
the =~ will give bad results on a whole string and you better use the
scan method.
cmd.scan /((\d+))/
this will return array of strings array
so this:
(cmd.scan /((\d+))/)[0][0]
will give you the result with a one liner.
and i just seen that Robert gave you a nice solution.
Regards,
Eliezer
On Wed, Aug 15, 2012 at 9:30 PM, Peter H.
[email protected] wrote:
You need to also make sure that you get the right line, not just pick
out the any digitsDATA.each do |line|
if line =~ /^Success - new device-id: ((\d+))/
puts $1
end
Alternatively
num = line[/^Success - new device-id: ((\d+))/, 1] and puts num
I find the solution with String#[] very elegant.
Btw, if there is always just one number this shorter solution will
work as well - but is not so robust against other inputs:
num = line[/\d+/]
Kind regards
robert
Peter H. wrote in post #1072475:
You need to also make sure that you get the right line, not just pick
out the any digitsDATA.each do |line|
if line =~ /^Success - new device-id: ((\d+))/
puts $1
end
endEND
uk-xxxxxxx-Z15A14
Adding uk-xxxxx-Z15A14 (uk-xxxx-Z15A14) as “Generic SNMP-enabled Host”
using SNMP v1 with community “public”
Success - new device-id: (26)The $1 is the captured value, the (\d+) part.
Hi thanks
but there is a problem , I have modified this as bellow
DATA.each do |line|
if line =~ /^Success - new device-id: ((\d+))/
device_id = $1
end
end
puts #{device_id}
and this does not return anything …
how can i use the value outside of the loop ??
i will try rest of the solution as well.
thanks you guys …
#!/usr/bin/ruby
host=ARGV[0]
puts host
cmd=php -q /var/lib/cacti/cli/add_device.php --description="#{host}" --ip="#{host}" --template=1 --community="public"
cmd.each do |line|
if line =~ /^Success - new device-id: ((\d+))/
device_id=$i
end
end
puts device_id ======== this does not give anything …
commands.
Fosiul A. wrote in post #1072480:
but there is a problem , I have modified this as bellow
DATA.each do |line|
if line =~ /^Success - new device-id: ((\d+))/
device_id = $1
end
endputs #{device_id}
and this does not return anything …
how can i use the value outside of the loop ??
put:
device_id = nil
before the loop. It’s one of the ruby scoping rules that if you first
assign to a variable inside a block, it disappears at the end of that
block.
Eliezer C. wrote in post #1072477:
On 8/15/2012 10:00 PM, Fosiul A. wrote:
will it work ??
this output should be a string and is not enumerable so you can use each
but you can use “each_line” to inspect each line but “getnumber = item”
will not extract the number.
and instead of iterating over each line you can use one regex on the
whole string like:
cmd =~ /((\d+))/you must use “\d+” to match one or more numbers
the =~ will give bad results on a whole string and you better use the
scan method.
cmd.scan /((\d+))/
this will return array of strings array
so this:
(cmd.scan /((\d+))/)[0][0]
will give you the result with a one liner.
and i just seen that Robert gave you a nice solution.Regards,
Eliezer
Hi thanks
this works perfec
host=ARGV[0]
puts host
cmd=php -q /var/lib/cacti/cli/add_device.php --description="#{host}" --ip="#{host}" --template=1 --community="public"
device_id = (cmd.scan /((\d+))/)[0][0]
puts device_id
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs