在PHP脚本上验证生成的bitbucket ssh密钥

在PHP脚本上验证生成的bitbucket ssh密钥,php,git,ssh,bitbucket,webhooks,Php,Git,Ssh,Bitbucket,Webhooks,在VPS ssh中,我将: admin@bpm3i:~$ ssh -vT git@bitbucket.org OpenSSH_5.9p1 Debian-5ubuntu1.4, OpenSSL 1.0.1 14 Mar 2012 debug1: Reading configuration data /home/admin/.ssh/config debug1: /home/admin/.ssh/config line 1: Applying options for bitbucket.or

在VPS ssh中,我将:

    admin@bpm3i:~$ ssh -vT git@bitbucket.org
OpenSSH_5.9p1 Debian-5ubuntu1.4, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /home/admin/.ssh/config
debug1: /home/admin/.ssh/config line 1: Applying options for bitbucket.org
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to bitbucket.org [131.103.20.168] port 22.
debug1: Connection established.
debug1: identity file /home/admin/ssh/id_rsa type -1
debug1: identity file /home/admin/ssh/id_rsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.4
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 97:8c:1b:f2:6f:14:6b:5c:3b:ec:aa:46:46:74:7c:40
debug1: Host 'bitbucket.org' is known and matches the RSA host key.
debug1: Found key in /home/admin/.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/admin/.ssh/id_rsa
debug1: Remote: Forced command: conq username:workquality
debug1: Remote: Port forwarding disabled.
debug1: Remote: X11 forwarding disabled.
debug1: Remote: Agent forwarding disabled.
debug1: Remote: Pty allocation disabled.
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Remote: Forced command: conq username:workquality
debug1: Remote: Port forwarding disabled.
debug1: Remote: X11 forwarding disabled.
debug1: Remote: Agent forwarding disabled.
debug1: Remote: Pty allocation disabled.
debug1: Authentication succeeded (publickey).
Authenticated to bitbucket.org ([131.103.20.168]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
logged in as workquality.

You can use git or hg to connect to Bitbucket. Shell access is disabled.
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2752, received 2888 bytes, in 0.3 seconds
Bytes per second: sent 10079.8, received 10577.9
debug1: Exit status 0
我不知道它怎么了。在SSH、PHP-CLI上可以,但在Web服务器上不工作,请帮助。谢谢

apache用户www数据没有对包含用户ssh密钥的文件的读取权限,因此无法执行该命令,因为linux正试图通过CGI使用用户www数据执行该命令

至于执行官“whoami”;和exec'echo~。$user;命令,我认为您当前正在直接运行它们,尝试从PHP文件中运行它们并通过Web服务器执行,您肯定会看到不同的用户

因此,如前所述,为apache用户生成一个ssh密钥

并在您的bitbucket帐户上添加具有只读访问权限的公钥。

这是因为默认情况下,ssh将在您的主目录、管理员的主目录中尝试查找您的ssh密钥

当您通过apache执行它时,脚本在远离admin的主目录下运行。虽然您可以通过命令行ssh-i/path/to/key将路径传递给密钥,但将apache读取权限授予admin的密钥是非常不安全的

我将创建一个单独的ssh密钥,并仅将其用于bitbucket读取访问。然后将其放入apache可访问的文件夹中,但不能通过web浏览

现在可以使用以下shell_exec调用来测试连接:

<?php echo shell_exec("ssh -i /path/to/key -vT git@bitbucket.org 2>&1");?>

我生成了两个ssh密钥,一个用于admin,另一个用于www-data-apache-user,我将它们放在bitbucket用户设置中,它就工作了!谢谢你的回复
<?php echo exec("ssh -vT git@bitbucket.org 2>&1");?>
exec('whoami'); //admin
exec('echo ~'.$user); // /home/admin
sudo -u apache ssh-keygen -t rsa
<?php echo shell_exec("ssh -i /path/to/key -vT git@bitbucket.org 2>&1");?>