dongtong2021 2010-02-16 18:27 采纳率: 0%
浏览 43
已采纳

通过PHP exec()函数连接SSH。

I am currently trying to figure out how to connect to another server via SSH using PHP's shell functions. I have a site where I have to pass data from PHP to a custom command line program, then return the output. On the old server I was using, this was possible via the exec() function:

$cmd = '/path/to/custom/program "arg1", "arg2", "arg3"';
exec($cms, $output);

$output, of course, would then hold the data that my program returned.

I am now in a position where the host will not allow me to run my custom program. PHP's shell functions, however, are still available. The host suggested I set up PHP to SSH into a different server, run the commands I need to run, and then return the output. This seems like an incredibly backwards way of doing things, but what do I know?

My specific question relates to how I would go about passing the login information to the exec() command. For example, when I connect to any SSH server manually, I put in:

ssh myserver.com -l myusername

And then I see:

myusername@myserver.com's password:

If I were to pass "ssh myserver.com -l myusername" as the command to exec(), how would I then give it the password? After that, how would I then know that I am properly authenticated and connected so I can run the command for my program?

Oh, and also, I do not have access to the ssh2 functions in PHP, nor would I be able to install that extension.

  • 写回答

3条回答 默认 最新

  • doucuo9126 2010-02-16 22:13
    关注

    You need bi-directional communication with the new process from your PHP code - that being the case, proc_open(...) is the only php function you can use to run ssh.

    After that - its simply a matter of writing code to wait for prompts and respond to them.

    However there is ssh implementation written in PHP posted at phpclasses - although I've no idea how fast/efficient/reliable/compatible it is. I suspect that it won't be any less effort than the proc_open() approach and possible a lot more.

    C.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题