I have the following code that I want to use to ping IP addresses and write to a file. It all works fine, except I can't get it to write to the file.
server = %w'192.168.150.254
192.168.150.251
192.168.120.1
192.168.120.2'
File.open('/test/test2.out','w') do |s|
server.each do |p|
r = `ping -a -n 1 #{p}`
puts r
end
end
Change puts r
to s.puts r
. You're writing to stdout instead of to s
. (See Kernel#puts
and IO#puts
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With