我正在编写一个php脚本,通过ssh连接到vyos路由器,并使用命令备份配置。
show configuration commands。
当我从命令提示符连接时,这个操作就像预期的那样。
ssh vyos@1.1.1.99
Password: ****
$ show configuration
interfaces {
...但是这里有一个我的脚本,我试图使用php做同样的事情。
<?php
//Connect to VyOS virtual router and backup config
$host = '192.168.171.50';
$user = 'vyos';
$pass = 'vyos';
$connection = ssh2_connect($host, 22 );
if (!$connection) die('Connection failed');
if (ssh2_auth_password($connection, $user, $pass)) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
$stream = ssh2_exec($connection, 'show configuration' );
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
// Enable blocking for both streams
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
echo "Output: " . stream_get_contents($stream);
echo "Error: " . stream_get_contents($errorStream);
// Close the streams
fclose($errorStream);
fclose($stream);
exit;代码返回错误。
Invalid command: [show]我最好的猜测是,这与路径或其他环境变量有关。有什么想法吗?我正在使用vyatta/vyos映像来测试这一点。
发布于 2014-08-18 00:42:42
我想你在phpseclib上可能会运气更好。例如:
$ssh = new Net_SSH2('192.168.171.50');
$ssh->login('vyos', 'vyos');
$ssh->read('$');
$ssh->write("show configuration running\n");
echo $ssh->read('$');这也可能奏效:
$ssh = new Net_SSH2('192.168.171.50');
$ssh->login('vyos', 'vyos');
echo $ssh->exec('show configuration running');如果这不起作用,这可能是:
$ssh = new Net_SSH2('192.168.171.50');
$ssh->login('vyos', 'vyos');
$ssh->enablePTY();
echo $ssh->exec('show configuration running');JC的编辑下面:最终工作代码-必须设置终端长度为0或代码挂在寻呼机上。
include('Net/SSH2.php');
$ssh = new \Net_SSH2('192.168.171.50');
$ssh->login('vyos', 'vyos');
$ssh->read('$');
$ssh->write("set terminal length 0\n");
$ssh->read('$');
$ssh->write("show configuration\n");
echo $ssh->read('$');https://stackoverflow.com/questions/25350739
复制相似问题