proc_open互动 [英] proc_open interaction

查看:120
本文介绍了proc_open互动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要实现的目标:打开一个外壳(无须korn或bash),从该外壳中,我想打开一个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'."\n");
    fflush($ps[0]);
    usleep(20);
    fwrite($ps[0],'password'."\n");
    fflush($ps[0]);
    usleep(20);
    fwrite($ps[0],'whoami'."\n");
    fflush($ps[0]);
    usleep(2);
    $msg = stream_get_contents($ps[1]);
    fwrite($ps[0],'exit'."\n");
    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互动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆