Lizinh
Lizinh

Reputation: 71

Insert the enable password ssh2 PHP

When I execute the command enable with ssh2_exec(), I can't load the page because it is waiting for the password.
I tried to do ssh2_exec(connection,'password') and the problem persists.
My question is how to put the enable password ? here is my code :

 <?php

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22

if(!($con = ssh2_connect("9.0.0.1", 22))){
    echo "fail: unable to establish connection\n";
} else { 
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con, "mehdi", "123")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";

        // execute a command
        if (!($stream = ssh2_exec($con, 'enable'))) {
            echo "fail: unable to execute command\n";
        } else {  
            // collect returning data from command
            stream_set_blocking($stream, true);
            ssh2_exec($con, 'PASSWORD');
            $data = "";

            while ($buf = fread($stream,4096)) {

                $data .=PHP_EOL. date("Y-m-d H:i:s ").$buf;
                $data .= PHP_EOL."------------------------------------------------------------------------------------------------------------------------\n";
            }
            echo $data;
            $fh = fopen("log".date("Y-m-d").".txt", 'a+') or die("can't open file");
            fwrite($fh, $data);
            fclose($fh);
            fclose($stream);
        }
    }
}
?>

ps: i am trying to configure a Cisco router

Upvotes: 0

Views: 2079

Answers (1)

lambdasemperfi
lambdasemperfi

Reputation: 11

what about this (with phpseclib)?

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('9.0.0.1');
$ssh->login('mehdi', '123');

$ssh->setTimeout(3);

$ssh->enablePTY();
$ssh->exec('enable');
$ssh->read('password:'); // or whatever the password prompt is
$ssh->write("password\m"); // or maybe without the \n
echo $ssh->read();

Upvotes: 1

Related Questions