开发者

File reading problem

开发者 https://www.devze.com 2023-03-02 19:34 出处:网络
f = File.open(\"/test/serverlist.txt\", \"r\") list = f.readlines li开发者_Python百科st.each do|servers|
f = File.open("/test/serverlist.txt", "r")
list = f.readlines
li开发者_Python百科st.each do|servers|
  File.open('/test/results.txt','w') do |b|
  servers.each do |p|
    r = `ping -n 1 #{p}`
    b.puts r
  end
end

It reads the serverlist file, and returns a string. The serverlist file contains the following IP addresses:

192.168.150.254
192.168.120.2


Are you looking to read each line from the file and then do something with like this.

fout = File.open('/test/results.txt','w')
File.open("/test/serverlist.txt", "r").each_line do |server|
   server.chomp!
   r = `ping -n 1 #{server}`
   fout.puts r
end


I don't think you will need to iterate over the server line itself, and with a few style mods added and ping(1) arguments changed, I would suggest...

open 'serverlist.txt', 'r' do |f|
  open '/tmp/results.txt', 'w' do |b|
    f.readlines.each do |server|
      b.puts `ping -c 1 -t 1 #{server}`
    end
  end
end


Just use b.write in place of b.puts


if you're using linux you could just go for

File.open("serverlist.txt").each { |addy| `echo "#{`ping -c 1 #{addy}`}" >> result.txt` }

and be done with it

well .. maybe add

`echo "# server-availability" > result.txt`

before the above line so the file gets reset every time you call this

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号