1 2 import com.jcraft.jsch.*; 3 import java.awt.*; 4 import javax.swing.*; 5 import java.io.*; 6 7 public class ScpFrom{ 8 public static void main(String [] arg){ 9 if(arg.length!=2){ 10 System.err.println("usage: java ScpFrom user@remotehost:file1 file2"); 11 System.exit(-1); 12 } 13 14 FileOutputStream fos=null; 15 try{ 16 17 String user=arg[0].substring(0, arg[0].indexOf('@')); 18 arg[0]=arg[0].substring(arg[0].indexOf('@')+1); 19 String host=arg[0].substring(0, arg[0].indexOf(':')); 20 String rfile=arg[0].substring(arg[0].indexOf(':')+1); 21 String lfile=arg[1]; 22 23 String prefix=null; 24 if(new File(lfile).isDirectory()){ 25 prefix=lfile+File.separator; 26 } 27 28 JSch jsch=new JSch(); 29 Session session=jsch.getSession(user, host, 22); 30 31 UserInfo ui=new MyUserInfo(); 33 session.setUserInfo(ui); 34 session.connect(); 35 36 String command="scp -f "+rfile; 38 Channel channel=session.openChannel("exec"); 39 ((ChannelExec)channel).setCommand(command); 40 41 OutputStream out=channel.getOutputStream(); 43 InputStream in=channel.getInputStream(); 44 45 channel.connect(); 46 47 byte[] buf=new byte[1024]; 48 49 buf[0]=0; out.write(buf, 0, 1); out.flush(); 51 52 while(true){ 53 int c=checkAck(in); 54 if(c!='C'){ 55 break; 56 } 57 58 in.read(buf, 0, 5); 60 61 long filesize=0L; 62 while(true){ 63 if(in.read(buf, 0, 1)<0){ 64 break; 66 } 67 if(buf[0]==' ')break; 68 filesize=filesize*10L+(long)(buf[0]-'0'); 69 } 70 71 String file=null; 72 for(int i=0;;i++){ 73 in.read(buf, i, 1); 74 if(buf[i]==(byte)0x0a){ 75 file=new String (buf, 0, i); 76 break; 77 } 78 } 79 80 82 buf[0]=0; out.write(buf, 0, 1); out.flush(); 84 85 fos=new FileOutputStream(prefix==null ? lfile : prefix+file); 87 int foo; 88 while(true){ 89 if(buf.length<filesize) foo=buf.length; 90 else foo=(int)filesize; 91 foo=in.read(buf, 0, foo); 92 if(foo<0){ 93 break; 95 } 96 fos.write(buf, 0, foo); 97 filesize-=foo; 98 if(filesize==0L) break; 99 } 100 fos.close(); 101 fos=null; 102 103 if(checkAck(in)!=0){ 104 System.exit(0); 105 } 106 107 buf[0]=0; out.write(buf, 0, 1); out.flush(); 109 } 110 111 session.disconnect(); 112 113 System.exit(0); 114 } 115 catch(Exception e){ 116 System.out.println(e); 117 try{if(fos!=null)fos.close();}catch(Exception ee){} 118 } 119 } 120 121 static int checkAck(InputStream in) throws IOException{ 122 int b=in.read(); 123 if(b==0) return b; 128 if(b==-1) return b; 129 130 if(b==1 || b==2){ 131 StringBuffer sb=new StringBuffer (); 132 int c; 133 do { 134 c=in.read(); 135 sb.append((char)c); 136 } 137 while(c!='\n'); 138 if(b==1){ System.out.print(sb.toString()); 140 } 141 if(b==2){ System.out.print(sb.toString()); 143 } 144 } 145 return b; 146 } 147 148 public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ 149 public String getPassword(){ return passwd; } 150 public boolean promptYesNo(String str){ 151 Object [] options={ "yes", "no" }; 152 int foo=JOptionPane.showOptionDialog(null, 153 str, 154 "Warning", 155 JOptionPane.DEFAULT_OPTION, 156 JOptionPane.WARNING_MESSAGE, 157 null, options, options[0]); 158 return foo==0; 159 } 160 161 String passwd; 162 JTextField passwordField=(JTextField)new JPasswordField(20); 163 164 public String getPassphrase(){ return null; } 165 public boolean promptPassphrase(String message){ return true; } 166 public boolean promptPassword(String message){ 167 Object [] ob={passwordField}; 168 int result= 169 JOptionPane.showConfirmDialog(null, ob, message, 170 JOptionPane.OK_CANCEL_OPTION); 171 if(result==JOptionPane.OK_OPTION){ 172 passwd=passwordField.getText(); 173 return true; 174 } 175 else{ return false; } 176 } 177 public void showMessage(String message){ 178 JOptionPane.showMessageDialog(null, message); 179 } 180 final GridBagConstraints gbc = 181 new GridBagConstraints(0,0,1,1,1,1, 182 GridBagConstraints.NORTHWEST, 183 GridBagConstraints.NONE, 184 new Insets(0,0,0,0),0,0); 185 private Container panel; 186 public String [] promptKeyboardInteractive(String destination, 187 String name, 188 String instruction, 189 String [] prompt, 190 boolean[] echo){ 191 panel = new JPanel(); 192 panel.setLayout(new GridBagLayout()); 193 194 gbc.weightx = 1.0; 195 gbc.gridwidth = GridBagConstraints.REMAINDER; 196 gbc.gridx = 0; 197 panel.add(new JLabel(instruction), gbc); 198 gbc.gridy++; 199 200 gbc.gridwidth = GridBagConstraints.RELATIVE; 201 202 JTextField[] texts=new JTextField[prompt.length]; 203 for(int i=0; i<prompt.length; i++){ 204 gbc.fill = GridBagConstraints.NONE; 205 gbc.gridx = 0; 206 gbc.weightx = 1; 207 panel.add(new JLabel(prompt[i]),gbc); 208 209 gbc.gridx = 1; 210 gbc.fill = GridBagConstraints.HORIZONTAL; 211 gbc.weighty = 1; 212 if(echo[i]){ 213 texts[i]=new JTextField(20); 214 } 215 else{ 216 texts[i]=new JPasswordField(20); 217 } 218 panel.add(texts[i], gbc); 219 gbc.gridy++; 220 } 221 222 if(JOptionPane.showConfirmDialog(null, panel, 223 destination+": "+name, 224 JOptionPane.OK_CANCEL_OPTION, 225 JOptionPane.QUESTION_MESSAGE) 226 ==JOptionPane.OK_OPTION){ 227 String [] response=new String [prompt.length]; 228 for(int i=0; i<prompt.length; i++){ 229 response[i]=texts[i].getText(); 230 } 231 return response; 232 } 233 else{ 234 return null; } 236 } 237 } 238 } 239 | Popular Tags |