2015-04-21 159 views
0

如何在php中调用ssh2_exec函数来执行远程服务器中的命令,而不用硬编码我的服务器密码?ssh2_exec密码加密

下面是PHP手册参考:

<?php 
$connection = ssh2_connect('shell.example.com', 22); 
ssh2_auth_password($connection, 'myusername', 'mypasswordisvisibleforeveryone'); 

$stream = ssh2_exec($connection, '/usr/local/bin/php -i'); 
?> 
+0

你也可以做'ssh2_auth_password($ connection,'myusername',$ _REQUEST ['password'])''。 – neubert

回答

1

我会设置一个用户只需从该服务器访问,则用户的SSH密钥

您可以连接一样,使用下面的代码...

ssh2_auth_pubkey_file($ssh, 'user', '/location/to/.ssh/id_rsa.pub', '/location/to/.ssh/id_rsa', 'passphrase'); 

其中$ssh是您$connection