-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathasync_test.php
More file actions
89 lines (67 loc) · 2.81 KB
/
async_test.php
File metadata and controls
89 lines (67 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
if (!isset($_SERVER["argc"]) || !$_SERVER["argc"])
{
echo "This file is intended to be run from the command-line.";
exit();
}
// Temporary root.
$rootpath = str_replace("\\", "/", dirname(__FILE__));
require_once $rootpath . "/support/cli.php";
// Check enabled extensions.
if (!extension_loaded("openssl")) CLI::DisplayError("The 'openssl' PHP module is not enabled. Please update the file '" . (php_ini_loaded_file() !== false ? php_ini_loaded_file() : "php.ini") . "' to enable the module.");
require_once $rootpath . "/support/phpseclib/Crypt/RSA.php";
require_once $rootpath . "/support/phpseclib/Math/BigInteger.php";
require_once $rootpath . "/support/phpseclib/Net/SSH2.php";
require_once $rootpath . "/support/phpseclib/Net/PatchedSSH2.php";
require_once $rootpath . "/support/phpseclib/Net/AsyncSSH2.php";
$path = get_include_path();
if (strpos($path, PATH_SEPARATOR . $rootpath . "/support/phpseclib/") === false) set_include_path($path . PATH_SEPARATOR . $rootpath . "/support/phpseclib/");
$ssh = new Async_Net_SSH2($argv[1], 22);
$publickey = $ssh->getServerPublicHostKey();
$rsa = new Crypt_RSA();
$rsa->loadKey($publickey);
echo $rsa->getPublicKeyFingerprint() . "\n";
$filename = $rootpath . "/ssh-keys/" . $argv[2] . ".json";
$data2 = json_decode(file_get_contents($filename), true);
$rsa = new Crypt_RSA();
if (!$rsa->loadKey($data2["publickey"])) CLI::DisplayError("An error occurred while loading the public key '" . $argv[2] . "'.");
if (!$rsa->loadKey($data2["privatekey"])) CLI::DisplayError("An error occurred while loading the private key '" . $argv[2] . "'.");
if (!$ssh->login($argv[3], $rsa)) CLI::DisplayError("SSH login to '" . $argv[1] . ":22' with RSA private key failed.", array("success" =>false, "error" => "See error above.", "errorcode" => "login_failed", "info" => $ssh->getErrors()));
$ssh->setTerminal("xterm");
// Run a process that takes a while to complete (e.g. sleep).
$ssh->startShell();
$ssh->setBlocking(false);
var_dump($ssh->write("sleep 30\n"));
$startts = time();
$lastwints = time();
do
{
$timeout = 3;
$readfps = array($ssh->getStream());
$writefps = array();
if ($ssh->wantWrite()) $writefps[] = $ssh->getStream();
$exceptfps = NULL;
stream_select($readfps, $writefps, $exceptfps, $timeout);
echo "[SSH]\n";
$ssh->sendWrite();
// Clear the read buffer.
do
{
$result = $ssh->readAsync();
if (is_string($result)) echo $result;
else if (feof($ssh->getStream())) $ssh->disconnect();
} while (is_string($result));
// Update the window size every few seconds.
if ($lastwints < time() - 5)
{
echo "[WIN]\n";
$ssh->setWindowSize(mt_rand(80, 100), mt_rand(25, 50));
$lastwints = time();
}
if ($startts < time() - 60)
{
$ssh->write("exit\n");
$startts = time();
}
} while ($ssh->hasShell());
?>