2016-04-14 24 views
2

私はssh経由でサーバに接続する必要がありますが、アクセスするにはまず別のsshサーバに接続する必要があります。私はそれらに標準的なパスワードでアクセスします。PHP:ssh2:2番目のsshサーバに接続する方法

は、だから私の手順は以下のとおりです。serverdomain1に接続したときに

ssh [email protected] 

は、私はターミナルで実行します。PHPの

ssh [email protected] 

、私は( 'sshのserverdomain2.com')ssh2_execを使用しようとしました。結果はありません。次にss2_tunnel($ connection、...)も試しました。しかし何も働かなかった。

これは動作しない。

$ssh = ssh2_connect('serverdomain1.com', 22); 
if (ssh2_auth_password($ssh, $user,$pass)) { 
    $stream = ssh_exec($ssh, "ssh serverdomain2.com"); 

    stream_set_blocking($stream, true); 
    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); 
    echo stream_get_contents($stream_out); // <== doesn't work!!! 
} 

これも動作しない。

$ssh = ssh2_connect('serverdomain1.com', 22); 
if (ssh2_auth_password($ssh, $user,$pass)) { 
    $tunnel = ssh2_tunnel($ssh, 'serverdomain2.com', 22); 

    if (!$tunnel) { 
     echo('no tunnel<br/>'); 
    } 
    else { 
     fwrite($tunnel, "echo 1\n"); 
     while (!feof($tunnel)) { 
      echo fgets($tunnel, 128); 
     } 
    }  
} 

トンネルのエコー結果: 「SSH-2.0-OpenSSH_6.6.1p1のUbuntu-2ubuntu2を.4プロトコルのミスマッチ。

PHPからSSH2でどうすればいいですか?

+0

を他の任意の一つは、この上で私を助けてくださいことができますか? thx – Zenslainer

答えて

0
  1. あなたはここでは、この使用するphpinfo

を確認することができますRSSH、PECL、SSH2ライブラリは、サーバー

  • にインストールされていることを確認しますSSH2を使用してサーバーにアクセスするための私の作業コードです。それが助けてくれることを願って!

    <?php 
         $host = 'SERVER_HOST_ADDR'; 
         $port = SERVER_PORT; 
         $username = 'SERVER_USERNAME'; 
         $password = 'SERVER_PASSWORD'; 
         $remoteDir = './home/'; //DIR_PATH 
         $localDir = '/var/www/html/';  //LOCAL_DIR_PATH 
    
         // Make our connection 
         $connection = ssh2_connect($host); 
    
         // Authenticate 
         if (!ssh2_auth_password($connection, $username, $password)) { 
          throw new Exception('Unable to connect.'); 
         } 
    
         // Create our SFTP resource 
         if (!$sftp = ssh2_sftp($connection)) { 
          throw new Exception('Unable to create SFTP connection.'); 
         } 
    
         /** 
          * Now that we have our SFTP resource, we can open a directory resource 
          * to get us a list of files. Here we will use the $sftp resource in 
          * our address string as I previously mentioned since our ssh2:// 
          * protocol allows it. 
          */ 
         $files = array(); 
         $dirHandle = opendir("ssh2.sftp://$sftp/$remoteDir"); 
    
         // Properly scan through the directory for files, ignoring directory indexes (. & ..) 
         while (false !== ($file = readdir($dirHandle))) { 
          if ($file != '.' && $file != '..') { 
           $files[] = $file; 
          } 
         } 
         echo "<pre>";print_r($files); 
        ?> 
    
  • +0

    コードsAcHありがとう。しかし、私がしたいのは、2番目のサーバーにコマンドを送信することです。私は次のようなことをしたい:ssh root @ domain1 => ssh domain2次にdomain2に接続すると、 "tail /var/www/html/mylog.log"のようなコマンドを送ることができる。 – Zenslainer

    1

    私は最近、PHPが実際のBashシェルを取得し、必要に応じてSSH経由でやり取りできるようにするプロジェクトを公開しました。ここをクリックしてください:https://github.com/merlinthemagic/MTS

    このプロジェクトでは、sshを使用してサーバー間でバウンスを維持できます。

    あなたは、単に次のコードを使用しますダウンロードした後:

    //first you get a shell on the first server: 
    $shellObj = \MTS\Factories::getDevices()->getRemoteHost('ip_address1')->setConnectionDetail('username1', 'password1')->getShell(); 
    
    //then build on that first shell, the following way. 
    \MTS\Factories::getDevices()->getRemoteHost('ip_address2')->setConnectionDetail('username2', 'password2')->getShell($shellObj); 
    
    
    //any command executed on the shell will run only on the second host you connected to. 
    $return1 = $shellObj->exeCmd("hostname"); 
    echo $return1;//hostname of the second host you connected to 
    

    //

    +0

    ありがとう。それは魔法です。 – Zenslainer

    +0

    うれしいことです。パッケージのセットアップに問題がありますか? – MerlinTheMagic

    関連する問題