2017-04-04 108 views
0

我遇到了PHP和SSH-Extension/Net-SSH-Libary的问题。我用它将命令发送到NetApp-Filer。所以我想创建/删除文件管理器上的卷。创建卷是没有问题的。PHP/SSH:在同一会话中执行多个命令(确认)

但是,当我想删除它们时,文件管理器询问是否确认(“您确定要删除.. y/n”),我无法向NetApp提供此信息。对于每个exec-Command ist开始一个新的会话。

是否有可能在同一个会话中运行更多的命令或给他们一些命令的确认?

我的代码(仅卷删除):

<?php 
include('Net/SSH2.php'); 
      $ssh = new Net_SSH2('172.22.31.53'); 
      if (!$ssh->login('admin', '12Test')) { 
       exit('Login Failed'); 
      } 

      echo $ssh->exec("vol unmount $row->name"); 
      sleep(1); 
      echo $ssh->exec("vol offline $row->name"); 
      sleep(1); 
      echo $ssh->exec("vol delete $vol_name \n y"); 
      $loesch = mysqli_query($db, "DELETE FROM volumes WHERE id = '$id'"); 
      header('Location: splash.html'); 

?> 

感谢的提前!

问候

回答

1

我看到几个可能的解决方案:

  1. 使用\n

    $ssh->exec("cd mydir\n./script"); 
    

    或者创建拥有您的命令的脚本,例如script.sh并将其保存在UNIX格式:

    cd mydir 
    ./script 
    

    然后EXEC脚本:

    $script = file_get_contents("script.sh"); 
    $ssh->exec($script); 
    
  2. 使用一个;&&到的命令中分离出来。

    ssh2_exec($connection, 'command1 ; command2'); //run both uncondtionally) 
    ssh2_exec($connection, 'command1 && command2'); //run command2 only if command1 succeeds 
    
  3. 使用stream_set_blocking()这样的:

    $cmds = [ 'ls', 'ps ux' ]; 
    $connection = ssh2_connect('127.0.0.1', 22); 
    ssh2_auth_password($connection, 'username', 'password'); 
    $output = []; 
    foreach ($cmds as $cmd) { 
        $stream = ssh2_exec($connection, $cmd); 
        stream_set_blocking($stream, true); 
        $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); 
        $output[] = stream_get_contents($stream_out); 
    } 
    

    你会发现阵列$output中所有的输出。

Reference

+0

我不认为你已经测试你的答案。 –

+0

@MarcinOrlowski为什么downvote?这已经过测试。请检查参考。 –

+0

我不喜欢以前发布的答案。现在好多了,所以我收回了我的downvote。 –