<?php
$ssh = ssh2_connect('domain.tld');
ssh2_auth_password($ssh, 'username', 'password');
$shell = ssh2_shell($ssh);
echo fread($shell, 1024*1024);
fwrite($shell, "sudo ls -la\n");
$output = fread($shell, 1024*1024);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
fwrite($shell, "password\n");
echo fread($shell, 1024*1024);
}所做的一切只是显示横幅和提示符。它实际上并没有给出ls -la命令的输出。在phpseclib上,它工作得很好。
有什么想法吗?
发布于 2013-03-28 06:30:28
我编写了一个SSH wrapper (https://github.com/bubba-h57/PHP-SSH2)来简化我自己的大量PHP脚本,这些脚本需要通过SSH连接到远程服务器,并且经常遇到这个问题。
我用这个咒语解决了这个问题:
echo <password> | sudo -S <command>这对你来说可能是这样的:
fwrite($shell, "echo $mySudoPassword | sudo -S ls -la\n");我只是在每次使用sudo时输入密码,不管我是否需要它。
使用我自己的包装器,它看起来如下所示:
require_once 'SSH2.php';
// Test Unix
$username = 'someuser';
$password = 'somepwd';
$host = 'somenixhost.com';
$port = 22;
$ssh2 = new My_SSH2($host, $port);
$ssh2->authPassword( $username, $password);
$ssh2->setPrompt(':~#'); // Set initial expected prompt
$ssh2->openShell();
$ssh2->setPrompt("MYCUSTOMSSHPROMPT> "); // Create a unique, easily found prompt
$ssh2->exec("PS1='MYCUSTOMSSHPROMPT> '"); // Execute the command.
echo $ssh2->exec('cd /var/www') . "\n"; // Change directories.
echo $ssh2->exec("echo $password | sudo -S ls -la\n") . "\n"; // Print LS
echo "\n===================Begin History=============\n";
echo $ssh2->getHistory();
$ssh2->disconnect();
echo "\n===================end=============\n";
exit;https://stackoverflow.com/questions/14309165
复制相似问题