2016-07-23 7 views
0

私はデータベースに文字列としてbashスクリプトを格納しており、ユーザーの要求に応じて呼び出す必要があります。スクリプトはPHPレベルからリモートマシン上で実行する必要があります。 ssh接続についてPHPを使ってリモートサーバー上でローカルbashスクリプトを実行するには

つのトピックと呼び出しリモートスクリプト:PHPでそれを使用する

  1. How to use SSH to run a shell script on a remote machine?
  2. https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password

そして、二つの方法:

    私は、次のトピックを見つけました
  1. Run Bash Command from PHP
  2. 初の試み:第二

    $connection = ssh2_connect('IP_ADDRESS', 22); 
    ssh2_auth_password($connection, 'user', 'password'); 
    $stream = ssh2_exec($connection, "'bash -s' < ".$script); 
    stream_set_blocking($stream, true); 
    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); 
    $output = stream_get_contents($stream_out); 
    //result => output = empty string 
    

    :例のように

    $old_path = getcwd(); 
    chdir('/path_to_script_directory'); 
    $output = shell_exec("ssh [email protected] 'bash -s' < test"); 
    chdir($old_path); 
    
    or 
    
    $old_path = getcwd(); 
    chdir('/path_to_script_directory'); 
    $output = shell_exec("ssh [email protected] 'bash -s' < ".$script); 
    chdir($old_path); 
    //result => output = null 
    
  3. get result from ssh2_execは、http://php.net/manual/en/function.ssh2-exec.php

は、私は私のSymfony2のアプリケーションに次のコードを使用しようとしました私は上記の2つのケースを試してみました。 ptと文字列スクリプト($スクリプト変数)。第二の選択肢は私によって優先されます。どちらのケースにも簡単なスクリプトが含まれています:

#!/bin/bash 

ifconfig 

ありがとうございます!ここで

+0

を?遭遇したエラー/問題は何ですか? – TenG

+0

両方の例の後ろにコメント "//"を書きました – Krzychu

+0

リモートサーバーの認証ログをチェックすることでSSH接続が成功したかどうか確認できますか? – ghoti

答えて

0

はあなたに、リモートサーバー上の実際のbashシェルを与えるプロジェクトです:https://github.com/merlinthemagic/MTS

次のコマンドをインストールして実行します。あなたの試みの結果だった何

//login to the remote server and get a shell: 

$shellObj  = \MTS\Factories::getDevices()->getRemoteHost('ip_address')->setConnectionDetail('username', 'password')->getShell(); 

//rather than executing your script, just run the command: 

$returnData = $shellObj->exeCmd("ifconfig"); 

echo $returnData; //string containing the interface config of the remote server. 
関連する問題