PHP : Write a file on a remote server via ssh


<?php

//remote server address
$remote_ip = '192.168.1.1';
$port = '80';

//login account
$username = 'yourusername';
$password = 'yourpassword';

//ssh connection
$connection = ssh2_connect($remote_ip, $port);
ssh2_auth_password($connection, $username, $password');
$sftp = ssh2_sftp($connection);


$remote_dir = '/var/www/';


$the_msg = 'I am creating and writing in a file on a remote server via ssh";

$stream = @fopen("ssh2.sftp://".$sftp.$remote_dir, 'w+');

if (fwrite($stream, $the_msg) == false){

          echo "Could not write data";
}

//close connection

fclose($stream);

?>

Comments

  1. I like phpseclib better. Far more portable and more reliable too:

    login('username', 'password')) {
    exit('Login Failed');
    }

    echo $ssh->exec('pwd');
    echo $ssh->exec('ls -la');
    ?>

    phpseclib:

    http://phpseclib.sourceforge.net/index.html

    ReplyDelete

Post a Comment