# PHP SSH libs



## Patric (17. Juli 2012)

Ich habe mir in meinem PHP die SSH2 Funktionen installiert.

Jetzt habe ich mir ein PHP Script zusammengebaut, um eine Verbindung aufzubauen. Aus Sicherheitsgründen will ich das nicht mit einem Passwort machen, sondern mit PubKeyAuth. Sieht dann so aus: 
	
	



```
<?php

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.psteffen.de on port 22
if(!($con = ssh2_connect("server1.psteffen.de", 22, array('hostkey'=>'ssh-rsa')))){
    echo "fail: unable to establish connection\n";
} else {
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_pubkey_file($con, "root", "keyfiles/key.pub", "keyfiles/id_dsa", "blubb")) {
        echo "fail: unable to authenticate\n";
    } else {
        // allright, we're in!
        echo "okay: logged in...\n";
 
        // execute a command
        if (!($stream = ssh2_exec($con, "ls -al" ))) {
            echo "fail: unable to execute command\n";
        } else {
            // collect returning data from command
            stream_set_blocking($stream, true);
            $data = "";
            while ($buf = fread($stream,4096)) {
                $data .= $buf;
            }
            fclose($stream);
        }
    }
}
```
Nur leider endet das in diesem Fehler: Warning: ssh2_auth_pubkey_file(): Authentication failed for root using public key in PATH/test.php on line 9 fail: unable to authenticate.

Meine keys sehen so aus: 
pub.key
	
	



```
ssh-rsa AAAB3NzaC1yc2EAAAABJQAAAIEAvRRjJo9m6NSi7sfqtLq8Ymw3Puz1k4gaE6RuvbP5+kzJfAR9pNsv0A1gwe/dOZ/o7ulfSoEbs1uGXrLxaihDo0jw1A7YUzZjmlOhcTlAeb11YCxLVD++ZYITsHj7KiClzm5sk83OkLK1d9VDphVku64FJ22xcKxL/1Rj4Dudru0=rsa-key-20120717
```
private:
	
	



```
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQC9FGMmj2bo1KLux+q0urxibDc+7PWTiBoTpG69s/n6TMl8BH2k
2y/QDWDB7905n+ju6V9KgRuzW4ZesvFqKEOjSPDUDthTNmOaU6FxOUB5vXVgLEtU
P75lghOwePsqIKXObmyTzc6QsrV31UOmFWS7rgUnbbFwrEv/VGPgO52u7QIBJQKB
gQCjh0D+wTZogIzqMFxJR44rm9zAzPAQ5GmUcolKHx1i2qBrQid50lLdc1qZ5DTz
kTgoknvtaL4C5190fxX68mQBWGXYabYd9U+VifLiGyFvqXMFHUYalDswtgiMie/x
Wv+aDq3K/UMQ5L4N/QYo7p0/AXRwZ33tZ8ba3fxLO5E/rQJBAPXTBjoj3p0i43GU
6wBY84+WRYFGY0K7GjjwpGf8FzRFvjuZ8qL6NexNfvJIoCRciwCJev4o6BwxxSjN
bnsF1iUCQQDE6AuaeeH1nLH6i7DJiN0R8hzBK+JBntr2qSmRgaPbKIYR2a51/s89
7Di2a+adIl/TzQp33u4QHE0a6rrDt+cpAkEAugd6VYLtp1isHpo1TF79gWrM0IhL
Hb4GAZODVZwtPFB0SMeHLznjnhEhzBtIwZIkAGgKDHH7wk8tavxTm15c3QJAam+X
kcVXi66lZNXjBSdivZeg1x6j0HGLHYvfHjg85SqpVcHGFkSLssTgYpswjEnunAAh
VY1CaZLB5QlCW/S0TQJAMNYdDmJNPwaTH2mn33SH5vBCiXnPGOp+zL8cFKfB3d/k
ZqviiRrrlzWX0xaCt+11v9GcxbNH/8qwaIHJm4duCA==
-----END RSA PRIVATE KEY-----
```


----------

