phpseclib ssh2启用tty
我的ssh脚本:phpseclib ssh2启用tty,php,linux,phpseclib,Php,Linux,Phpseclib,我的ssh脚本: include('Net/SSH2.php'); set_include_path(getcwd()); $ssh_host = "myserver.com"; if($ssh_host != null){ $ssh_user = globals::$ssh_logins[$ssh_host]; $ssh = new Net_SSH2($ssh_host); //$ssh->enablePTY(); if (!$ssh->log
include('Net/SSH2.php');
set_include_path(getcwd());
$ssh_host = "myserver.com";
if($ssh_host != null){
$ssh_user = globals::$ssh_logins[$ssh_host];
$ssh = new Net_SSH2($ssh_host);
//$ssh->enablePTY();
if (!$ssh->login($ssh_user['username'], $ssh_user['password'])) {
echo $ssh->isConnected() ? 'bad username or password' : 'unable to establish connection';
}
echo $ssh->exec('sudo bash /myscript.sh');
}else{
echo "no host: " . $ssh_host;
}
打电话时:
echo $ssh->exec('sudo bash /myscript.sh');
我得到输出:
stdin: is not a tty
sudo: sorry, you must have a tty to run sudo
如何在phpseclib上启用tty?
Sudo是必要的,检索输出也是重要的。我的脚本返回多行
- Centos 7
- PHP7
有趣的链接:您的
$ssh->enablePTY()
已经走上了正确的轨道,但一旦您这样做,您就需要执行$ssh->read()
。如果没有PTY,$ssh->exec()
将返回命令输出。如果执行$ssh->enablePTY()
操作,则需要执行$ssh->read()
以获得输出
其原因是,有PTY时,数据可以发送到shell,也可以从shell发送,而没有PTY则无法发送。如果没有PTY,您得到的输出就是您得到的输出,没有输入可以改变这一点,因为您无法发送输入。使用PTY输入可以更改输出
同样重要的是,phpseclib的文档讨论了如何使用它来做sudo。发件人:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->read('username@username:~$');
$ssh->write("sudo ls -la\n");
$output = $ssh->read('#[pP]assword[^:]*:|username@username:~\$#', NET_SSH2_READ_REGEX);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
$ssh->write("password\n");
echo $ssh->read('username@username:~$');
}