当我使用ssh2_exec()
执行命令enable时,我无法加载页面,因为它正在等待密码。
我试图做ssh2_exec(connection,'password')
并且问题仍然存在
我的问题是如何设置启用密码?
这是我的代码:
<?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:我正在尝试配置Cisco路由器
答案 0 :(得分:1)
这个怎么样(用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();