Ruby-使用ssh检查ping状态featback,通过ssh反击? [英] Ruby - checking ping status featback with ssh, backtick via ssh?
问题描述
在我的项目中,我想编写一个脚本来检查网络中的每个设备是否在线/可访问。我有一个叫做pingtest的方法,它现在可以使用。.
In my project I want to write a script to check if every device in my network is online/reachable. I have a method called pingtest and it works for now..
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与网络中其他设备的会话:
Now I wanted to ping via a ssh session with an other device in my network:
#--------------------------------
require 'net/ssh'
Net::SSH.start(@ip, @user, :password => @password)
#--------------------------------
@ssh = Ssh.new(@config)
@ssh.cmd("ping -c 3 #{@IP}")
ping正常,但是现在如何使用回溯思路确定是否成功?
我考虑过使用sftp连接。.
The ping works fine but how can I use my backtrack idea now to determine if it was succesful or not?
I thought about using a sftp connection..
ping -c 3#{@ IP} => tmpfile.txt =>下载=>检查/比较=>删除
"ping -c 3 #{@IP} => tmpfile.txt" => download => check/compare => delete
(或类似的东西)来检查它是否正确,但我对此没有资格。是否可以像以前一样检查成功状态?
我也尝试过类似的方法。
(or something like that) to check if it was correct, but im not statusfied with that. Is there a possibility to check the success status like I did before?
I also tried something like this..
result = @ssh.cmd("ping -c 3 #{@IP}")
if result.success? == 0 # and so on..
几天前我开始学习红宝石,所以我是新手
I startet learning ruby some days ago, so im a newbie looking forward for your ideas to help me with this problem.
推荐答案
您可以使用Net :: SSH远程运行命令,
You can use Net::SSH to run the command remotely, similarly to what you've already got there.
从运行命令返回的结果
将是所写的内容到 stdout
和 stderr
。
The result
returned from running the command will be whatever is written to both stdout
and stderr
.
您可以使用返回值的内容以检查其是否成功。
You can use the contents of that returned value to check if it was successful or not.
Net::SSH.start(@ip, @user. password: @password) do |ssh|
response = ssh.exec! "ping -c 3 #{@other_ip}"
if response.include? 'Destination Host Unreachable'
close("Host unreachable. Result was: #{result}")
else
puts "\n Ping was successful"
end
end
这篇关于Ruby-使用ssh检查ping状态featback,通过ssh反击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!