forked from cBackup/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetSsh.php
137 lines (117 loc) · 3.52 KB
/
NetSsh.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* This file is part of cBackup, network equipment configuration backup tool
* Copyright (C) 2017, Oļegs Čapligins, Imants Černovs, Dmitrijs Galočkins
*
* cBackup is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace app\components;
use phpseclib\File\ANSI;
use phpseclib\Net\SSH2;
use yii\helpers\Json;
/**
* @package app\components
*/
class NetSsh
{
/**
* @var \phpseclib\Net\SSH2
*/
private $ssh;
/**
* @var string
*/
private $ip;
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $password;
/**
* @var int
*/
private $port;
/**
* @var int
*/
private $timeout = 2;
/**
* NetSsh constructor.
*/
public function __construct()
{
$this->ip = \Y::param('javaHost');
$this->port = \Y::param('javaSchedulerPort');
$this->username = \Y::param('javaSchedulerUsername');
$this->password = \Y::param('javaSchedulerPassword');
}
/**
* Init SSH wrapper
*
* @param array $options
* @return $this
* @throws \Exception
*/
public function init(array $options = [])
{
/** Set custom init options */
array_walk($options, function($value, $option) { $this->$option = $value; });
/** Connect to device */
$this->ssh = new SSH2($this->ip, $this->port, $this->timeout);
/** Show exception if can not login */
if (!$this->ssh->login($this->username, $this->password)) {
throw new \Exception("Authentication failed. Host:{$this->ip}:{$this->port}. Check SSH credentials");
}
return $this;
}
/**
* Execute command using exec command
*
* @param string $command
* @return string
*/
public function exec(string $command)
{
return trim($this->ssh->exec($command));
}
/**
* Execute command by parsing terminal output
*
* @param string $command
* @return array
* @throws \Exception
*/
public function schedulerExec(string $command):array
{
/** Execute command */
$this->ssh->read('/.*[>]\s$/', $this->ssh::READ_REGEX);
$this->ssh->write("{$command}\n");
$this->ssh->setTimeout(10);
/** Read command output */
$output = $this->ssh->read('/.*[>]\s$/', $this->ssh::READ_REGEX);
/** Show console output if error occurs */
if (!preg_match('/{.*}/i', $output, $json)) {
$ansi = new ANSI();
$ansi->appendString($output);
$prep_output = htmlspecialchars_decode(strip_tags($ansi->getScreen()));
$error_array = explode("\n", $prep_output);
$error_text = (array_key_exists(1, $error_array) && !empty($error_array[1])) ? $error_array[1] : $prep_output;
throw new \Exception($error_text);
}
return Json::decode($json[0]);
}
}