2016-05-25 12 views
3

SSHを介してVPSに接続するためにRunTimeExceptionを処理しようとしています。私はSSH接続エラーを処理することができません

SSH経由でVPSに接続するコードは次のとおりです。

$server_ip = Input::get('server_ip'); 
$password = Input::get('password'); 

$validator = Validator::make(
    ['server_ip' => $server_ip], 
    ['server_ip' => 'ip|required|unique:servers'], 
    ['password' => $password], 
    ['password' => 'required|confirmed'] 
); 

if(!$validator->fails()) 
{ 
    Config::set('remote.connections.production.host',$server_ip); 
    Config::set('remote.connections.production.username','root'); 
    Config::set('remote.connections.production.password',$password); 
    Config::set('remote.connections.production.root','/'); 

    $command = 'mysql --version'; 

    $connection_status = SSH::run($command, function($line) 
    { 
     $this->output = $line.PHP_EOL; 
    }); 

    if(!$connection_status) 
     return $this->output; 
    else 
     return view('dashboard.add_server'); 
} 
else 
    return "Validation failed..."; 

これはLaravelで書かれています。 SSH接続でエラーが発生した場合は、add_serverという名前のビューにリダイレクトする必要があります。しかし、リダイレクトの代わりに例外が発生しています。リダイレクションの代わりに毎回得られるエラーです。私が欲しいもの

enter image description here

は、SSH経由VPSためにいくつかのエラー接続がある場合、それは私がコードで間違っているものを知ってみましょう

add_serverという名前を見るために私をリダイレクトする必要があり、です。

答えて

3
try 
{ 
    $connection_status = SSH::run($command, function($line) 
    { 
     $this->output = $line.PHP_EOL; 
    }); 

    if(!$connection_status) 
     return $this->output; 
} 
catch(\RunTimeException $e) 
{ 
    return view('dashboard.add_server'); 
} 
1

あなたは$connection_statusが成功した接続時に偽の値を取得することになっているが、離れているから、それが動作するかどうか、私は知りませんtry-catch

try 
{ 
    $connection_status = SSH::run($command, function($line) 
    { 
     $this->output = $line.PHP_EOL; 
    }); 

    if(!$connection_status) 
     return $this->output; 
} 
catch(RunTimeException $e) 
{ 
    return view('dashboard.add_server'); 
} 

を使用する必要があります。

+0

私はこのコードを試しました。それでも、リダイレクトなしで同じ例外が発生しています... –

関連する問題