Author: Jim Wigginton (terrafrost@php.net)
Beispiel #1
16
 public function Exec($command)
 {
     $engine = $this->session->getSpooler();
     if (!isset($engine[0]['shell'])) {
         print "?!";
         exit;
     }
     $shell = $engine[0]['shell'];
     $host = $shell['host'];
     $user = $shell['user'];
     $password = $shell['password'];
     $method = 'CURL';
     set_include_path(get_include_path() . PATH_SEPARATOR . '../vendor/phpseclib');
     include 'Net/SSH2.php';
     include 'Crypt/RSA.php';
     $ssh = new \Net_SSH2($host);
     if (1) {
         $key = new \Crypt_RSA();
         $ret = $key->loadKey("-----BEGIN RSA PRIVATE KEY-----\nMIICWgIBAAKBgQCzRy01HoHIzBJJb/D8A/eTV3qiZxy0NIR97NE14rJblnJZT5Kg\noP2DvIRzlB0msL5cHQJ/qXYAoemHRDKqNZuj89+MYsBeZqNu3/DXdZLq9XJ8e2rb\nsGrDjHvCHEDWL0JIRFnRacem55+XsUsKTIs4tbcD6adMPIYJSQQ7oB/8AQIBIwKB\ngB67vptkUMNWLwVGY9NuZPSv6SMnnoVK1OJjHIzlCKH8iKGYnMsUSLd/ZynBnpjr\nGVGekrbMl+LZ7YTnHqDV/WxGoWEc3xiHE8/HwZwQZxP92K70inz8+6dGEagsrSqO\nQkdAPR/+qen7uQ9yXqj7WAoNFicPJ2cpo8kuEW33KywzAkEA4yH4jf0uNBFDUkR6\ni9DQC5bsgEloVezWnCsm6eIm5o5SGKPZ6Rpro/h3pq5qvPmCtjrZFnK0Dab9xkFr\n/F9lkwJBAMoQMqxYdnPz74Bto99o0PZrk2ikROwXR9eURi3B4bWGq9+mvN3OEQdE\n8JofGyq60LMlnFAkE7v49fYHziyaFJsCQHTPpGZHsVybKe/LcjlG0WULyhYXH7cp\nWG2SiQqRkFlQgf4LH5xz/Nf8IEcX3x9bv5DrEI8zrQ5V4Zko9bT93HcCQQCEyNDX\np9jP2tCWOWuwEa3jwwkY4PoXfQNTJuxJ9G/AbnDyDnwcup15zje1vKtz2dmaS+pg\njLyC1s2Ea4d8ZUC9AkAeUr/N+011K2zGTjxZnAFY/Ow348bomzddiJYAYA+76exV\n3wUYsjeDxqq8Km93+iMQ8DDNZIvoVcfYQW9BfDlf\n-----END RSA PRIVATE KEY-----   ");
         if (!$ret) {
             echo "loadKey failed\n";
             print "<pre>" . $ssh->getLog() . '</pre>';
             exit;
         }
     } elseif (isset($shell['password'])) {
         $key = $shell['password'];
     } else {
         $key = '';
         // ?! possible ?
     }
     if (!$ssh->login($user, $key)) {
         exit("Login Failed ({$user})");
     }
     return $ssh->exec("system '{$command}'");
 }
Beispiel #2
1
 /**
  * Logs in to a SSH server with the specified credentials,
  * and returns the Net_SSH2 instance.
  *
  * @return 	Net_SSH2 $ssh 
  * @author 	Ronald Rey
  **/
 public static final function getSSH($host, $user, $pass, $timeout)
 {
     $ssh = new Net_SSH2($host);
     if (!$ssh->login($user, $pass)) {
         exit('Login to failed.');
     }
     $ssh->setTimeout($timeout);
     $ssh->write(" ");
     return $ssh;
 }
Beispiel #3
0
 public function Exec($shell, $command, $stdin = '')
 {
     $host = $shell['host'];
     $user = $shell['user'];
     $ssh = new \Net_SSH2($host);
     if (isset($shell['key'])) {
         $key = new \Crypt_RSA();
         $ret = $key->loadKey($shell['key']);
         if (!$ret) {
             $this->status = '!KEY';
             echo "loadKey failed\n";
             print "<pre>" . $ssh->getLog() . '</pre>';
             return;
         }
     } elseif (isset($shell['password'])) {
         $key = $shell['password'];
     } else {
         $key = '';
         // ?! possible ?
     }
     if (!@$ssh->login($shell['user'], $key)) {
         $this->status = '!LOGIN';
         print 'Login Failed: ' . $shell['user'];
         print "<pre>" . $ssh->getLog() . '</pre>';
         return;
     }
     $this->status = 'RUNNING';
     if ($stdin == '') {
         return $ssh->exec("{$command}");
     }
     return;
 }
function getPing($sourceIP, $destinationIP)
{
    // This will work with any pfSense install. $sourceIP is the IP address of the WAN that you want to
    // use to ping with. This allows you to ping the same address from multiple WANs if you need to.
    global $local_pfsense_ip;
    global $pfSense_username;
    global $pfSense_password;
    $ssh = new Net_SSH2($local_pfsense_ip);
    if (!$ssh->login($pfSense_username, $pfSense_password)) {
        //exit('Login Failed');
        return array(0, 0);
    }
    $terminal_output = $ssh->exec('ping -c 5 -q -S ' . $sourceIP . ' ' . $destinationIP);
    // If using something besides OS X you might want to customize the following variables for proper output of average ping.
    $findme_start = '= ';
    $start = strpos($terminal_output, $findme_start);
    $ping_return_value_str = substr($terminal_output, $start + 2, 100);
    $findme_stop1 = '.';
    $stop = strpos($ping_return_value_str, $findme_stop1);
    $findme_avgPing_decimal = '.';
    $avgPing_decimal = strpos($ping_return_value_str, $findme_avgPing_decimal, 6);
    $findme_forward_slash = '/';
    $avgPing_forward_slash = strpos($ping_return_value_str, $findme_forward_slash);
    $avgPing = substr($ping_return_value_str, $stop + 5, $avgPing_decimal - $avgPing_forward_slash - 1);
    return $avgPing;
}
Beispiel #5
0
 public function Exec($host, $user, $password, $command)
 {
     $ssh = new Net_SSH2($host);
     if (!$ssh->login($user, $password)) {
         exit('Login Failed');
     }
     echo $ssh->exec("system '{$command}'");
 }
Beispiel #6
0
 function delete_user($username, $server_host, $server_username, $server_password)
 {
     $ssh = new Net_SSH2($server_host);
     if ($ssh->login($server_username, $server_password) == false) {
         exit('Login Failed');
     }
     $command = 'userdel -r ' . $username;
     echo $ssh->exec($command);
 }
function ConectSSHStation($server)
{
    $ssh = new Net_SSH2($server);
    if (!$ssh->login(userubnt, passubnt)) {
        print "Login Failed {$server}";
    }
    $mca = $ssh->exec("mca-status");
    return $mca;
}
Beispiel #8
0
 public function Exec($command)
 {
     set_include_path(get_include_path() . PATH_SEPARATOR . '../vendor/phpseclib');
     include 'Net/SSH2.php';
     $ssh = new \Net_SSH2($this->host);
     if (!$ssh->login($this->user, $this->password)) {
         exit('Login Failed');
     }
     return $ssh->exec("system '{$command}'");
 }
Beispiel #9
0
 public function connectToServer()
 {
     include Mage::getBaseDir('base') . DS . 'lib' . DS . 'Ssh' . DS . 'Net' . DS . 'SSH2.php';
     $ssh = new Net_SSH2($this->getMacServerIp());
     if ($ssh->login($this->getMacServerUser(), $this->getMacServerPassword())) {
         return $ssh;
     } else {
         return false;
     }
 }
Beispiel #10
0
function runcmd($cmd)
{
    include 'Net/SSH2.php';
    $ssh = new Net_SSH2('localhost');
    if (!$ssh->login('rc', 'cacapedo')) {
        exit('Login Failed');
    }
    // echo $ssh->exec('pwd');
    $r = $ssh->exec($cmd);
    return str_replace("\n", "<br>", $r);
}
Beispiel #11
0
 /**
  * connect to the remote server
  * @return bool|\Net_SSH2
  */
 private function connect()
 {
     $ssh = new \Net_SSH2(Setting::getSetting('remote:host'));
     $key = new Crypt_RSA();
     $key->loadKey(file_get_contents(Setting::getSetting('ssh:priv_key')));
     if (!$ssh->login(Setting::getSetting('remote:user'), $key)) {
         $cli = new CLImate();
         $cli->error("Could not connect to server");
         return false;
     }
     return $ssh;
 }
Beispiel #12
0
 public static function localhost_connect()
 {
     global $cphp_config;
     $sSSH = new Net_SSH2('127.0.0.1');
     $sKey = new Crypt_RSA();
     $sKey->loadKey(file_get_contents($cphp_config->settings->rootkey));
     if ($sSSH->login("root", $sKey)) {
         return $sSSH;
     } else {
         return $sErrors = array("Could not connect to the local host.");
     }
 }
Beispiel #13
0
/**
 * Establish a SSH2 connection using PHPSECLIB
 *
 * @return object (ssh obj) OR string (err)
 */
function newNetSSH2($ip, $sshport = 22, $login, $password)
{
    $ssh = new Net_SSH2($ip, $sshport);
    if (!$ssh->login($login, $password)) {
        $socket = @fsockopen($ip, $sshport, $errno, $errstr, 5);
        if ($socket == FALSE) {
            $debug = "Unable to connect to {$ip} on port {$sshport} : {$errstr} ( Errno: {$errno} )";
            return $debug;
        }
        return 'Unable to connect to box with SSH';
    }
    return $ssh;
}
Beispiel #14
0
 /** 	IS USED TO CONNECT TO SSH
 		INCLUDE THIS BEFORE FUNCTION CALL
 	 *		set_include_path(get_include_path() . PATH_SEPARATOR . ROOT_DIRECTORY_PATH . 'phpseclib');
 			include_once('Net/SSH2.php');
 			include_once('Crypt/RSA.php');
 		EXAMPLE AT addEc2Request.php
 
 		@return empty sting if thigs goes wrong.
 	 */
 function connect_to_ssh($publicDnsName, $keyPairName)
 {
     $ssh = new Net_SSH2($publicDnsName);
     $key = new Crypt_RSA();
     $key->loadKey(file_get_contents(ROOT_DIRECTORY_PATH . AMAZON_SSH_KEYS . $keyPairName));
     if (!$ssh->login('ubuntu', $key)) {
         //WHEN USER IS NOT LOGGED INTO SSH
         return 2;
     } else {
         //if things goes right.
         return array('Net_SSH2' => $ssh, 'Crypt_RSA' => $key);
     }
 }
Beispiel #15
0
 /**
  * Execute task and return report info
  *
  * @return \Deployer\Task
  */
 public function execute()
 {
     foreach ($this->params() as $param => $value) {
         $this->_params[$param] = \Deployer::applyGlobalParams($value);
     }
     $ssh = new \Net_SSH2($this->param('host'), $this->param('port'));
     if (!$ssh->login($this->param('username'), $this->param('password'))) {
         throw new \Deployer\TaskException('Can\'t connect to remote host');
     }
     $scp = new \Net_SCP($ssh);
     $scp->put($this->param('dest'), file_get_contents($this->param('file')));
     $info = sprintf('Filesize: %d bytes', filesize($this->param('file')));
     return $this->info($info);
 }
Beispiel #16
0
function set_addr($subnet, $interface)
{
    $interface = $interface != '' ? $interface : $GLOBALS['ethernet_forenaming_scheme'] . '1';
    $ip = explode('/', $subnet);
    $cidr = $ip[1];
    $ip_parts = explode('.', $ip[0]);
    $ip_parts[3] = $ip_parts[3] + 1;
    $gw = implode('.', $ip_parts);
    $ip_parts[3] = $ip_parts[3] + 1;
    $ip = implode('.', $ip_parts);
    $ssh = new Net_SSH2('192.168.88.1');
    if ($ssh->login('admin', $GLOBALS['admin_password'])) {
        //while we're here we might as well reset the mac addresses again.
        $detail = $ssh->exec('int eth print');
        if (preg_match_all("/{$GLOBALS['ethernet_forenaming_scheme']}/", $detail, $matches)) {
            $i = 1;
            foreach ($matches[0] as $match) {
                $ssh->exec('int ethernet reset-mac-address ' . $GLOBALS['ethernet_forenaming_scheme'] . $i);
                $ssh->exec(':beep frequency=120 length=2ms;');
                $i++;
                if ($i == '10') {
                    $ssh->exec('int ethernet reset-mac-address ' . $GLOBALS['sfp_forenaming_scheme'] . '1');
                }
            }
        }
        $ssh->exec('ip address add address=' . $ip . '/' . $cidr . ' interface=' . $interface);
        //set the ip on the specified interface.
        $ssh->exec('ip route add dst-address=0.0.0.0/0 gateway=' . $gw);
        //add the default route
    }
}
Beispiel #17
0
 function connect()
 {
     try {
         $ssh = new \Net_SSH2($this['addr'], $this['ssh_port'] ?: 22);
         $key = $this->getPrivateKey();
         if (!$ssh->login($this['ssh_user'] ?: 'dokku', $key)) {
             throw $this->exception('Login Failed!');
         }
         return $ssh;
     } catch (BaseException $e) {
         throw $e;
         // don't do anything yet
         // var_dump($e);
     }
 }
Beispiel #18
0
 public function runCommand($cmd)
 {
     if (!empty($this->key_path)) {
         $key = new Crypt_RSA();
         $key->loadKey(file_get_contents($this->key_path));
     }
     $ssh = new Net_SSH2($this->host, $this->port);
     if ($key) {
         $ssh->login($this->user, $key);
     } else {
         $ssh->login($this->user, $this->password);
     }
     $out = $ssh->exec($cmd);
     return $out;
 }
 public static function server_connect($sServer, $sAPI = 0)
 {
     $sSSH = new Net_SSH2($sServer->sIPAddress);
     if ($sServer->sPassword == 0) {
         $sKey = new Crypt_RSA();
         $sKey->loadKey(file_get_contents('/var/feathur/data/keys/' . $sServer->sKey));
     } else {
         $sKey = file_get_contents('/var/feathur/data/keys' . $sServer->sKey);
     }
     try {
         if (!$sSSH->login($sServer->sUser, $sKey)) {
             if (!empty($sAPI)) {
                 return $sResult = array("result" => 'Unable to connect to the host node, please contact customer service.');
             }
             echo json_encode(array("result" => 'Unable to connect to the host node, please contact customer service.'));
             die;
         } else {
             $sSSH->setTimeout(30);
             return $sSSH;
         }
     } catch (Exception $e) {
         if (!empty($sAPI)) {
             return $sResult = array("result" => 'Unable to connect to the host node, please contact customer service.');
         }
         echo json_encode(array("result" => 'Unable to connect to the host node, please contact customer service.'));
         die;
     }
 }
Beispiel #20
0
 protected function setUp()
 {
     if (!file_exists('config.php')) {
         $this->markTestSkipped("Can't run test without setting up a local configution! See config.dist.php");
     }
     include 'config.php';
     include_once 'Net/SSH2.php';
     $this->client = Net_SSH2::factory('LibSSH2', $options['libssh2']);
 }
Beispiel #21
0
 /**
  * Execute task and return report info
  *
  * @return \Deployer\Task
  */
 public function execute()
 {
     $info = '';
     foreach ($this->params() as $param => $value) {
         $this->_params[$param] = \Deployer::applyGlobalParams($value);
     }
     $ssh = new \Net_SSH2($this->param('host'), $this->param('port'));
     if (!$ssh->login($this->param('username'), $this->param('password'))) {
         throw new \Deployer\TaskException('Can\'t connect to remote host');
     }
     foreach ((array) $this->param('cmd') as $cmd) {
         $command = 'cd ' . $this->param('directory') . ' && ' . $cmd;
         $command = \Deployer::applyGlobalParams($command);
         $result = $ssh->exec($command);
         $info .= '$ ' . $command . PHP_EOL . $result . PHP_EOL;
     }
     return $this->info($info);
 }
Beispiel #22
0
function get_json_info_from_server($ip_address, $username, $password, $start_date, $end_date = null)
{
    global $path_to_logfile_rollup;
    $ssh = new Net_SSH2($ip_address);
    if (!$ssh->login($username, $password)) {
        exit('Login Failed');
    }
    $command = 'php ' . $path_to_logfile_rollup . 'logfile_rollup.php ' . $start_date;
    if ($end_date !== null) {
        $command .= ' ' . $end_date;
    }
    $json_response = null;
    try {
        $json_response = $ssh->exec($command);
    } catch (Exception $e) {
        die($e->getMessage());
    }
    return $json_response;
}
function ssh_connect($host)
{
    dbg_log("Connecting over SSH to {$host}");
    #define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
    $ssh = new Net_SSH2($host);
    $key = new Crypt_RSA();
    $key->setPassword(get_config()->host_ssh_private_key_password);
    $keyPath = get_config()->host_ssh_private_key;
    $keyString = file_get_contents($keyPath);
    $userString = get_config()->host_ssh_username;
    if (!$key->loadKey($keyString)) {
        dbg_log(var_dump($ssh->getErrors(), true));
        exit("cannot import key {$keyPath}");
    }
    if (!$ssh->login($userString, $key)) {
        dbg_log($ssh->getLastError());
        exit('Login Failed');
    }
    return $ssh;
}
 public function execute($com = false)
 {
     if ($com == false) {
         die("Epic Fail! Yes, it was epic");
     }
     $oldPath = set_include_path("/home/sites/berrics.dev/sharedVendors/phpseclib");
     App::import("Vendor", "SSH2", array("file" => "phpseclib/Net/SSH2.php"));
     set_include_path($oldPath);
     $ssh = new Net_SSH2('10.181.67.27');
     $login = $ssh->login('root', 'WEB1MH0r5t7Wn');
     $out = '';
     switch ($com) {
         case "sync-berrics-all":
             $out .= $ssh->exec("/home/sites/berrics.shell/sync-berrics-all");
             $out .= $ssh->exec("/home/sites/berrics.shell/clear-cache");
             break;
         case "sync-berrics-all-check":
             $out .= $ssh->exec("/home/sites/berrics.shell/sync-berrics-all-check");
             break;
         case "sync-berrics-splash":
             $out .= $ssh->exec("/home/sites/berrics.shell/sync-berrics-splash");
             break;
         default:
             $out .= "Nothing to do...";
             break;
     }
     $this->set(compact("out"));
     //echo $ssh->exec('/home/sites/berrics.shell/sync-berrics-all-check');
 }
Beispiel #25
0
 /**
  * ???
  *
  * @param string[][] $data Die Serverdaten
  * @return ???
  */
 public static function Verbinden($data)
 {
     $ssh = null;
     if ($data['ZV']['zv_ssh_auth_type'] == 'passwd') {
         $ssh = new Net_SSH2($data['ZV']['zv_ssh_address']);
         $res = @$ssh->login($data['ZV']['zv_ssh_login'], $data['ZV']['zv_ssh_password']);
         if (!$res) {
             return null;
         }
         $ssh->setTimeout(0);
     } else {
         if ($data['ZV']['zv_ssh_auth_type'] == 'keyFile') {
             $ssh = new Net_SSH2($data['ZV']['zv_ssh_address']);
             $key = new Crypt_RSA();
             $key->loadKey(file_get_contents($data['ZV']['zv_ssh_key_file']));
             if (!$ssh->login($data['ZV']['zv_ssh_login'], $key)) {
                 return null;
             }
             $ssh->setTimeout(0);
         }
     }
     return $ssh;
 }
function getInfo($ip, $sshkey_location)
{
    $ssh = new Net_SSH2($ip);
    //key
    $key = new Crypt_RSA();
    $key->loadKey(file_get_contents($sshkey_location));
    if (!$ssh->login('root', $key)) {
        die("SSH Login Failed!");
    } else {
        $return['disk_usage'] = $ssh->exec("df -k .|awk 'NR==2 {print \$5}'");
        $return['disk_remaining'] = 100 - substr($return['disk_usage'], 0, -1) . "%";
        $uptime = $ssh->exec('echo `uptime`');
        list($uptime, $load) = explode('load average:', $uptime);
        list($trash, $uptime) = explode(' up ', $uptime);
        list($uptime, $trash) = explode(', ', $uptime);
        $return['uptime'] = $uptime;
        $return['load'] = $load;
        //get memory
        $return['memory_free'] = $ssh->exec("cat /proc/meminfo|grep 'MemFree'");
        list($trash, $return['memory_free']) = explode('MemFree:', $return['memory_free']);
    }
    return $return;
}
Beispiel #27
0
 private function doRunTests($testSuite, array $arguments)
 {
     $arguments = implode(' ', $arguments);
     $this->ssh->exec("ps -ef | grep \"php console tests:run\" | grep -v grep | awk '{print \$2}' | xargs kill -9");
     if ('all' === $testSuite) {
         $this->ssh->exec('php console tests:run --options="--colors" ' . $arguments);
     } elseif ('ui' === $testSuite) {
         $this->ssh->exec('php console tests:run-ui --persist-fixture-data --assume-artifacts ' . $arguments);
     } else {
         $this->ssh->exec('php console tests:run --options="--colors" --testsuite="unit" ' . $arguments);
         $this->ssh->exec('php console tests:run --options="--colors" --testsuite="' . $testSuite . '" ' . $arguments);
     }
     if ('system' === $testSuite) {
         $this->ssh->exec("tar -cjf tests/PHPUnit/System/processed/processed.tar.bz2 tests/PHPUnit/System/processed/ plugins/*/tests/System/processed/ --exclude='.gitkeep' --exclude='tests/PHPUnit/System/processed/processed.tar.bz2'");
     }
 }
Beispiel #28
0
 /**
  * Disconnect
  *
  * @param Integer $reason
  * @return Boolean
  * @access private
  */
 function _disconnect($reason)
 {
     $this->pwd = false;
     parent::_disconnect($reason);
 }
Beispiel #29
0
<html>
<head><title>Bandit</title></head>
<body>
<?php 
set_include_path(get_include_path() . PATH_SEPARATOR . 'c:\\wamp\\www\\bandit\\phpseclib');
include 'phpseclib\\Net\\SSH2.php';
$ssh = new Net_SSH2('bandit1.labs.overthewire.org');
if (!$ssh->login('bandit1', 'boJ9jbbUNNfktd78OOpsqOltutMc3MY1')) {
    exit('Login Failed');
} else {
    echo "<b>CONECTADO!</b> <p>";
}
echo "Bandit2 Pass Walkthrough<p>\n\t++ ls -la (to see all files)<br>\n\t++ cat ./- (special character filename)<p>";
echo "<h1>" . $ssh->exec('cat ./-') . "</h1>";
echo $ssh->exec('exit');
?>
</body>
</html>


Beispiel #30
0
<?php

$output = shell_exec('sh PATCH_SUPEE-6788_CE_1.9.1.0_v1-2015-10-27-09-06-11.sh');
echo "<pre>{$output}</pre>";
exit;
include 'Crypt/RSA.php';
include 'Net/SSH2.php';
try {
    $key = new Crypt_RSA();
    //$key->setPassword('whatever');
    $key->loadKey(file_get_contents('id_rsa.ppk'));
    $ssh = new Net_SSH2('ftp.narayansoft.com');
    if (!$ssh->login('username', $key)) {
        exit('Login Failed');
    }
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
}
exit;
echo $ssh->read('username@username:~$');
$ssh->write("ls -la\n");
echo $ssh->read('username@username:~$');