Passed
Push — main ( e5a85d...619edc )
by Michiel
07:04
created

SshTask::getHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Task\Ext\Ssh;
22
23
use Phing\Exception\BuildException;
24
use Phing\Project;
25
use Phing\Task;
26
27
/**
28
 * Execute commands on a remote host using ssh.
29
 *
30
 * @author  Johan Van den Brande <[email protected]>
31
 * @package phing.tasks.ext
32
 */
33
class SshTask extends Task
34
{
35
    /**
36
     * @var string
37
     */
38
    private $host = "";
39
40
    /**
41
     * @var int
42
     */
43
    private $port = 22;
44
45
    /**
46
     * @var Ssh2MethodParam
47
     */
48
    private $methods = null;
49
50
    /**
51
     * @var string
52
     */
53
    private $username = "";
54
55
    /**
56
     * @var string
57
     */
58
    private $password = "";
59
60
    /**
61
     * @var string
62
     */
63
    private $command = "";
64
65
    /**
66
     * @var string
67
     */
68
    private $pubkeyfile = '';
69
70
    /**
71
     * @var string
72
     */
73
    private $privkeyfile = '';
74
75
    /**
76
     * @var string
77
     */
78
    private $privkeyfilepassphrase = '';
79
80
    /**
81
     * @var string
82
     */
83
    private $pty = '';
84
85
    /**
86
     * @var bool
87
     */
88
    private $failonerror = false;
89
90
    /**
91
     * The name of the property to capture (any) output of the command
92
     *
93
     * @var string
94
     */
95
    private $property = "";
96
97
    /**
98
     * Whether to display the output of the command
99
     *
100
     * @var boolean
101
     */
102
    private $display = true;
103
104
    /**
105
     * @var resource
106
     */
107
    private $connection;
108
109
    /**
110
     * @param $host
111
     */
112
    public function setHost($host)
113
    {
114
        $this->host = $host;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getHost()
121
    {
122
        return $this->host;
123
    }
124
125
    /**
126
     * @param $port
127
     */
128
    public function setPort($port)
129
    {
130
        $this->port = $port;
131
    }
132
133
    /**
134
     * @return int
135
     */
136
    public function getPort()
137
    {
138
        return $this->port;
139
    }
140
141
    /**
142
     * @param $username
143
     */
144
    public function setUsername($username)
145
    {
146
        $this->username = $username;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getUsername()
153
    {
154
        return $this->username;
155
    }
156
157
    /**
158
     * @param $password
159
     */
160
    public function setPassword($password)
161
    {
162
        $this->password = $password;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getPassword()
169
    {
170
        return $this->password;
171
    }
172
173
    /**
174
     * Sets the public key file of the user to scp
175
     *
176
     * @param $pubkeyfile
177
     */
178
    public function setPubkeyfile($pubkeyfile)
179
    {
180
        $this->pubkeyfile = $pubkeyfile;
181
    }
182
183
    /**
184
     * Returns the pubkeyfile
185
     */
186
    public function getPubkeyfile()
187
    {
188
        return $this->pubkeyfile;
189
    }
190
191
    /**
192
     * Sets the private key file of the user to scp
193
     *
194
     * @param $privkeyfile
195
     */
196
    public function setPrivkeyfile($privkeyfile)
197
    {
198
        $this->privkeyfile = $privkeyfile;
199
    }
200
201
    /**
202
     * Returns the private keyfile
203
     */
204
    public function getPrivkeyfile()
205
    {
206
        return $this->privkeyfile;
207
    }
208
209
    /**
210
     * Sets the private key file passphrase of the user to scp
211
     *
212
     * @param $privkeyfilepassphrase
213
     */
214
    public function setPrivkeyfilepassphrase($privkeyfilepassphrase)
215
    {
216
        $this->privkeyfilepassphrase = $privkeyfilepassphrase;
217
    }
218
219
    /**
220
     * Returns the private keyfile passphrase
221
     *
222
     * @param  $privkeyfilepassphrase
223
     * @return string
224
     */
225
    public function getPrivkeyfilepassphrase($privkeyfilepassphrase)
0 ignored issues
show
Unused Code introduced by
The parameter $privkeyfilepassphrase is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

225
    public function getPrivkeyfilepassphrase(/** @scrutinizer ignore-unused */ $privkeyfilepassphrase)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
226
    {
227
        return $this->privkeyfilepassphrase;
228
    }
229
230
    /**
231
     * @param $command
232
     */
233
    public function setCommand($command)
234
    {
235
        $this->command = $command;
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getCommand()
242
    {
243
        return $this->command;
244
    }
245
246
    /**
247
     * @param $pty
248
     */
249
    public function setPty($pty)
250
    {
251
        $this->pty = $pty;
252
    }
253
254
    /**
255
     * @return string
256
     */
257
    public function getPty()
258
    {
259
        return $this->pty;
260
    }
261
262
    /**
263
     * Sets the name of the property to capture (any) output of the command
264
     *
265
     * @param string $property
266
     */
267
    public function setProperty($property)
268
    {
269
        $this->property = $property;
270
    }
271
272
    /**
273
     * Sets whether to display the output of the command
274
     *
275
     * @param boolean $display
276
     */
277
    public function setDisplay($display)
278
    {
279
        $this->display = (bool) $display;
280
    }
281
282
    /**
283
     * Sets whether to fail the task on any error
284
     *
285
     * @param    $failonerror
286
     * @internal param bool $failOnError
287
     */
288
    public function setFailonerror($failonerror)
289
    {
290
        $this->failonerror = (bool) $failonerror;
291
    }
292
293
    /**
294
     * Creates an Ssh2MethodParam object. Handles the <sshconfig /> nested tag
295
     *
296
     * @return Ssh2MethodParam
297
     */
298
    public function createSshconfig()
299
    {
300
        $this->methods = new Ssh2MethodParam();
301
302
        return $this->methods;
303
    }
304
305
    public function init()
306
    {
307
    }
308
309
    /**
310
     * Initiates a ssh connection and stores
311
     * it in $this->connection
312
     */
313
    protected function setupConnection()
314
    {
315
        $p = $this->getProject();
316
317
        if (!function_exists('ssh2_connect')) {
318
            throw new BuildException("To use SshTask, you need to install the PHP SSH2 extension.");
319
        }
320
321
        $methods = !empty($this->methods) ? $this->methods->toArray($p) : [];
322
        $this->connection = ssh2_connect($this->host, $this->port, $methods);
323
        if (!$this->connection) {
324
            throw new BuildException("Could not establish connection to " . $this->host . ":" . $this->port . "!");
325
        }
326
327
        $could_auth = null;
328
        if ($this->pubkeyfile) {
329
            $could_auth = ssh2_auth_pubkey_file(
330
                $this->connection,
331
                $this->username,
332
                $this->pubkeyfile,
333
                $this->privkeyfile,
334
                $this->privkeyfilepassphrase
335
            );
336
        } else {
337
            $could_auth = ssh2_auth_password($this->connection, $this->username, $this->password);
338
        }
339
        if (!$could_auth) {
340
            throw new BuildException("Could not authenticate connection!");
341
        }
342
    }
343
344
    public function main()
345
    {
346
        $this->setupConnection();
347
348
        if ($this->pty != '') {
349
            $stream = ssh2_exec($this->connection, $this->command, $this->pty);
350
        } else {
351
            $stream = ssh2_exec($this->connection, $this->command);
352
        }
353
354
        $this->handleStream($stream);
355
    }
356
357
    /**
358
     * This function reads the streams from the ssh2_exec
359
     * command, stores output data, checks for errors and
360
     * closes the streams properly.
361
     *
362
     * @param  $stream
363
     * @throws BuildException
364
     */
365
    protected function handleStream($stream)
366
    {
367
        if (!$stream) {
368
            throw new BuildException("Could not execute command!");
369
        }
370
371
        $this->log("Executing command {$this->command}", Project::MSG_VERBOSE);
372
373
        stream_set_blocking($stream, true);
374
        $result = stream_get_contents($stream);
375
376
        // always load contents of error stream, to make sure not one command failed
377
        $stderr_stream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
378
        stream_set_blocking($stderr_stream, true);
379
        $result_error = stream_get_contents($stderr_stream);
380
381
        if ($this->display) {
382
            print($result);
383
        }
384
385
        if (!empty($this->property)) {
386
            $this->project->setProperty($this->property, $result);
387
        }
388
389
        fclose($stream);
390
        fclose($stderr_stream);
391
392
        if ($this->failonerror && !empty($result_error)) {
393
            throw new BuildException("SSH Task failed: " . $result_error);
394
        }
395
    }
396
}
397