Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SSHByPassword |
|
| 2.5;2.5 |
1 | /** | |
2 | * Copyright (c) 2014-2015, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.ssh; | |
31 | ||
32 | import com.jcabi.aspects.RetryOnFailure; | |
33 | import com.jcabi.aspects.Tv; | |
34 | import com.jcabi.log.Logger; | |
35 | import com.jcraft.jsch.JSch; | |
36 | import com.jcraft.jsch.JSchException; | |
37 | import com.jcraft.jsch.Session; | |
38 | import java.io.IOException; | |
39 | import java.net.UnknownHostException; | |
40 | import java.util.concurrent.TimeUnit; | |
41 | import lombok.EqualsAndHashCode; | |
42 | import lombok.ToString; | |
43 | ||
44 | /** | |
45 | * SSH channel with authentication by password. | |
46 | * @author Georgy Vlasov (wlasowegor@gmail.com) | |
47 | * @version $Id: 382a606c7c722429fb5b454c43307cd825d0d3d1 $ | |
48 | * @since 1.4 | |
49 | * @see SSH For SSH channel with authenticaton using private key. | |
50 | */ | |
51 | 7 | @ToString |
52 | 0 | @EqualsAndHashCode(of = { "password" }, callSuper = true) |
53 | public final class SSHByPassword extends AbstractSSHShell { | |
54 | ||
55 | /** | |
56 | * User password. | |
57 | */ | |
58 | private final transient String password; | |
59 | ||
60 | /** | |
61 | * Constructor. | |
62 | * @param adr IP address | |
63 | * @param prt Port of server | |
64 | * @param user Login | |
65 | * @param passwd Password | |
66 | * @throws UnknownHostException If fails | |
67 | * @checkstyle ParameterNumberCheck (6 lines) | |
68 | */ | |
69 | public SSHByPassword(final String adr, final int prt, | |
70 | final String user, final String passwd) | |
71 | throws UnknownHostException { | |
72 | 1 | super(adr, prt, user); |
73 | 1 | this.password = passwd; |
74 | 1 | } |
75 | ||
76 | // @checkstyle ProtectedMethodInFinalClassCheck (10 lines) | |
77 | @Override | |
78 | @RetryOnFailure( | |
79 | attempts = Tv.SEVEN, | |
80 | delay = 1, | |
81 | unit = TimeUnit.MINUTES, | |
82 | verbose = false, | |
83 | randomize = true, | |
84 | types = IOException.class | |
85 | ) | |
86 | protected Session session() throws IOException { | |
87 | try { | |
88 | 1 | JSch.setConfig("StrictHostKeyChecking", "no"); |
89 | 1 | JSch.setLogger(new JschLogger()); |
90 | 1 | final JSch jsch = new JSch(); |
91 | 1 | Logger.debug( |
92 | this, | |
93 | "Opening SSH session to %s@%s:%s (auth with password)...", | |
94 | this.getLogin(), this.getAddr(), this.getPort() | |
95 | ); | |
96 | 1 | final Session session = jsch.getSession( |
97 | this.getLogin(), this.getAddr(), this.getPort() | |
98 | ); | |
99 | 1 | session.setPassword(this.password); |
100 | 1 | session.setServerAliveInterval( |
101 | (int) TimeUnit.SECONDS.toMillis(Tv.TEN) | |
102 | ); | |
103 | 1 | session.setServerAliveCountMax(Tv.MILLION); |
104 | 1 | session.connect(); |
105 | 1 | return session; |
106 | 0 | } catch (final JSchException ex) { |
107 | 0 | throw new IOException(ex); |
108 | } | |
109 | } | |
110 | } |