Mercurial > 510Connectbot
comparison src/com/trilead/ssh2/SCPClient.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
1 | |
2 package com.trilead.ssh2; | |
3 | |
4 import java.io.BufferedInputStream; | |
5 import java.io.BufferedOutputStream; | |
6 import java.io.File; | |
7 import java.io.FileInputStream; | |
8 import java.io.FileOutputStream; | |
9 import java.io.IOException; | |
10 import java.io.InputStream; | |
11 import java.io.OutputStream; | |
12 | |
13 /** | |
14 * A very basic <code>SCPClient</code> that can be used to copy files from/to | |
15 * the SSH-2 server. On the server side, the "scp" program must be in the PATH. | |
16 * <p> | |
17 * This scp client is thread safe - you can download (and upload) different sets | |
18 * of files concurrently without any troubles. The <code>SCPClient</code> is | |
19 * actually mapping every request to a distinct {@link Session}. | |
20 * | |
21 * @author Christian Plattner, plattner@trilead.com | |
22 * @version $Id: SCPClient.java,v 1.2 2008/04/01 12:38:09 cplattne Exp $ | |
23 */ | |
24 | |
25 public class SCPClient { | |
26 Connection conn; | |
27 | |
28 class LenNamePair { | |
29 long length; | |
30 String filename; | |
31 } | |
32 | |
33 public SCPClient(Connection conn) { | |
34 if (conn == null) | |
35 throw new IllegalArgumentException("Cannot accept null argument!"); | |
36 | |
37 this.conn = conn; | |
38 } | |
39 | |
40 private void readResponse(InputStream is) throws IOException { | |
41 int c = is.read(); | |
42 | |
43 if (c == 0) | |
44 return; | |
45 | |
46 if (c == -1) | |
47 throw new IOException("Remote scp terminated unexpectedly."); | |
48 | |
49 if ((c != 1) && (c != 2)) | |
50 throw new IOException("Remote scp sent illegal error code."); | |
51 | |
52 if (c == 2) | |
53 throw new IOException("Remote scp terminated with error."); | |
54 | |
55 String err = receiveLine(is); | |
56 throw new IOException("Remote scp terminated with error (" + err + ")."); | |
57 } | |
58 | |
59 private String receiveLine(InputStream is) throws IOException { | |
60 StringBuffer sb = new StringBuffer(30); | |
61 | |
62 while (true) { | |
63 /* | |
64 * This is a random limit - if your path names are longer, then | |
65 * adjust it | |
66 */ | |
67 if (sb.length() > 8192) | |
68 throw new IOException("Remote scp sent a too long line"); | |
69 | |
70 int c = is.read(); | |
71 | |
72 if (c < 0) | |
73 throw new IOException("Remote scp terminated unexpectedly."); | |
74 | |
75 if (c == '\n') | |
76 break; | |
77 | |
78 sb.append((char) c); | |
79 } | |
80 | |
81 return sb.toString(); | |
82 } | |
83 | |
84 private LenNamePair parseCLine(String line) throws IOException { | |
85 /* Minimum line: "xxxx y z" ---> 8 chars */ | |
86 long len; | |
87 | |
88 if (line.length() < 8) | |
89 throw new IOException("Malformed C line sent by remote SCP binary, line too short."); | |
90 | |
91 if ((line.charAt(4) != ' ') || (line.charAt(5) == ' ')) | |
92 throw new IOException("Malformed C line sent by remote SCP binary."); | |
93 | |
94 int length_name_sep = line.indexOf(' ', 5); | |
95 | |
96 if (length_name_sep == -1) | |
97 throw new IOException("Malformed C line sent by remote SCP binary."); | |
98 | |
99 String length_substring = line.substring(5, length_name_sep); | |
100 String name_substring = line.substring(length_name_sep + 1); | |
101 | |
102 if ((length_substring.length() <= 0) || (name_substring.length() <= 0)) | |
103 throw new IOException("Malformed C line sent by remote SCP binary."); | |
104 | |
105 if ((6 + length_substring.length() + name_substring.length()) != line.length()) | |
106 throw new IOException("Malformed C line sent by remote SCP binary."); | |
107 | |
108 try { | |
109 len = Long.parseLong(length_substring); | |
110 } | |
111 catch (NumberFormatException e) { | |
112 throw new IOException("Malformed C line sent by remote SCP binary, cannot parse file length."); | |
113 } | |
114 | |
115 if (len < 0) | |
116 throw new IOException("Malformed C line sent by remote SCP binary, illegal file length."); | |
117 | |
118 LenNamePair lnp = new LenNamePair(); | |
119 lnp.length = len; | |
120 lnp.filename = name_substring; | |
121 return lnp; | |
122 } | |
123 | |
124 private void sendBytes(Session sess, byte[] data, String fileName, String mode) throws IOException { | |
125 OutputStream os = sess.getStdin(); | |
126 InputStream is = new BufferedInputStream(sess.getStdout(), 512); | |
127 readResponse(is); | |
128 String cline = "C" + mode + " " + data.length + " " + fileName + "\n"; | |
129 os.write(cline.getBytes("ISO-8859-1")); | |
130 os.flush(); | |
131 readResponse(is); | |
132 os.write(data, 0, data.length); | |
133 os.write(0); | |
134 os.flush(); | |
135 readResponse(is); | |
136 os.write("E\n".getBytes("ISO-8859-1")); | |
137 os.flush(); | |
138 } | |
139 | |
140 private void sendFiles(Session sess, String[] files, String[] remoteFiles, String mode) throws IOException { | |
141 byte[] buffer = new byte[8192]; | |
142 OutputStream os = new BufferedOutputStream(sess.getStdin(), 40000); | |
143 InputStream is = new BufferedInputStream(sess.getStdout(), 512); | |
144 readResponse(is); | |
145 | |
146 for (int i = 0; i < files.length; i++) { | |
147 File f = new File(files[i]); | |
148 long remain = f.length(); | |
149 String remoteName; | |
150 | |
151 if ((remoteFiles != null) && (remoteFiles.length > i) && (remoteFiles[i] != null)) | |
152 remoteName = remoteFiles[i]; | |
153 else | |
154 remoteName = f.getName(); | |
155 | |
156 String cline = "C" + mode + " " + remain + " " + remoteName + "\n"; | |
157 os.write(cline.getBytes("ISO-8859-1")); | |
158 os.flush(); | |
159 readResponse(is); | |
160 FileInputStream fis = null; | |
161 | |
162 try { | |
163 fis = new FileInputStream(f); | |
164 | |
165 while (remain > 0) { | |
166 int trans; | |
167 | |
168 if (remain > buffer.length) | |
169 trans = buffer.length; | |
170 else | |
171 trans = (int) remain; | |
172 | |
173 if (fis.read(buffer, 0, trans) != trans) | |
174 throw new IOException("Cannot read enough from local file " + files[i]); | |
175 | |
176 os.write(buffer, 0, trans); | |
177 remain -= trans; | |
178 } | |
179 } | |
180 finally { | |
181 if (fis != null) | |
182 fis.close(); | |
183 } | |
184 | |
185 os.write(0); | |
186 os.flush(); | |
187 readResponse(is); | |
188 } | |
189 | |
190 os.write("E\n".getBytes("ISO-8859-1")); | |
191 os.flush(); | |
192 } | |
193 | |
194 private void receiveFiles(Session sess, OutputStream[] targets) throws IOException { | |
195 byte[] buffer = new byte[8192]; | |
196 OutputStream os = new BufferedOutputStream(sess.getStdin(), 512); | |
197 InputStream is = new BufferedInputStream(sess.getStdout(), 40000); | |
198 os.write(0x0); | |
199 os.flush(); | |
200 | |
201 for (int i = 0; i < targets.length; i++) { | |
202 LenNamePair lnp = null; | |
203 | |
204 while (true) { | |
205 int c = is.read(); | |
206 | |
207 if (c < 0) | |
208 throw new IOException("Remote scp terminated unexpectedly."); | |
209 | |
210 String line = receiveLine(is); | |
211 | |
212 if (c == 'T') { | |
213 /* Ignore modification times */ | |
214 continue; | |
215 } | |
216 | |
217 if ((c == 1) || (c == 2)) | |
218 throw new IOException("Remote SCP error: " + line); | |
219 | |
220 if (c == 'C') { | |
221 lnp = parseCLine(line); | |
222 break; | |
223 } | |
224 | |
225 throw new IOException("Remote SCP error: " + ((char) c) + line); | |
226 } | |
227 | |
228 os.write(0x0); | |
229 os.flush(); | |
230 long remain = lnp.length; | |
231 | |
232 while (remain > 0) { | |
233 int trans; | |
234 | |
235 if (remain > buffer.length) | |
236 trans = buffer.length; | |
237 else | |
238 trans = (int) remain; | |
239 | |
240 int this_time_received = is.read(buffer, 0, trans); | |
241 | |
242 if (this_time_received < 0) { | |
243 throw new IOException("Remote scp terminated connection unexpectedly"); | |
244 } | |
245 | |
246 targets[i].write(buffer, 0, this_time_received); | |
247 remain -= this_time_received; | |
248 } | |
249 | |
250 readResponse(is); | |
251 os.write(0x0); | |
252 os.flush(); | |
253 } | |
254 } | |
255 | |
256 private void receiveFiles(Session sess, String[] files, String target) throws IOException { | |
257 byte[] buffer = new byte[8192]; | |
258 OutputStream os = new BufferedOutputStream(sess.getStdin(), 512); | |
259 InputStream is = new BufferedInputStream(sess.getStdout(), 40000); | |
260 os.write(0x0); | |
261 os.flush(); | |
262 | |
263 for (int i = 0; i < files.length; i++) { | |
264 LenNamePair lnp = null; | |
265 | |
266 while (true) { | |
267 int c = is.read(); | |
268 | |
269 if (c < 0) | |
270 throw new IOException("Remote scp terminated unexpectedly."); | |
271 | |
272 String line = receiveLine(is); | |
273 | |
274 if (c == 'T') { | |
275 /* Ignore modification times */ | |
276 continue; | |
277 } | |
278 | |
279 if ((c == 1) || (c == 2)) | |
280 throw new IOException("Remote SCP error: " + line); | |
281 | |
282 if (c == 'C') { | |
283 lnp = parseCLine(line); | |
284 break; | |
285 } | |
286 | |
287 throw new IOException("Remote SCP error: " + ((char) c) + line); | |
288 } | |
289 | |
290 os.write(0x0); | |
291 os.flush(); | |
292 File f = new File(target + File.separatorChar + lnp.filename); | |
293 FileOutputStream fop = null; | |
294 | |
295 try { | |
296 fop = new FileOutputStream(f); | |
297 long remain = lnp.length; | |
298 | |
299 while (remain > 0) { | |
300 int trans; | |
301 | |
302 if (remain > buffer.length) | |
303 trans = buffer.length; | |
304 else | |
305 trans = (int) remain; | |
306 | |
307 int this_time_received = is.read(buffer, 0, trans); | |
308 | |
309 if (this_time_received < 0) { | |
310 throw new IOException("Remote scp terminated connection unexpectedly"); | |
311 } | |
312 | |
313 fop.write(buffer, 0, this_time_received); | |
314 remain -= this_time_received; | |
315 } | |
316 } | |
317 finally { | |
318 if (fop != null) | |
319 fop.close(); | |
320 } | |
321 | |
322 readResponse(is); | |
323 os.write(0x0); | |
324 os.flush(); | |
325 } | |
326 } | |
327 | |
328 /** | |
329 * Copy a local file to a remote directory, uses mode 0600 when creating the | |
330 * file on the remote side. | |
331 * | |
332 * @param localFile | |
333 * Path and name of local file. | |
334 * @param remoteTargetDirectory | |
335 * Remote target directory. Use an empty string to specify the | |
336 * default directory. | |
337 * | |
338 * @throws IOException | |
339 */ | |
340 public void put(String localFile, String remoteTargetDirectory) throws IOException { | |
341 put(new String[] { localFile }, remoteTargetDirectory, "0600"); | |
342 } | |
343 | |
344 /** | |
345 * Copy a set of local files to a remote directory, uses mode 0600 when | |
346 * creating files on the remote side. | |
347 * | |
348 * @param localFiles | |
349 * Paths and names of local file names. | |
350 * @param remoteTargetDirectory | |
351 * Remote target directory. Use an empty string to specify the | |
352 * default directory. | |
353 * | |
354 * @throws IOException | |
355 */ | |
356 | |
357 public void put(String[] localFiles, String remoteTargetDirectory) throws IOException { | |
358 put(localFiles, remoteTargetDirectory, "0600"); | |
359 } | |
360 | |
361 /** | |
362 * Copy a local file to a remote directory, uses the specified mode when | |
363 * creating the file on the remote side. | |
364 * | |
365 * @param localFile | |
366 * Path and name of local file. | |
367 * @param remoteTargetDirectory | |
368 * Remote target directory. Use an empty string to specify the | |
369 * default directory. | |
370 * @param mode | |
371 * a four digit string (e.g., 0644, see "man chmod", "man open") | |
372 * @throws IOException | |
373 */ | |
374 public void put(String localFile, String remoteTargetDirectory, String mode) throws IOException { | |
375 put(new String[] { localFile }, remoteTargetDirectory, mode); | |
376 } | |
377 | |
378 /** | |
379 * Copy a local file to a remote directory, uses the specified mode and | |
380 * remote filename when creating the file on the remote side. | |
381 * | |
382 * @param localFile | |
383 * Path and name of local file. | |
384 * @param remoteFileName | |
385 * The name of the file which will be created in the remote | |
386 * target directory. | |
387 * @param remoteTargetDirectory | |
388 * Remote target directory. Use an empty string to specify the | |
389 * default directory. | |
390 * @param mode | |
391 * a four digit string (e.g., 0644, see "man chmod", "man open") | |
392 * @throws IOException | |
393 */ | |
394 public void put(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) | |
395 throws IOException { | |
396 put(new String[] { localFile }, new String[] { remoteFileName }, remoteTargetDirectory, mode); | |
397 } | |
398 | |
399 /** | |
400 * Create a remote file and copy the contents of the passed byte array into | |
401 * it. Uses mode 0600 for creating the remote file. | |
402 * | |
403 * @param data | |
404 * the data to be copied into the remote file. | |
405 * @param remoteFileName | |
406 * The name of the file which will be created in the remote | |
407 * target directory. | |
408 * @param remoteTargetDirectory | |
409 * Remote target directory. Use an empty string to specify the | |
410 * default directory. | |
411 * @throws IOException | |
412 */ | |
413 | |
414 public void put(byte[] data, String remoteFileName, String remoteTargetDirectory) throws IOException { | |
415 put(data, remoteFileName, remoteTargetDirectory, "0600"); | |
416 } | |
417 | |
418 /** | |
419 * Create a remote file and copy the contents of the passed byte array into | |
420 * it. The method use the specified mode when creating the file on the | |
421 * remote side. | |
422 * | |
423 * @param data | |
424 * the data to be copied into the remote file. | |
425 * @param remoteFileName | |
426 * The name of the file which will be created in the remote | |
427 * target directory. | |
428 * @param remoteTargetDirectory | |
429 * Remote target directory. Use an empty string to specify the | |
430 * default directory. | |
431 * @param mode | |
432 * a four digit string (e.g., 0644, see "man chmod", "man open") | |
433 * @throws IOException | |
434 */ | |
435 public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode) throws IOException { | |
436 Session sess = null; | |
437 | |
438 if ((remoteFileName == null) || (remoteTargetDirectory == null) || (mode == null)) | |
439 throw new IllegalArgumentException("Null argument."); | |
440 | |
441 if (mode.length() != 4) | |
442 throw new IllegalArgumentException("Invalid mode."); | |
443 | |
444 for (int i = 0; i < mode.length(); i++) | |
445 if (Character.isDigit(mode.charAt(i)) == false) | |
446 throw new IllegalArgumentException("Invalid mode."); | |
447 | |
448 remoteTargetDirectory = remoteTargetDirectory.trim(); | |
449 remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : "."; | |
450 String cmd = "scp -t -d " + remoteTargetDirectory; | |
451 | |
452 try { | |
453 sess = conn.openSession(); | |
454 sess.execCommand(cmd); | |
455 sendBytes(sess, data, remoteFileName, mode); | |
456 } | |
457 catch (IOException e) { | |
458 throw(IOException) new IOException("Error during SCP transfer.").initCause(e); | |
459 } | |
460 finally { | |
461 if (sess != null) | |
462 sess.close(); | |
463 } | |
464 } | |
465 | |
466 /** | |
467 * Copy a set of local files to a remote directory, uses the specified mode | |
468 * when creating the files on the remote side. | |
469 * | |
470 * @param localFiles | |
471 * Paths and names of the local files. | |
472 * @param remoteTargetDirectory | |
473 * Remote target directory. Use an empty string to specify the | |
474 * default directory. | |
475 * @param mode | |
476 * a four digit string (e.g., 0644, see "man chmod", "man open") | |
477 * @throws IOException | |
478 */ | |
479 public void put(String[] localFiles, String remoteTargetDirectory, String mode) throws IOException { | |
480 put(localFiles, null, remoteTargetDirectory, mode); | |
481 } | |
482 | |
483 public void put(String[] localFiles, String[] remoteFiles, String remoteTargetDirectory, String mode) | |
484 throws IOException { | |
485 Session sess = null; | |
486 | |
487 /* | |
488 * remoteFiles may be null, indicating that the local filenames shall be | |
489 * used | |
490 */ | |
491 | |
492 if ((localFiles == null) || (remoteTargetDirectory == null) || (mode == null)) | |
493 throw new IllegalArgumentException("Null argument."); | |
494 | |
495 if (mode.length() != 4) | |
496 throw new IllegalArgumentException("Invalid mode."); | |
497 | |
498 for (int i = 0; i < mode.length(); i++) | |
499 if (Character.isDigit(mode.charAt(i)) == false) | |
500 throw new IllegalArgumentException("Invalid mode."); | |
501 | |
502 if (localFiles.length == 0) | |
503 return; | |
504 | |
505 remoteTargetDirectory = remoteTargetDirectory.trim(); | |
506 remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : "."; | |
507 String cmd = "scp -t -d " + remoteTargetDirectory; | |
508 | |
509 for (int i = 0; i < localFiles.length; i++) { | |
510 if (localFiles[i] == null) | |
511 throw new IllegalArgumentException("Cannot accept null filename."); | |
512 } | |
513 | |
514 try { | |
515 sess = conn.openSession(); | |
516 sess.execCommand(cmd); | |
517 sendFiles(sess, localFiles, remoteFiles, mode); | |
518 } | |
519 catch (IOException e) { | |
520 throw(IOException) new IOException("Error during SCP transfer.").initCause(e); | |
521 } | |
522 finally { | |
523 if (sess != null) | |
524 sess.close(); | |
525 } | |
526 } | |
527 | |
528 /** | |
529 * Download a file from the remote server to a local directory. | |
530 * | |
531 * @param remoteFile | |
532 * Path and name of the remote file. | |
533 * @param localTargetDirectory | |
534 * Local directory to put the downloaded file. | |
535 * | |
536 * @throws IOException | |
537 */ | |
538 public void get(String remoteFile, String localTargetDirectory) throws IOException { | |
539 get(new String[] { remoteFile }, localTargetDirectory); | |
540 } | |
541 | |
542 /** | |
543 * Download a file from the remote server and pipe its contents into an | |
544 * <code>OutputStream</code>. Please note that, to enable flexible usage | |
545 * of this method, the <code>OutputStream</code> will not be closed nor | |
546 * flushed. | |
547 * | |
548 * @param remoteFile | |
549 * Path and name of the remote file. | |
550 * @param target | |
551 * OutputStream where the contents of the file will be sent to. | |
552 * @throws IOException | |
553 */ | |
554 public void get(String remoteFile, OutputStream target) throws IOException { | |
555 get(new String[] { remoteFile }, new OutputStream[] { target }); | |
556 } | |
557 | |
558 private void get(String remoteFiles[], OutputStream[] targets) throws IOException { | |
559 Session sess = null; | |
560 | |
561 if ((remoteFiles == null) || (targets == null)) | |
562 throw new IllegalArgumentException("Null argument."); | |
563 | |
564 if (remoteFiles.length != targets.length) | |
565 throw new IllegalArgumentException("Length of arguments does not match."); | |
566 | |
567 if (remoteFiles.length == 0) | |
568 return; | |
569 | |
570 String cmd = "scp -f"; | |
571 | |
572 for (int i = 0; i < remoteFiles.length; i++) { | |
573 if (remoteFiles[i] == null) | |
574 throw new IllegalArgumentException("Cannot accept null filename."); | |
575 | |
576 String tmp = remoteFiles[i].trim(); | |
577 | |
578 if (tmp.length() == 0) | |
579 throw new IllegalArgumentException("Cannot accept empty filename."); | |
580 | |
581 cmd += (" " + tmp); | |
582 } | |
583 | |
584 try { | |
585 sess = conn.openSession(); | |
586 sess.execCommand(cmd); | |
587 receiveFiles(sess, targets); | |
588 } | |
589 catch (IOException e) { | |
590 throw(IOException) new IOException("Error during SCP transfer.").initCause(e); | |
591 } | |
592 finally { | |
593 if (sess != null) | |
594 sess.close(); | |
595 } | |
596 } | |
597 | |
598 /** | |
599 * Download a set of files from the remote server to a local directory. | |
600 * | |
601 * @param remoteFiles | |
602 * Paths and names of the remote files. | |
603 * @param localTargetDirectory | |
604 * Local directory to put the downloaded files. | |
605 * | |
606 * @throws IOException | |
607 */ | |
608 public void get(String remoteFiles[], String localTargetDirectory) throws IOException { | |
609 Session sess = null; | |
610 | |
611 if ((remoteFiles == null) || (localTargetDirectory == null)) | |
612 throw new IllegalArgumentException("Null argument."); | |
613 | |
614 if (remoteFiles.length == 0) | |
615 return; | |
616 | |
617 String cmd = "scp -f"; | |
618 | |
619 for (int i = 0; i < remoteFiles.length; i++) { | |
620 if (remoteFiles[i] == null) | |
621 throw new IllegalArgumentException("Cannot accept null filename."); | |
622 | |
623 String tmp = remoteFiles[i].trim(); | |
624 | |
625 if (tmp.length() == 0) | |
626 throw new IllegalArgumentException("Cannot accept empty filename."); | |
627 | |
628 cmd += (" " + tmp); | |
629 } | |
630 | |
631 try { | |
632 sess = conn.openSession(); | |
633 sess.execCommand(cmd); | |
634 receiveFiles(sess, remoteFiles, localTargetDirectory); | |
635 } | |
636 catch (IOException e) { | |
637 throw(IOException) new IOException("Error during SCP transfer.").initCause(e); | |
638 } | |
639 finally { | |
640 if (sess != null) | |
641 sess.close(); | |
642 } | |
643 } | |
644 } |