1 2 package ch.ethz.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 24 25 public class SCPClient 26 { 27 Connection conn; 28 29 class LenNamePair 30 { 31 long length; 32 String filename; 33 } 34 35 public SCPClient(Connection conn) 36 { 37 if (conn == null) 38 throw new IllegalArgumentException ("Cannot accept null argument!"); 39 this.conn = conn; 40 } 41 42 private void readResponse(InputStream is) throws IOException 43 { 44 int c = is.read(); 45 46 if (c == 0) 47 return; 48 49 if (c == -1) 50 throw new IOException ("Remote scp terminated unexpectedly."); 51 52 if ((c != 1) && (c != 2)) 53 throw new IOException ("Remote scp sent illegal error code."); 54 55 if (c == 2) 56 throw new IOException ("Remote scp terminated with error."); 57 58 String err = receiveLine(is); 59 throw new IOException ("Remote scp terminated with error (" + err + ")."); 60 } 61 62 private String receiveLine(InputStream is) throws IOException 63 { 64 StringBuffer sb = new StringBuffer (30); 65 66 while (true) 67 { 68 69 70 if (sb.length() > 8192) 71 throw new IOException ("Remote scp sent a too long line"); 72 73 int c = is.read(); 74 75 if (c < 0) 76 throw new IOException ("Remote scp terminated unexpectedly."); 77 78 if (c == '\n') 79 break; 80 81 sb.append((char) c); 82 83 } 84 return sb.toString(); 85 } 86 87 private LenNamePair parseCLine(String line) throws IOException 88 { 89 90 91 long len; 92 93 if (line.length() < 8) 94 throw new IOException ("Malformed C line sent by remote SCP binary, line too short."); 95 96 if ((line.charAt(4) != ' ') || (line.charAt(5) == ' ')) 97 throw new IOException ("Malformed C line sent by remote SCP binary."); 98 99 int length_name_sep = line.indexOf(' ', 5); 100 101 if (length_name_sep == -1) 102 throw new IOException ("Malformed C line sent by remote SCP binary."); 103 104 String length_substring = line.substring(5, length_name_sep); 105 String name_substring = line.substring(length_name_sep + 1); 106 107 if ((length_substring.length() <= 0) || (name_substring.length() <= 0)) 108 throw new IOException ("Malformed C line sent by remote SCP binary."); 109 110 if ((6 + length_substring.length() + name_substring.length()) != line.length()) 111 throw new IOException ("Malformed C line sent by remote SCP binary."); 112 113 try 114 { 115 len = Long.parseLong(length_substring); 116 } 117 catch (NumberFormatException e) 118 { 119 throw new IOException ("Malformed C line sent by remote SCP binary, cannot parse file length."); 120 } 121 122 if (len < 0) 123 throw new IOException ("Malformed C line sent by remote SCP binary, illegal file length."); 124 125 LenNamePair lnp = new LenNamePair(); 126 lnp.length = len; 127 lnp.filename = name_substring; 128 129 return lnp; 130 } 131 132 private void sendBytes(Session sess, byte[] data, String fileName, String mode) throws IOException 133 { 134 OutputStream os = sess.getStdin(); 135 InputStream is = new BufferedInputStream (sess.getStdout(), 512); 136 137 readResponse(is); 138 139 String cline = "C" + mode + " " + data.length + " " + fileName + "\n"; 140 141 os.write(cline.getBytes()); 142 os.flush(); 143 144 readResponse(is); 145 146 os.write(data, 0, data.length); 147 os.write(0); 148 os.flush(); 149 150 readResponse(is); 151 152 os.write("E\n".getBytes()); 153 os.flush(); 154 } 155 156 private void sendFiles(Session sess, String [] files, String [] remoteFiles, String mode) throws IOException 157 { 158 byte[] buffer = new byte[8192]; 159 160 OutputStream os = new BufferedOutputStream (sess.getStdin(), 40000); 161 InputStream is = new BufferedInputStream (sess.getStdout(), 512); 162 163 readResponse(is); 164 165 for (int i = 0; i < files.length; i++) 166 { 167 File f = new File (files[i]); 168 long remain = f.length(); 169 170 String remoteName; 171 172 if ((remoteFiles != null) && (remoteFiles.length > i) && (remoteFiles[i] != null)) 173 remoteName = remoteFiles[i]; 174 else 175 remoteName = f.getName(); 176 177 String cline = "C" + mode + " " + remain + " " + remoteName + "\n"; 178 179 os.write(cline.getBytes()); 180 os.flush(); 181 182 readResponse(is); 183 184 FileInputStream fis = null; 185 186 try 187 { 188 fis = new FileInputStream (f); 189 190 while (remain > 0) 191 { 192 int trans; 193 if (remain > buffer.length) 194 trans = buffer.length; 195 else 196 trans = (int) remain; 197 198 if (fis.read(buffer, 0, trans) != trans) 199 throw new IOException ("Cannot read enough from local file " + files[i]); 200 201 os.write(buffer, 0, trans); 202 203 remain -= trans; 204 } 205 } 206 finally 207 { 208 if (fis != null) 209 fis.close(); 210 } 211 212 os.write(0); 213 os.flush(); 214 215 readResponse(is); 216 } 217 218 os.write("E\n".getBytes()); 219 os.flush(); 220 } 221 222 private void receiveFiles(Session sess, OutputStream [] targets) throws IOException 223 { 224 byte[] buffer = new byte[8192]; 225 226 OutputStream os = new BufferedOutputStream (sess.getStdin(), 512); 227 InputStream is = new BufferedInputStream (sess.getStdout(), 40000); 228 229 os.write(0x0); 230 os.flush(); 231 232 for (int i = 0; i < targets.length; i++) 233 { 234 LenNamePair lnp = null; 235 236 while (true) 237 { 238 int c = is.read(); 239 if (c < 0) 240 throw new IOException ("Remote scp terminated unexpectedly."); 241 242 String line = receiveLine(is); 243 244 if (c == 'T') 245 { 246 247 248 continue; 249 } 250 251 if ((c == 1) || (c == 2)) 252 throw new IOException ("Remote SCP error: " + line); 253 254 if (c == 'C') 255 { 256 lnp = parseCLine(line); 257 break; 258 259 } 260 throw new IOException ("Remote SCP error: " + ((char) c) + line); 261 } 262 263 os.write(0x0); 264 os.flush(); 265 266 long remain = lnp.length; 267 268 while (remain > 0) 269 { 270 int trans; 271 if (remain > buffer.length) 272 trans = buffer.length; 273 else 274 trans = (int) remain; 275 276 int this_time_received = is.read(buffer, 0, trans); 277 278 if (this_time_received < 0) 279 { 280 throw new IOException ("Remote scp terminated connection unexpectedly"); 281 } 282 283 targets[i].write(buffer, 0, this_time_received); 284 285 remain -= this_time_received; 286 } 287 288 readResponse(is); 289 290 os.write(0x0); 291 os.flush(); 292 } 293 } 294 295 private void receiveFiles(Session sess, String [] files, String target) throws IOException 296 { 297 byte[] buffer = new byte[8192]; 298 299 OutputStream os = new BufferedOutputStream (sess.getStdin(), 512); 300 InputStream is = new BufferedInputStream (sess.getStdout(), 40000); 301 302 os.write(0x0); 303 os.flush(); 304 305 for (int i = 0; i < files.length; i++) 306 { 307 LenNamePair lnp = null; 308 309 while (true) 310 { 311 int c = is.read(); 312 if (c < 0) 313 throw new IOException ("Remote scp terminated unexpectedly."); 314 315 String line = receiveLine(is); 316 317 if (c == 'T') 318 { 319 320 321 continue; 322 } 323 324 if ((c == 1) || (c == 2)) 325 throw new IOException ("Remote SCP error: " + line); 326 327 if (c == 'C') 328 { 329 lnp = parseCLine(line); 330 break; 331 332 } 333 throw new IOException ("Remote SCP error: " + ((char) c) + line); 334 } 335 336 os.write(0x0); 337 os.flush(); 338 339 File f = new File (target + File.separatorChar + lnp.filename); 340 FileOutputStream fop = null; 341 342 try 343 { 344 fop = new FileOutputStream (f); 345 346 long remain = lnp.length; 347 348 while (remain > 0) 349 { 350 int trans; 351 if (remain > buffer.length) 352 trans = buffer.length; 353 else 354 trans = (int) remain; 355 356 int this_time_received = is.read(buffer, 0, trans); 357 358 if (this_time_received < 0) 359 { 360 throw new IOException ("Remote scp terminated connection unexpectedly"); 361 } 362 363 fop.write(buffer, 0, this_time_received); 364 365 remain -= this_time_received; 366 } 367 } 368 finally 369 { 370 if (fop != null) 371 fop.close(); 372 } 373 374 readResponse(is); 375 376 os.write(0x0); 377 os.flush(); 378 } 379 } 380 381 392 public void put(String localFile, String remoteTargetDirectory) throws IOException 393 { 394 put(new String [] { localFile }, remoteTargetDirectory, "0600"); 395 } 396 397 408 409 public void put(String [] localFiles, String remoteTargetDirectory) throws IOException 410 { 411 put(localFiles, remoteTargetDirectory, "0600"); 412 } 413 414 426 public void put(String localFile, String remoteTargetDirectory, String mode) throws IOException 427 { 428 put(new String [] { localFile }, remoteTargetDirectory, mode); 429 } 430 431 445 public void put(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) 446 throws IOException 447 { 448 put(new String [] { localFile }, new String [] { remoteFileName }, remoteTargetDirectory, mode); 449 } 450 451 463 464 public void put(byte[] data, String remoteFileName, String remoteTargetDirectory) throws IOException 465 { 466 put(data, remoteFileName, remoteTargetDirectory, "0600"); 467 } 468 469 483 public void put(byte[] data, String remoteFileName, String remoteTargetDirectory, String mode) throws IOException 484 { 485 Session sess = null; 486 487 if ((remoteFileName == null) || (remoteTargetDirectory == null) || (mode == null)) 488 throw new IllegalArgumentException ("Null argument."); 489 490 if (mode.length() != 4) 491 throw new IllegalArgumentException ("Invalid mode."); 492 493 for (int i = 0; i < mode.length(); i++) 494 if (Character.isDigit(mode.charAt(i)) == false) 495 throw new IllegalArgumentException ("Invalid mode."); 496 497 remoteTargetDirectory = remoteTargetDirectory.trim(); 498 remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : "."; 499 500 String cmd = "scp -t -d " + remoteTargetDirectory; 501 502 try 503 { 504 sess = conn.openSession(); 505 sess.execCommand(cmd); 506 sendBytes(sess, data, remoteFileName, mode); 507 } 508 catch (IOException e) 509 { 510 throw (IOException ) new IOException ("Error during SCP transfer.").initCause(e); 511 } 512 finally 513 { 514 if (sess != null) 515 sess.close(); 516 } 517 } 518 519 531 public void put(String [] localFiles, String remoteTargetDirectory, String mode) throws IOException 532 { 533 put(localFiles, null, remoteTargetDirectory, mode); 534 } 535 536 public void put(String [] localFiles, String [] remoteFiles, String remoteTargetDirectory, String mode) 537 throws IOException 538 { 539 Session sess = null; 540 541 542 543 if ((localFiles == null) || (remoteTargetDirectory == null) || (mode == null)) 544 throw new IllegalArgumentException ("Null argument."); 545 546 if (mode.length() != 4) 547 throw new IllegalArgumentException ("Invalid mode."); 548 549 for (int i = 0; i < mode.length(); i++) 550 if (Character.isDigit(mode.charAt(i)) == false) 551 throw new IllegalArgumentException ("Invalid mode."); 552 553 if (localFiles.length == 0) 554 return; 555 556 remoteTargetDirectory = remoteTargetDirectory.trim(); 557 remoteTargetDirectory = (remoteTargetDirectory.length() > 0) ? remoteTargetDirectory : "."; 558 559 String cmd = "scp -t -d " + remoteTargetDirectory; 560 561 for (int i = 0; i < localFiles.length; i++) 562 { 563 if (localFiles[i] == null) 564 throw new IllegalArgumentException ("Cannot accept null filename."); 565 } 566 567 try 568 { 569 sess = conn.openSession(); 570 sess.execCommand(cmd); 571 sendFiles(sess, localFiles, remoteFiles, mode); 572 } 573 catch (IOException e) 574 { 575 throw (IOException ) new IOException ("Error during SCP transfer.").initCause(e); 576 } 577 finally 578 { 579 if (sess != null) 580 sess.close(); 581 } 582 } 583 584 594 public void get(String remoteFile, String localTargetDirectory) throws IOException 595 { 596 get(new String [] { remoteFile }, localTargetDirectory); 597 } 598 599 610 public void get(String remoteFile, OutputStream target) throws IOException 611 { 612 get(new String [] { remoteFile }, new OutputStream [] { target }); 613 } 614 615 private void get(String remoteFiles[], OutputStream [] targets) throws IOException 616 { 617 Session sess = null; 618 619 if ((remoteFiles == null) || (targets == null)) 620 throw new IllegalArgumentException ("Null argument."); 621 622 if (remoteFiles.length != targets.length) 623 throw new IllegalArgumentException ("Length of arguments does not match."); 624 625 if (remoteFiles.length == 0) 626 return; 627 628 String cmd = "scp -f"; 629 630 for (int i = 0; i < remoteFiles.length; i++) 631 { 632 if (remoteFiles[i] == null) 633 throw new IllegalArgumentException ("Cannot accept null filename."); 634 635 String tmp = remoteFiles[i].trim(); 636 637 if (tmp.length() == 0) 638 throw new IllegalArgumentException ("Cannot accept empty filename."); 639 640 cmd += (" " + tmp); 641 } 642 643 try 644 { 645 sess = conn.openSession(); 646 sess.execCommand(cmd); 647 receiveFiles(sess, targets); 648 } 649 catch (IOException e) 650 { 651 throw (IOException ) new IOException ("Error during SCP transfer.").initCause(e); 652 } 653 finally 654 { 655 if (sess != null) 656 sess.close(); 657 } 658 } 659 660 670 public void get(String remoteFiles[], String localTargetDirectory) throws IOException 671 { 672 Session sess = null; 673 674 if ((remoteFiles == null) || (localTargetDirectory == null)) 675 throw new IllegalArgumentException ("Null argument."); 676 677 if (remoteFiles.length == 0) 678 return; 679 680 String cmd = "scp -f"; 681 682 for (int i = 0; i < remoteFiles.length; i++) 683 { 684 if (remoteFiles[i] == null) 685 throw new IllegalArgumentException ("Cannot accept null filename."); 686 687 String tmp = remoteFiles[i].trim(); 688 689 if (tmp.length() == 0) 690 throw new IllegalArgumentException ("Cannot accept empty filename."); 691 692 cmd += (" " + tmp); 693 } 694 695 try 696 { 697 sess = conn.openSession(); 698 sess.execCommand(cmd); 699 receiveFiles(sess, remoteFiles, localTargetDirectory); 700 } 701 catch (IOException e) 702 { 703 throw (IOException ) new IOException ("Error during SCP transfer.").initCause(e); 704 } 705 finally 706 { 707 if (sess != null) 708 sess.close(); 709 } 710 } 711 } 712 | Popular Tags |