Search code examples
rubyautomationtelnet

Send a telnet command and kill connection without waiting for a response


I am trying to make a telnet connection and send a command which forces a reboot and then kill the connection. So far i have:

require 'net/telnet'
host = 192.168.20.11
connection = Net::Telnet::new("Host" => host)
connection.login("User")
connection.cmd("Reboot")

This works and achieves what i want it to, however the program then waits for feedback from the reboot command which there is none because it is rebooting, the program therefore fails. Is there a way i can send the command and then close the connection without waiting for feedback? This would allow me to continue through the process.


Solution

  • Unfortunately, I don't see any way documented to handle this. You might be able to fake it with something like this:

    begin
      connection.cmd("String" => "Reboot", "Timeout" => 0.1)
    rescue # I'm not sure what that will raise, but you should rescue that specific error here
      nil
    end
    

    I have to ask though, is there any way you can use ssh as opposed to telnet?