Skip to content

Instantly share code, notes, and snippets.

@elgervb
Created December 7, 2018 20:44
Show Gist options
  • Save elgervb/0d68d50bb02c8a20508e3a935b3d38ae to your computer and use it in GitHub Desktop.
Save elgervb/0d68d50bb02c8a20508e3a935b3d38ae to your computer and use it in GitHub Desktop.
Networkspace2 ssh access through php script
// source: http://lacie.nas-central.org/wiki/Enabling_ssh_through_uploading_a_PHP_script_(Network_space_2)
<?php
/******************************************************
* Enable SSH root access on Lacie Network Space 2 (v1.0.2 & v1.2.5)
*
* - Use puttygen to create a public/private key pair
* - Save the public and private key to your computer
* - Copy the key from the puttygen box "Public key for pasting into OpenSSH authorized_keys file"
* - Paste the key into this script in the $sshkey variable below.
* - Save this script (call it ssh.php) to the OpenShare of your Network Space 2 (\\<IP Address of NetworkSpace2>\OpenShare)
* - Open a brower and point it to the following URL:
* http://<IP Address of NetworkSpace2>/webdav/OpenShare/ssh.php
* - Restart the Network Space 2
* - Tell PuTTY to use your private key to connect to the Network Space 2
* - You should be able to log in as root
*
******************************************************/
$sshkey=''; // Paste your key here. It starts with ssh-rsa (or ssh-dss) and ends with your "Key comment"
/******************************************************
// No changes needed below this line
******************************************************/
if (empty($sshkey)) {
die("Please paste your public key into this script!!");
}
$runlevelpath = '/etc/initng/runlevel/default.runlevel';
$authorizedkeyspath = '/root/.ssh/authorized_keys';
$initngfile = file ($runlevelpath, FILE_IGNORE_NEW_LINES);
if (in_array('sshd',$initngfile)) {
echo "SSH already enabled<br/>";
} else {
echo "Enabling SSH Access<br/>";
$fh = fopen($runlevelpath, 'a');
fwrite($fh, "sshd\n");
fclose($fh);
}
if (!is_dir('/root/.ssh')) {
echo "Creating directory /root/.ssh<br/>";
mkdir('/root/.ssh');
}
if (file_exists($authorizedkeyspath)) {
$authkeys = file('/root/.ssh/authorized_keys', FILE_IGNORE_NEW_LINES);
} else {
$authkeys = Array(); //suppress warning
}
if (!in_array($sshkey,$authkeys)) {
echo "Adding your public key to /root/.ssh/authorized_keys<br/>";
$fh = fopen('/root/.ssh/authorized_keys', 'a');
fwrite($fh, $sshkey."\n");
fclose($fh);
} else {
echo "Your public key is already in /root/.ssh/authorized_keys<br/>";
}
echo "Please restart your device";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment