简体   繁体   English

php脚本访问远程linux服务器并执行bash脚本

[英]php script to access remote linux server and execute bash script

I am trying to do the following.我正在尝试执行以下操作。

Have php execute shell commands on our remote servers.让 php 在我们的远程服务器上执行 shell 命令。 This because i would like to be able to install scripts through bash So php is going to execute the bash scripts on the remote server.这是因为我希望能够通过 bash 安装脚本,所以 php 将在远程服务器上执行 bash 脚本。

The issue is the can't connect to the remote server for some reason.问题是由于某种原因无法连接到远程服务器。 I have tried several way to do this and i have to do with the root and password.我已经尝试了几种方法来做到这一点,而且我必须使用 root 和密码。 Later i can look at encrypting them or something like that, but i can't use a ssh key so that is not going to work.稍后我可以考虑加密它们或类似的东西,但我不能使用 ssh 密钥,所以这是行不通的。 I am pretty new to php part of it so i need some help figuring out hot to connect to remote servers.我对 php 部分很陌生,所以我需要一些帮助来确定连接到远程服务器的热点。

the code i am trying right now is我现在正在尝试的代码是

  $ip = '37.130.000.00'; 
$user = 'admin'; 
$pass = 'xxxxxxxxx'; 

$connection = ssh2_connect($ip); 
ssh2_auth_password($connection,$user,$pass); 
$shell = ssh2_shell($connection,"uptime");


 echo $shell;

For some reason this code is giving me the following error出于某种原因,此代码给了我以下错误

Fatal error: Call to undefined function ssh2_connect() in致命错误:调用未定义的函数 ssh2_connect()

How can i make this work or what else should i do to properly connect to the remote servers and this all should be very flexible as we are talking around 100 server who should be able to connect this way.我怎样才能完成这项工作,或者我应该怎么做才能正确连接到远程服务器,这一切都应该非常灵活,因为我们正在谈论 100 台应该能够以这种方式连接的服务器。

Thanks for any input.感谢您提供任何意见。

That function is provided by an optional extension that you don't seem to have installed.该功能由您似乎尚未安装的可选扩展提供。 Provided you have libssh and other dependencies in your system, get pecl-ssh by executing如果您的系统中有 libssh 和其他依赖项,请通过执行获得 pecl-ssh

sudo pecl install ssh2 channel://pecl.php.net/ssh2-0.11.3

and then restart php-fpm or apache if you're using modphp.如果您使用的是 modphp,然后重新启动 php-fpm 或 apache。

Edit: btw, this question has been asked before and one of the wisest advises I've seen in regards to is was to switch over to a pure php implementation, no pecl extensions whatsoever: php-function-ssh2-connect-is-not-working编辑:顺便说一句,之前已经问过这个问题,我见过的最明智的建议之一是切换到纯 php 实现,没有任何 pecl 扩展: php-function-ssh2-connect-is-not -在职的

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

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