Forked from petenelson/gga-ssh2-wordpress-sample.php
Last active
August 29, 2015 14:16
-
-
Save LateButEarly/87836c2c37d715297e22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: GGA SSH2 Sample | |
Description: Sample SSH2 upload | |
Author: Pete Nelson (@GunGeekATX) | |
Version: 1.0 | |
*/ | |
if (!defined( 'ABSPATH' )) exit('restricted access'); | |
add_action( 'admin_init', 'gga_ssh2_sample_upload' ); | |
function gga_ssh2_sample_upload() { | |
// this is VERY basic demonstration code for SSHing a file to a server | |
// files for the filesytem classes | |
require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-ssh2.php' ); | |
if ( class_exists( 'WP_Filesystem_SSH2' ) ) { | |
// also accepts public_key and private_key filenames | |
$options = array( | |
'port' => 22, | |
'hostname' => 'baconipsum.com', | |
'username' => 'awp-sample', | |
'password' => 'password-here', | |
); | |
$ssh = new WP_Filesystem_SSH2( $options ); | |
// check for errors | |
// if ssh2 is not an installed PHP extension: sudo apt-get install libssh2-php | |
if ( ! empty( $ssh->errors->errors ) ) { | |
echo $ssh->errors->errors; | |
return; | |
} | |
if ( ! $ssh->connect() ) { | |
echo 'unable to connect'; | |
} else { | |
// put the contents into a remote file | |
if ( $ssh->put_contents( '/home/awp-sample/hello-world.txt', 'Hello world! ' . current_time( 'timestamp' ) , 0644 ) ) { | |
echo 'hello-world.txt uploaded'; | |
} else { | |
echo 'unable to upload file'; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment