2012-07-09 3 views
0

연결이 성공적으로 설정되면 내 컨트롤러에서 ssh 연결을 만들고 뷰를 표시합니다. 컨트롤러ruby ​​on rails ssh 연결 유효성 검사

<%= form_for :check_validity, :method => "get", :url => {:action => "check_validity"} do |f| %> 
//enters user name and password here and submits 
<% end%> 

체크 유효 조치 : : 사용은 자신의 정보를 입력합니다 곳 나는 연결이 여기에 를 설립 내보기하지 않은 경우 'denied_access'보기로 리디렉션 내 행동이 필요

def check_validity 
    @USER = params[:check_validity][:username] 
    @PASS = params[:check_validity][:password] 
    @DOMAIN = params[:check_validity][:domain] 
    @HOST = 'hostname' 

    Net::SSH.start(@HOST, @USER, :password => @PASS) do|ssh| 

    @result = ssh.exec!("/lclapps/oppen/http/ldap-group-list-2.4.3.py #{@DOMAIN} #{@USER} #{@PASS}") 
    end 
     respond_to do |format| 
     if(@result =~ /Informatica Developer/) 
      format.html{redirect_to display_command_list_path({:username => @USER})} 
     else 
      format.html{redirect_to denied_access_path} 
     end 
     end 
    end 

여기서 연결이 설정되고 resutl에서 Informatica Developer 문자열을 확인하고 display_command_list_path으로 리디렉션합니다. 하지만이 문자열을 확인하기 전에 연결이 설정되었는지 여부를 확인하고 연결이 설정되지 않은 경우 denied_access_path로 리디렉션하고 싶습니다. 어떻게 할 수 있습니까?

답변

2

Net::SSH.start으로 연결을 시도하면 자격 증명이 유효하지 않은 경우 Net::SSH::AuthenticationFailed 예외가 발생합니다.

begin 
    Net::SSH.start(@HOST, @USER, :password => @PASS) do |ssh| 
    # ... 
    end 
rescue Net::SSH::AuthenticationFailed => e 
    # redirect to denied_access_path 
end 

# your respond_to block ... 

나는 또한 @HOST가 나쁜 경우, 그것은 SocketError을 올릴 것이다, 언급해야한다. 위의 코드에 두 번째 구조를 추가 할 수 있습니다.

+0

내 현재 프로젝트에서 호스트가 하드 코드 된대로 잘못된 호스트를 가질 가능성은 없습니다. 하지만 어쨌든 소켓 오류도 추가됩니다. – user1455116