这是我想要实现的目标:打开一个 shell(korn 或 bash,无所谓),从那个 shell,我想打开一个 ssh 连接(ssh user@host
).在某些时候可能会发生这种情况,我会被提示输入密码,或者我可能会被问到是否确定要连接(有问题的密钥).
Here's what I'm trying to achieve: open a shell (korn or bash, doesn't matter), from that shell, I want to open a ssh connection (ssh user@host
). At some point it is likely to happen I will be prompted for either a password or I might be asked whether or not I'm sure I want to connect (offending keys).
在有人问之前:是的,我知道有一个用于 ssh2 exec 调用的插件,但是我正在使用的服务器不支持它,并且不太可能这样做.
Before anyone asks: yes, I am aware there is a plugin for ssh2 exec calls, but the servers I'm working on don't support it, and are unlikely to do so.
这是我迄今为止尝试过的:
Here's what I've tried so far:
$desc = array(array('pipe','r'),array('pipe','w'));//used in all example code
$p = proc_open('ssh user@host',$desc,$pipes);
if(!is_resource($p)){ die('@!#$%');}//will omit this line from now on
sleep(1);//omitting this,too but it's there every time I need it
然后我尝试读取控制台输出 (stream_get_contents($pipes[1])
) 以查看我接下来必须传递的内容(密码,是或返回 'connection failed: '.stream_get_contents($pipes[1])
和 proc_close $p.
Then I tried to read console output (stream_get_contents($pipes[1])
) to see what I have to pass next (either password, yes or return 'connection failed: '.stream_get_contents($pipes[1])
and proc_close $p.
这给了我以下错误:
伪终端不会被分配,因为 stdin 不是终端.
Pseudo-terminal will not be allocated because stdin is not a terminal.
所以,我虽然在 php://
io-stream 上下文中调用了 ssh,但似乎是上述错误的合理解释.
So, I though ssh was called in the php://
io-stream context, seems a plausible explanation of the above error.
下一步:我虽然关于 我的第一个 SO 问题 并决定它可能是一个最好先打开 bash/ksh shell:
Next: I though about my first SO question and decided it might be a good idea to open a bash/ksh shell first:
$p = proc_open('bash',$desc,$pipes);
从那里开始,但我收到了完全相同的错误消息,只是这一次,脚本停止运行,但 ssh 确实运行了.所以我充满希望,然后觉得自己很愚蠢,最终绝望了:
And take it from there, but I got the exact same error message, only this time, the script stopped running but ssh did run. So I got hopeful, then felt stupid and, eventually, desperate:
$p=proc_open('bash && ssh user@host',$desc,$pipes);
等待几秒钟后,我收到以下错误:
After a few seconds wait, I got the following error:
PHP 致命错误:允许的内存大小为 134217728 字节已用完(尝试分配 133693440 字节)
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 133693440 bytes)
即使在我最后一次绝望的尝试中,调用堆栈也会不断地调用 stream_get_contents 行:
The Call Stack keeps bringing up the stream_get_contents line, even in my last desperate attempt:
#!/path/to/bin/php -n
<?php
$p = proc_open('bash && ssh user@host',array(array('pipe','r'),array('pipe','w')),$ps);
if (!is_resource($p))
{
die('FFS');
}
usleep(10);
fwrite($ps[0],'yes'."
");
fflush($ps[0]);
usleep(20);
fwrite($ps[0],'password'."
");
fflush($ps[0]);
usleep(20);
fwrite($ps[0],'whoami'."
");
fflush($ps[0]);
usleep(2);
$msg = stream_get_contents($ps[1]);
fwrite($ps[0],'exit'."
");
fclose($ps[0]);
fclose($ps[1]);
proc_close($p);
?>
我知道,它很乱,有很多fflush
和冗余,但重点是:我知道这个连接会首先提示我输入有问题的密钥,然后询问密码.我的猜测是 $pipes[1] 中的流包含 ssh 连接,因此它的内容很大.那么我需要的是管道内的管道......这甚至可能吗?我一定是遗漏了什么,如果这不可能的话,管子有什么用……我的猜测是 proc_open 命令开始是错误的,(错误:管道损坏).但我真的看不到第一个错误的任何其他方式......有什么想法吗?或者,如果上述咆哮根本不清楚(可能不是),请跟进问题.
I know, its a mess, a lot of fflush
and redundancy, but the point is: I know this connection will first prompt me for offending keys, and then ask a password. My guess is the stream in $pipes[1] holds the ssh connection, hence it's content is huge. what I need then, is a pipe inside a pipe... is this even possible? I must be missing something, what good is a pipe if this isn't possible...
My guess is the proc_open command is wrong to begin with, (error: Broken pipe). But I really can't see any other way around the first error... any thoughts? Or follow up questions if the above rant isn't at all clear (which it probably isn't).
在有人问之前:是的,我知道有一个用于 ssh2 exec 的插件调用,但我正在使用的服务器不支持它,并且不太可能这样做.
Before anyone asks: yes, I am aware there is a plugin for ssh2 exec calls, but the servers I'm working on don't support it, and are unlikely to do so.
实际上有两个.PECL 模块,这是大多数服务器都没有安装的 PITA 和 phpseclib,一个纯 PHP SSH2 实现.其使用示例:
There are actually two. The PECL module, which is a PITA that most servers don't have installed anyway and phpseclib, a pure PHP SSH2 implementation. An example of its use:
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
这篇关于proc_open 交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!