dra11767 2011-01-18 23:22
浏览 47
已采纳

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

I'm currently writing a deployment framework in PHP. The framework connects to servers and executes commands over SSH. I've been looking for quite a while trying to find a way in PHP to do this better. Here are the requirements. The technique should be able to:

  1. Enter the SSH password programmatically. 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. It could potentially be deploying to 25 servers at a time. 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? 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. 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. 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. 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.

  4. Support SSH v2.

Currently, my framework "compiles" the commands from the deploy script into one giant string and executes them by using the SSH command. Each final deploy command looks something like this:

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

Each of these SSH commands is executed via PHP's built-in passthru function:

<?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. In addition, I've tried several pure PHP SSH implementations, also to no avail. The libraries either don't supply write access to the terminal or don't support SSH v2.

Any help on this would be greatly appreciated - thanks in advance!

  • 写回答

3条回答 默认 最新

  • dsaf415212 2011-01-19 03:26
    关注

    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. 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? Maybe offer to pay him some money to incentivize him to develop the feature. Looking at the support forums the author seems responsive enough.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵