1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 6 public class Sftp{ 7 public static void main(String [] arg){ 8 9 try{ 10 JSch jsch=new JSch(); 11 12 String host=JOptionPane.showInputDialog("Enter username@hostname", 13 System.getProperty("user.name")+ 14 "@localhost"); 15 String user=host.substring(0, host.indexOf('@')); 16 host=host.substring(host.indexOf('@')+1); 17 18 Session session=jsch.getSession(user, host, 22); 19 20 UserInfo ui=new MyUserInfo(); 22 session.setUserInfo(ui); 23 24 session.connect(); 25 26 Channel channel=session.openChannel("sftp"); 27 channel.connect(); 28 ChannelSftp c=(ChannelSftp)channel; 29 30 java.io.InputStream in=System.in; 31 java.io.PrintStream out=System.out; 32 33 java.util.Vector cmds=new java.util.Vector (); 34 byte[] buf=new byte[1024]; 35 int i; 36 String str; 37 int level=0; 38 39 while(true){ 40 out.print("sftp> "); 41 cmds.removeAllElements(); 42 i=in.read(buf, 0, 1024); 43 if(i<=0)break; 44 45 i--; 46 if(i>0 && buf[i-1]==0x0d)i--; 47 int s=0; 50 for(int ii=0; ii<i; ii++){ 51 if(buf[ii]==' '){ 52 if(ii-s>0){ cmds.addElement(new String (buf, s, ii-s)); } 53 while(ii<i){if(buf[ii]!=' ')break; ii++;} 54 s=ii; 55 } 56 } 57 if(s<i){ cmds.addElement(new String (buf, s, i-s)); } 58 if(cmds.size()==0)continue; 59 60 String cmd=(String )cmds.elementAt(0); 61 if(cmd.equals("quit")){ 62 c.quit(); 63 break; 64 } 65 if(cmd.equals("exit")){ 66 c.exit(); 67 break; 68 } 69 if(cmd.equals("rekey")){ 70 session.rekey(); 71 continue; 72 } 73 if(cmd.equals("compression")){ 74 if(cmds.size()<2){ 75 out.println("compression level: "+level); 76 continue; 77 } 78 try{ 79 level=Integer.parseInt((String )cmds.elementAt(1)); 80 java.util.Hashtable config=new java.util.Hashtable (); 81 if(level==0){ 82 config.put("compression.s2c", "none"); 83 config.put("compression.c2s", "none"); 84 } 85 else{ 86 config.put("compression.s2c", "zlib@openssh.com,zlib,none"); 87 config.put("compression.c2s", "zlib@openssh.com,zlib,none"); 88 } 89 session.setConfig(config); 90 } 91 catch(Exception e){} 92 session.rekey(); 93 continue; 94 } 95 if(cmd.equals("cd") || cmd.equals("lcd")){ 96 if(cmds.size()<2) continue; 97 String path=(String )cmds.elementAt(1); 98 try{ 99 if(cmd.equals("cd")) c.cd(path); 100 else c.lcd(path); 101 } 102 catch(SftpException e){ 103 System.out.println(e.toString()); 104 } 105 continue; 106 } 107 if(cmd.equals("rm") || cmd.equals("rmdir") || cmd.equals("mkdir")){ 108 if(cmds.size()<2) continue; 109 String path=(String )cmds.elementAt(1); 110 try{ 111 if(cmd.equals("rm")) c.rm(path); 112 else if(cmd.equals("rmdir")) c.rmdir(path); 113 else c.mkdir(path); 114 } 115 catch(SftpException e){ 116 System.out.println(e.toString()); 117 } 118 continue; 119 } 120 if(cmd.equals("chgrp") || cmd.equals("chown") || cmd.equals("chmod")){ 121 if(cmds.size()!=3) continue; 122 String path=(String )cmds.elementAt(2); 123 int foo=0; 124 if(cmd.equals("chmod")){ 125 byte[] bar=((String )cmds.elementAt(1)).getBytes(); 126 int k; 127 for(int j=0; j<bar.length; j++){ 128 k=bar[j]; 129 if(k<'0'||k>'7'){foo=-1; break;} 130 foo<<=3; 131 foo|=(k-'0'); 132 } 133 if(foo==-1)continue; 134 } 135 else{ 136 try{foo=Integer.parseInt((String )cmds.elementAt(1));} 137 catch(Exception e){continue;} 138 } 139 try{ 140 if(cmd.equals("chgrp")){ c.chgrp(foo, path); } 141 else if(cmd.equals("chown")){ c.chown(foo, path); } 142 else if(cmd.equals("chmod")){ c.chmod(foo, path); } 143 } 144 catch(SftpException e){ 145 System.out.println(e.toString()); 146 } 147 continue; 148 } 149 if(cmd.equals("pwd") || cmd.equals("lpwd")){ 150 str=(cmd.equals("pwd")?"Remote":"Local"); 151 str+=" working directory: "; 152 if(cmd.equals("pwd")) str+=c.pwd(); 153 else str+=c.lpwd(); 154 out.println(str); 155 continue; 156 } 157 if(cmd.equals("ls") || cmd.equals("dir")){ 158 String path="."; 159 if(cmds.size()==2) path=(String )cmds.elementAt(1); 160 try{ 161 java.util.Vector vv=c.ls(path); 162 if(vv!=null){ 163 for(int ii=0; ii<vv.size(); ii++){ 164 166 Object obj=vv.elementAt(ii); 167 if(obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry){ 168 out.println(((com.jcraft.jsch.ChannelSftp.LsEntry)obj).getLongname()); 169 } 170 171 } 172 } 173 } 174 catch(SftpException e){ 175 System.out.println(e.toString()); 176 } 177 continue; 178 } 179 if(cmd.equals("lls") || cmd.equals("ldir")){ 180 String path="."; 181 if(cmds.size()==2) path=(String )cmds.elementAt(1); 182 try{ 183 java.io.File file=new java.io.File (path); 184 if(!file.exists()){ 185 out.println(path+": No such file or directory"); 186 continue; 187 } 188 if(file.isDirectory()){ 189 String [] list=file.list(); 190 for(int ii=0; ii<list.length; ii++){ 191 out.println(list[ii]); 192 } 193 continue; 194 } 195 out.println(path); 196 } 197 catch(Exception e){ 198 System.out.println(e); 199 } 200 continue; 201 } 202 if(cmd.equals("get") || 203 cmd.equals("get-resume") || cmd.equals("get-append") || 204 cmd.equals("put") || 205 cmd.equals("put-resume") || cmd.equals("put-append") 206 ){ 207 if(cmds.size()!=2 && cmds.size()!=3) continue; 208 String p1=(String )cmds.elementAt(1); 209 String p2="."; 211 if(cmds.size()==3)p2=(String )cmds.elementAt(2); 212 try{ 213 SftpProgressMonitor monitor=new MyProgressMonitor(); 214 if(cmd.startsWith("get")){ 215 int mode=ChannelSftp.OVERWRITE; 216 if(cmd.equals("get-resume")){ mode=ChannelSftp.RESUME; } 217 else if(cmd.equals("get-append")){ mode=ChannelSftp.APPEND; } 218 c.get(p1, p2, monitor, mode); 219 } 220 else{ 221 int mode=ChannelSftp.OVERWRITE; 222 if(cmd.equals("put-resume")){ mode=ChannelSftp.RESUME; } 223 else if(cmd.equals("put-append")){ mode=ChannelSftp.APPEND; } 224 c.put(p1, p2, monitor, mode); 225 } 226 } 227 catch(SftpException e){ 228 System.out.println(e.toString()); 229 } 230 continue; 231 } 232 if(cmd.equals("ln") || cmd.equals("symlink") || cmd.equals("rename")){ 233 if(cmds.size()!=3) continue; 234 String p1=(String )cmds.elementAt(1); 235 String p2=(String )cmds.elementAt(2); 236 try{ 237 if(cmd.equals("rename")) c.rename(p1, p2); 238 else c.symlink(p1, p2); 239 } 240 catch(SftpException e){ 241 System.out.println(e.toString()); 242 } 243 continue; 244 } 245 if(cmd.equals("stat") || cmd.equals("lstat")){ 246 if(cmds.size()!=2) continue; 247 String p1=(String )cmds.elementAt(1); 248 SftpATTRS attrs=null; 249 try{ 250 if(cmd.equals("stat")) attrs=c.stat(p1); 251 else attrs=c.lstat(p1); 252 } 253 catch(SftpException e){ 254 System.out.println(e.toString()); 255 } 256 if(attrs!=null){ 257 out.println(attrs); 258 } 259 else{ 260 } 261 continue; 262 } 263 if(cmd.equals("readlink")){ 264 if(cmds.size()!=2) continue; 265 String p1=(String )cmds.elementAt(1); 266 String filename=null; 267 try{ 268 filename=c.readlink(p1); 269 out.println(filename); 270 } 271 catch(SftpException e){ 272 System.out.println(e.toString()); 273 } 274 continue; 275 } 276 if(cmd.equals("version")){ 277 out.println("SFTP protocol version "+c.version()); 278 continue; 279 } 280 if(cmd.equals("help") || cmd.equals("?")){ 281 out.println(help); 282 continue; 283 } 284 out.println("unimplemented command: "+cmd); 285 } 286 session.disconnect(); 287 } 288 catch(Exception e){ 289 System.out.println(e); 290 } 291 System.exit(0); 292 } 293 294 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 295 public String getPassword(){ return passwd; } 296 public boolean promptYesNo(String str){ 297 Object [] options={ "yes", "no" }; 298 int foo=JOptionPane.showOptionDialog(null, 299 str, 300 "Warning", 301 JOptionPane.DEFAULT_OPTION, 302 JOptionPane.WARNING_MESSAGE, 303 null, options, options[0]); 304 return foo==0; 305 } 306 307 String passwd; 308 JTextField passwordField=(JTextField)new JPasswordField(20); 309 310 public String getPassphrase(){ return null; } 311 public boolean promptPassphrase(String message){ return true; } 312 public boolean promptPassword(String message){ 313 Object [] ob={passwordField}; 314 int result= 315 JOptionPane.showConfirmDialog(null, ob, message, 316 JOptionPane.OK_CANCEL_OPTION); 317 if(result==JOptionPane.OK_OPTION){ 318 passwd=passwordField.getText(); 319 return true; 320 } 321 else{ return false; } 322 } 323 public void showMessage(String message){ 324 JOptionPane.showMessageDialog(null, message); 325 } 326 final GridBagConstraints gbc = 327 new GridBagConstraints(0,0,1,1,1,1, 328 GridBagConstraints.NORTHWEST, 329 GridBagConstraints.NONE, 330 new Insets(0,0,0,0),0,0); 331 private Container panel; 332 public String [] promptKeyboardInteractive(String destination, 333 String name, 334 String instruction, 335 String [] prompt, 336 boolean[] echo){ 337 panel = new JPanel(); 338 panel.setLayout(new GridBagLayout()); 339 340 gbc.weightx = 1.0; 341 gbc.gridwidth = GridBagConstraints.REMAINDER; 342 gbc.gridx = 0; 343 panel.add(new JLabel(instruction), gbc); 344 gbc.gridy++; 345 346 gbc.gridwidth = GridBagConstraints.RELATIVE; 347 348 JTextField[] texts=new JTextField[prompt.length]; 349 for(int i=0; i<prompt.length; i++){ 350 gbc.fill = GridBagConstraints.NONE; 351 gbc.gridx = 0; 352 gbc.weightx = 1; 353 panel.add(new JLabel(prompt[i]),gbc); 354 355 gbc.gridx = 1; 356 gbc.fill = GridBagConstraints.HORIZONTAL; 357 gbc.weighty = 1; 358 if(echo[i]){ 359 texts[i]=new JTextField(20); 360 } 361 else{ 362 texts[i]=new JPasswordField(20); 363 } 364 panel.add(texts[i], gbc); 365 gbc.gridy++; 366 } 367 368 if(JOptionPane.showConfirmDialog(null, panel, 369 destination+": "+name, 370 JOptionPane.OK_CANCEL_OPTION, 371 JOptionPane.QUESTION_MESSAGE) 372 ==JOptionPane.OK_OPTION){ 373 String [] response=new String [prompt.length]; 374 for(int i=0; i<prompt.length; i++){ 375 response[i]=texts[i].getText(); 376 } 377 return response; 378 } 379 else{ 380 return null; } 382 } 383 } 384 385 425 426 public static class MyProgressMonitor implements SftpProgressMonitor{ 427 ProgressMonitor monitor; 428 long count=0; 429 long max=0; 430 public void init(int op, String src, String dest, long max){ 431 this.max=max; 432 monitor=new ProgressMonitor(null, 433 ((op==SftpProgressMonitor.PUT)? 434 "put" : "get")+": "+src, 435 "", 0, (int)max); 436 count=0; 437 percent=-1; 438 monitor.setProgress((int)this.count); 439 monitor.setMillisToDecideToPopup(1000); 440 } 441 private long percent=-1; 442 public boolean count(long count){ 443 this.count+=count; 444 445 if(percent>=this.count*100/max){ return true; } 446 percent=this.count*100/max; 447 448 monitor.setNote("Completed "+this.count+"("+percent+"%) out of "+max+"."); 449 monitor.setProgress((int)this.count); 450 451 return !(monitor.isCanceled()); 452 } 453 public void end(){ 454 monitor.close(); 455 } 456 } 457 458 private static String help = 459 " Available commands:\n"+ 460 " * means unimplemented command.\n"+ 461 "cd path Change remote directory to 'path'\n"+ 462 "lcd path Change local directory to 'path'\n"+ 463 "chgrp grp path Change group of file 'path' to 'grp'\n"+ 464 "chmod mode path Change permissions of file 'path' to 'mode'\n"+ 465 "chown own path Change owner of file 'path' to 'own'\n"+ 466 "help Display this help text\n"+ 467 "get remote-path [local-path] Download file\n"+ 468 "get-resume remote-path [local-path] Resume to download file.\n"+ 469 "get-append remote-path [local-path] Append remote file to local file\n"+ 470 "*lls [ls-options [path]] Display local directory listing\n"+ 471 "ln oldpath newpath Symlink remote file\n"+ 472 "*lmkdir path Create local directory\n"+ 473 "lpwd Print local working directory\n"+ 474 "ls [path] Display remote directory listing\n"+ 475 "*lumask umask Set local umask to 'umask'\n"+ 476 "mkdir path Create remote directory\n"+ 477 "put local-path [remote-path] Upload file\n"+ 478 "put-resume local-path [remote-path] Resume to upload file\n"+ 479 "put-append local-path [remote-path] Append local file to remote file.\n"+ 480 "pwd Display remote working directory\n"+ 481 "stat path Display info about path\n"+ 482 "exit Quit sftp\n"+ 483 "quit Quit sftp\n"+ 484 "rename oldpath newpath Rename remote file\n"+ 485 "rmdir path Remove remote directory\n"+ 486 "rm path Delete remote file\n"+ 487 "symlink oldpath newpath Symlink remote file\n"+ 488 "readlink path Check the target of a symbolic link\n"+ 489 "rekey Key re-exchanging\n"+ 490 "compression level Packet compression will be enabled\n"+ 491 "version Show SFTP version\n"+ 492 "? Synonym for help"; 493 } 494 | Popular Tags |