Skip to content

Package: SshRemoteUpdate

SshRemoteUpdate

nameinstructionbranchcomplexitylinemethod
SshRemoteUpdate(File[])
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
remoteUpdate(String, String, String, int)
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
remoteUpdate(String, String, int, String)
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
remoteUpdate(String, String, int, String, String)
M: 28 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
update(SshSession)
M: 56 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package update.remote.ssh;
2:
3: import java.io.File;
4:
5: import com.jcraft.jsch.JSchException;
6:
7: import update.remote.exceptions.RemoteUpdateException;
8:
9: /**
10: * This class initiates a remote update on a fli4l router.
11: *
12: * @author Muri
13: *
14: */
15: public final class SshRemoteUpdate {
16:
17:         /**
18:          * message in exception.
19:          */
20:         private static final String CONNECTION_ERROR = "JSch connection error: ";
21:
22:         /**
23:          * private constructor.
24:          *
25:          * @param files
26:          * files
27:          */
28:         public SshRemoteUpdate(final File[] files) {
29:                 super();
30:                 this.files = files.clone();
31:         }
32:
33:         /**
34:          * Files to send.
35:          */
36:         private final transient File[] files;
37:
38:         /**
39:          * Main method to initiate an update.
40:          *
41:          * @param args
42:          * args
43:          * @throws JSchException
44:          * JSchException
45:          * @throws InterruptedException
46:          * InterruptedException
47:          * @throws IOException
48:          * IOException
49:          */
50:         // public static void main(final String[] args) throws JSchException, InterruptedException,
51:         // IOException {
52:         // final String user = "root";
53:         // final String host = "172.17.143.41";
54:         // final int port = 4102;
55:         // final String keyPath = "C:\\Users\\Muri\\.ssh\\firewall";
56:         // final String passphrase = "fli4l";
57:         // final String buildPath =
58:         // "N:\\201 Sync\\Dropbox\\Router_1\\fli4l-Testfield\\fli4l-3.6.2\\builds\\Firewall\\";
59:         // SshRemoteUpdate.remoteUpdate(user, host, port, keyPath, passphrase, buildPath);
60:         // }
61:
62:         /**
63:          * Connect with password.
64:          *
65:          * @param user
66:          * user
67:          * @param password
68:          * password
69:          * @param host
70:          * host
71:          * @param port
72:          * port
73:          * @throws RemoteUpdateException
74:          * ConnectionFailedException
75:          */
76:         public void remoteUpdate(final String user, final String password, final String host,
77:                         final int port) throws RemoteUpdateException {
78:                 final SshSession sshs;
79:                 try {
80:                         sshs = new SshSession(user, password, host, port);
81:                 } catch (final JSchException e) {
82:                         throw new RemoteUpdateException(CONNECTION_ERROR + e.getMessage()); // NOPMD nevermind
83:                                                                                                                                                                 // losing the stack
84:                                                                                                                                                                 // trace
85:
86:                 }
87:                 update(sshs);
88:         }
89:
90:         /**
91:          * Connect with PrivateKey and passphrase.
92:          *
93:          * @param user
94:          * user
95:          * @param host
96:          * host
97:          * @param port
98:          * port
99:          * @param keyPath
100:          * keyPath
101:          * @param passphrase
102:          * passphrase
103:          * @throws RemoteUpdateException
104:          * ConnectionFailedException
105:          */
106:         public void remoteUpdate(final String user, final String host, final int port,
107:                         final String keyPath, final String passphrase) throws RemoteUpdateException {
108:                 final SshSession sshs;
109:                 try {
110:                         sshs = new SshSession(user, host, port, keyPath, passphrase);
111:                 } catch (final JSchException e) {
112:                         throw new RemoteUpdateException(CONNECTION_ERROR + e.getMessage()); // NOPMD nevermind
113:                                                                                                                                                                 // losing the stack
114:                                                                                                                                                                 // trace
115:
116:                 }
117:                 update(sshs);
118:         }
119:
120:         /**
121:          * Connect with PrivateKey without passphrase.
122:          *
123:          * @param user
124:          * user
125:          * @param host
126:          * host
127:          * @param port
128:          * port
129:          * @param keyPath
130:          * keyPath
131:          * @throws RemoteUpdateException
132:          * ConnectionFailedException
133:          */
134:         public void remoteUpdate(final String user, final String host, final int port,
135:                         final String keyPath) throws RemoteUpdateException {
136:                 final SshSession sshs;
137:                 try {
138:                         sshs = new SshSession(user, host, port, keyPath);
139:                 } catch (final JSchException e) {
140:                         throw new RemoteUpdateException(CONNECTION_ERROR + e.getMessage()); // NOPMD nevermind
141:                                                                                                                                                                 // losing the stack
142:                                                                                                                                                                 // trace
143:
144:                 }
145:                 update(sshs);
146:         }
147:
148:         /**
149:          * updates a router when source directory and session are known.
150:          *
151:          * @param sshs
152:          * sshs
153:          * @throws RemoteUpdateException
154:          * ConnectionFailedException
155:          */
156:         private void update(final SshSession sshs) throws RemoteUpdateException {
157:                 try {
158:                         sshs.connect();
159:•                        for (File file : this.files) {
160:                                 final String filePath = file.getAbsolutePath();
161:                                 final String fileName = file.getName();
162:                                 ScpTo.scpTo(filePath, sshs, "/boot/" + fileName);
163:                         }
164:                         SshReboot.rebootNow(sshs);
165:                         sshs.disconnect();
166:                 } catch (final JSchException e) {
167:                         throw new RemoteUpdateException(CONNECTION_ERROR + e.getMessage()); // NOPMD nevermind
168:                                                                                                                                                                 // losing the stack
169:                                                                                                                                                                 // trace
170:                 }
171:         }
172: }