954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Confirm connectivity Net::SSH?

Hi.

I'm just trying out Ruby on Rails, and I'm trying to create some application that takes a hostname from the user and then checks if it is connectible through Net::SSH and if it is, it should check if a file is present.

Quite simple, and I also almost reached goal. But I can't seem to implement what should happen if Net::SSH.start fails?

I just get a big Rails error page with param dumps and stuff, but actually I'd just like to do a render :text => 'Hey couldn't connect dewd' or something.

How is that doable? Do I have to make my own error page of some kind?
Thank you.

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

Hi.

I'm just trying out Ruby on Rails, and I'm trying to create some application that takes a hostname from the user and then checks if it is connectible through Net::SSH and if it is, it should check if a file is present.

Quite simple, and I also almost reached goal. But I can't seem to implement what should happen if Net::SSH.start fails?

I just get a big Rails error page with param dumps and stuff, but actually I'd just like to do a render :text => 'Hey couldn't connect dewd' or something.

How is that doable? Do I have to make my own error page of some kind? Thank you.

Hope this would help:

around_filter :catch_exceptions

private
def catch_exceptions
yield
rescue => exception
logger.debug "Caught exception! #{exception}"
render :text => 'Hey couldn't connect'
raise
end

sourcebits
Junior Poster
102 posts since Dec 2008
Reputation Points: 16
Solved Threads: 14
 

I'm able to do what I want to achieve with your code, but it also overrides all other errors, making it hard to debug - also seems to override my 'Insufficient admin priviliges' system.

Is there some way to tune the functionality so that it render :text if it is the Net::SSH related error, or yield if not?

Thank you :)

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

I'm able to do what I want to achieve with your code, but it also overrides all other errors, making it hard to debug - also seems to override my 'Insufficient admin priviliges' system.

Is there some way to tune the functionality so that it render :text if it is the Net::SSH related error, or yield if not?

Thank you :)

You can have this filter to only to a particular action

before_filter :catch_exceptions, :only => [:action_name]

sourcebits
Junior Poster
102 posts since Dec 2008
Reputation Points: 16
Solved Threads: 14
 

Thank you, works like a charm.
Unfortunately the action SSH is called from, is giantic with lots of different functionality depending on post params, but I'll just make a new action dedicated for the SSH connection and then call that action from the original action.

Thanks again ;)

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

Thank you, works like a charm. Unfortunately the action SSH is called from, is giantic with lots of different functionality depending on post params, but I'll just make a new action dedicated for the SSH connection and then call that action from the original action.

Thanks again ;)

That's great to hear :)

sourcebits
Junior Poster
102 posts since Dec 2008
Reputation Points: 16
Solved Threads: 14
 

Actually it seems that the error is raised from my original action although moving it... Guess I'll have to live with that.

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You