Skip to content

Instantly share code, notes, and snippets.

@irkanu
Forked from petenelson/gga-ssh2-wordpress-sample.php
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irkanu/87836c2c37d715297e22 to your computer and use it in GitHub Desktop.
Save irkanu/87836c2c37d715297e22 to your computer and use it in GitHub Desktop.
<?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