問題描述
這是我想要實現的目標:打開一個 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 交互的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!