0

I've a problem with PHP and the SSH-Extension/Net-SSH-Libary. I use it for sending commands to a NetApp-Filer. So I want to create/delete Volumes on the filer. Creation for Volumes is no problem.

But when I want to delete them, the filer ask's for an confirmation ("Are you sure you want to delete.. y/n") and I can't give the NetApp this information. For every exec-Command ist starts a new session.

Is it possible to run more commands in the same session or give them a confirmation of some commands?

My Code (only Volume delete):

<?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');

?>

Thank's in advance!

Greetings

1 Answer 1

1

I see a few possible solutions:

  1. use \n :

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

    Or create a script with your commands, for example script.sh and save it in UNIX format:

    cd mydir
    ./script
    

    Then exec the script:

    $script = file_get_contents("script.sh");
    $ssh->exec($script);
    
  2. Use either a ; or a && to separate the commands.

    ssh2_exec($connection, 'command1 ; command2');   //run both uncondtionally)
    ssh2_exec($connection, 'command1 && command2');  //run command2 only if command1 succeeds
    
  3. Use stream_set_blocking() like this:

    $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);
    }
    

    You will find all the output in the array $output.

Reference

Sign up to request clarification or add additional context in comments.

3 Comments

I do not think you have tested your answer.
@MarcinOrlowski why the downvote? This has been tested. Kindly check the reference too.
I did not like the answer in the way it formerly was posted. Now it's better, so I retracted my downvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.