简体   繁体   English

在PHP中通过SSH与多个服务器进行交互

[英]Interacting with multiple servers over SSH in PHP

I'm currently writing a deployment framework in PHP. 我目前正在用PHP编写部署框架。 The framework connects to servers and executes commands over SSH. 该框架连接到服务器并通过SSH执行命令。 I've been looking for quite a while trying to find a way in PHP to do this better. 我一直在寻找很长时间,试图在PHP中找到一种更好的方法。 Here are the requirements. 这是要求。 The technique should be able to: 该技术应能够:

  1. Enter the SSH password programmatically. 以编程方式输入SSH密码。 I know that using SSH keys is the way to go when you want password-less SSH logons, but remember, this is a deployment framework. 我知道使用SSH密钥是需要无密码SSH登录的一种方式,但是请记住,这是一个部署框架。 It could potentially be deploying to 25 servers at a time. 它可能一次可以部署到25台服务器。 It doesn't seem right to require the user to have set up SSH keys to use the framework, and who wants to enter their password 25 times? 要求用户设置SSH密钥才能使用该框架似乎不正确,谁想输入25次密码? I'm using Capistrano as a model here - it asks for your password once, then uses it to establish the SSH connections without re-prompting the user. 我在这里使用Capistrano作为模型-它要求您输入一次密码,然后使用它来建立SSH连接,而无需重新提示用户。 I'm not suggesting the passwords be stored in the deploy script, just (silently) entered once and used until the deploy tasks are finished. 我不建议将密码存储在部署脚本中,仅(静默地)输入一次并在完成部署任务之前使用它们。

  2. Send output to the PHP script. 将输出发送到PHP脚本。 I would like to be able to intercept the terminal output from each of the SSH sessions independently, modify it, then send it back to the terminal for the user to see. 我希望能够分别拦截每个SSH会话的终端输出,进行修改,然后将其发送回终端供用户查看。 This way, I can prepend the name of the server onto each line of output to show what's going on. 这样,我可以在输出的每一行前添加服务器名称,以显示正在发生的事情。

  3. Provide write (as well as read) access to the terminal. 提供对终端的写(和读)访问。 It's important that the user (or the script) be able to enter other information into the terminal besides just the SSH password. 用户(或脚本)除了可以输入SSH密码外,还可以在终端中输入其他信息,这一点很重要。

  4. Support SSH v2. 支持SSH v2。

Currently, my framework "compiles" the commands from the deploy script into one giant string and executes them by using the SSH command. 当前,我的框架将来自部署脚本的命令“编译”为一个巨型字符串,并使用SSH命令执行它们。 Each final deploy command looks something like this: 每个最终的deploy命令如下所示:

ssh -t -t -p 12345 user@server.com 'command1; command2;'

Each of these SSH commands is executed via PHP's built-in passthru function: 这些SSH命令均通过PHP的内置passthru函数执行:

<?php passthru("ssh -t -t -p 12345 user@server.com 'command1; command2;'"); ?>

I have tried using proc_open and nearly all of PHP's other command-executing functions to no avail - none of them provide all the functionality I've listed above. 我尝试使用proc_open和几乎所有其他的PHP其他命令执行功能都无济于事-它们都不提供我上面列出的所有功能。 In addition, I've tried several pure PHP SSH implementations, also to no avail. 另外,我尝试了几种纯PHP SSH实现,但也无济于事。 The libraries either don't supply write access to the terminal or don't support SSH v2. 这些库不提供对终端的写访问权限,或者不支持SSH v2。

Any help on this would be greatly appreciated - thanks in advance! 在这方面的任何帮助将不胜感激-预先感谢!

Do you consider the following to be an example of write access to the terminal?: 您是否认为以下示例是对终端进行写访问的示例?:

ssh -t -t -p 12345 user@server.com 'command1; command2;'

If so then you can do that with phpseclib's pure PHP SSH implementation . 如果是这样,那么您可以使用phpseclib的纯PHP SSH实现来实现 eg. 例如。

$ssh = new Net_SSH2(...);
$ssh->login(..., ...);
$ssh->exec('command1; command2;');

If not then (1) I'd say you'd be better off using phpseclib than you are with you're current implementation and (2) if you want what the phpseclib author has referred to as "interactive" support maybe you could try contacting the author? 如果不是,那么(1)我想说的是,使用phpseclib会比使用当前的实现更好;以及(2)如果您想要phpseclib作者所说的“交互式”支持,也许您可​​以尝试联系作者? Maybe offer to pay him some money to incentivize him to develop the feature. 也许愿意付给他一些钱来激励他开发功能。 Looking at the support forums the author seems responsive enough. 在支持论坛上,作者似乎反应足够。

I'm not sure php is the right tool here. 我不确定php是这里的正确工具。 Why not use something that is closer to the shell environment, like a bash script? 为什么不使用更接近shell环境的东西,例如bash脚本?

The only reason I can imagine you want to use PHP is so you can start the process by clicking a link on a page somewhere, and thereby punch a large hole in whatever security your system supposedly has. 我可以想象您要使用PHP的唯一原因是,您可以通过单击某处页面上的链接来启动该过程,从而在您的系统应该具有的任何安全性上打一个大漏洞。 But if you really must, then it is still easier to write a script in a more shell friendly language and simply use php as a gateway to invoke the script. 但是,如果确实需要,那么使用一种更加易于使用的外壳程序语言编写脚本并轻松地使用php作为调用脚本的网关,仍然更加容易。

You can make SSH connection with a PECL extension in PHP. 您可以使用PHP中的PECL扩展名建立SSH连接。 http://php.net/manual/en/book.ssh2.php http://php.net/manual/en/book.ssh2.php

$connection = ssh2_connect('server.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream1 = ssh2_exec($connection, $command1);
$stream2 = ssh2_exec($connection, $command2);

You can also create a ssh interactive shell with ssh2_shell() if you need to more advanced things. 如果需要更高级的内容,还可以使用ssh2_shell()创建ssh交互式shell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM