文件读取问题
f = File.open("/test/serverlist.txt", "r")
list = f.readlines
list.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
它读取 serverlist
文件,并返回一个字符串。 serverlist
文件包含以下 IP 地址:
192.168.150.254
192.168.120.2
f = File.open("/test/serverlist.txt", "r")
list = f.readlines
list.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否希望读取文件中的每一行,然后执行类似这样的操作。
Are you looking to read each line from the file and then do something with like this.
我认为您不需要迭代服务器线路本身,并且添加了一些样式 mods 并更改了 ping(1) 参数,我建议...
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...
只需使用 b.write 代替 b.puts
Just use b.write in place of b.puts
如果你使用的是linux,你可以直接使用
它并很好地完成它
..也许
在上面的行之前添加,这样每次你调用这个文件时都会重置文件
if you're using linux you could just go for
and be done with it
well .. maybe add
before the above line so the file gets reset every time you call this