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