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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据