conexão ssh falhou via php
Tentando se conectar ao ssh usando um script php através de um servidor web.
Eu trabalho em um MacOsX.
Eu mudei o usuário e grupo do apache (_www: _www) para o meu próprio usuário e grupo ().
O comando que eu uso é isso (eu mudei os detalhes da conexão):
ssh -v -p 2222 -i /Users/myname/.ssh/id_dsa_mykey -l myuser 10.1.12.10 2>&1
De um terminal, não há problema, ele se conecta e fornece a seguinte saída:
OpenSSH_5.9p1, OpenSSL 0.9.8y 5 Feb 2013
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 54: Applying options for *
debug1: Connecting to 10.1.12.10 [10.1.12.10] port 2222.
debug1: Connection established.
debug1: identity file /Users/myname/.ssh/id_dsa_mykey type 2
debug1: identity file /Users/myname/.ssh/id_dsa_mykey-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4
debug1: match: OpenSSH_6.0p1 Debian-4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
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 0c:bd:4e:da:17:28:d2:a6:20:b9:11:a3:de:91:f0:ce
debug1: Host '[10.1.12.10]:2222' is known and matches the RSA host key.
debug1: Found key in /Users/myname/.ssh/known_hosts:6
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,password
debug1: Next authentication method: publickey
debug1: Offering DSA public key: /Users/myname/.ssh/id_dsa_mykey
debug1: Server accepts key: pkalg ssh-dss blen 434
debug1: Authentication succeeded (publickey).
Authenticated to 10.1.12.10 ([10.1.12.10]:2222).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
...
No entanto, a partir de um script php, ele fornece a seguinte saída:
OpenSSH_5.9p1, OpenSSL 0.9.8y 5 Feb 2013
Pseudo-terminal will not be allocated because stdin is not a terminal.
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 54: Applying options for *
debug1: Connecting to 10.1.12.10 [10.1.12.10] port 2222.
debug1: Connection established.
debug1: identity file /Users/myname/.ssh/id_dsa_mykey type 2
debug1: identity file /Users/myname/.ssh/id_dsa_mykey-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.0p1 Debian-4
debug1: match: OpenSSH_6.0p1 Debian-4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
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 0c:bd:4e:da:17:28:d2:a6:20:b9:11:a3:de:91:f0:ce
debug1: Host '[10.1.12.10]:2222' is known and matches the RSA host key.
debug1: Found key in /Users/myname/.ssh/known_hosts:6
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,password
debug1: Next authentication method: publickey
debug1: Offering DSA public key: /Users/myname/.ssh/id_dsa_mykey
debug1: Server accepts key: pkalg ssh-dss blen 434
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: Authentications that can continue: publickey,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: Authentications that can continue: publickey,password
Permission denied, please try again.
debug1: read_passphrase: can't open /dev/tty: Device not configured
debug1: Authentications that can continue: publickey,password
debug1: No more authentication methods to try.
Permission denied (publickey,password).
Acho que o problema vem depois dessa linha:
debug1: Server accepts key: pkalg ssh-dss blen 434
Com o terminal, ele pode se conectar:
debug1: Authentication succeeded (publickey).
Mas do php ele falha:
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
Eu não sei porque, porque o servidor tem o mesmo usuário e grupo que eu quando eu executo o comando
no terminal.
RESOLVIDO por HBruijn
Então criei um novo par de chaves ssh e ... funcionou.
Mohahahaaaa, sou muito poderoso agora!