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