【发布时间】:2015-04-29 06:34:13
【问题描述】:
在我的项目中,我想编写一个脚本来检查我网络中的每个设备是否都在线/可访问。我有一个叫做 pingtest 的方法,它现在可以工作了..
def pingtest(destination)
system("ping -n 2 #{destination}")
if $? == 0 #checking status of the backtick
puts "\n Ping was successful!"
else
close("Device is unreachable. Check the config.txt for the correct IPs.")
#close() is just print & exit..
end
end
现在我想通过 ssh 会话与网络中的其他设备进行 ping:
#--------------------------------
require 'net/ssh'
Net::SSH.start(@ip, @user, :password => @password)
#--------------------------------
@ssh = Ssh.new(@config)
@ssh.cmd("ping -c 3 #{@IP}")
ping 工作正常,但我现在如何使用我的回溯想法来确定它是否成功?
我考虑过使用 sftp 连接..
"ping -c 3 #{@IP} => tmpfile.txt" => 下载 => 检查/比较 => 删除
(或类似的东西)来检查它是否正确,但我不知道它是否正确。是否有可能像以前一样检查成功状态?
我也试过这样的..
result = @ssh.cmd("ping -c 3 #{@IP}")
if result.success? == 0 # and so on..
我几天前开始学习 ruby,所以我是新手,期待您的想法来帮助我解决这个问题。
【问题讨论】: