KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ScpToNoneCipher


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
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 JavaDoc[] 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 JavaDoc lfile=arg[0];
18       String JavaDoc user=arg[1].substring(0, arg[1].indexOf('@'));
19       arg[1]=arg[1].substring(arg[1].indexOf('@')+1);
20       String JavaDoc host=arg[1].substring(0, arg[1].indexOf(':'));
21       String JavaDoc rfile=arg[1].substring(arg[1].indexOf(':')+1);
22
23       JSch jsch=new JSch();
24       Session session=jsch.getSession(user, host, 22);
25
26       // username and password will be given via UserInfo interface.
27
UserInfo ui=new MyUserInfo();
28       session.setUserInfo(ui);
29       session.connect();
30
31       java.util.Hashtable JavaDoc config=new java.util.Hashtable JavaDoc();
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       // exec 'scp -t rfile' remotely
38
String JavaDoc command="scp -p -t "+rfile;
39       Channel channel=session.openChannel("exec");
40       ((ChannelExec)channel).setCommand(command);
41
42       // get I/O streams for remote scp
43
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       // send "C0644 filesize filename", where filename should not include '/'
53
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       // send a content of lfile
69
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       // send '\0'
80
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 JavaDoc e){
91       System.out.println(e);
92       try{if(fis!=null)fis.close();}catch(Exception JavaDoc ee){}
93     }
94   }
95
96   static int checkAck(InputStream in) throws IOException{
97     int b=in.read();
98     // b may be 0 for success,
99
// 1 for error,
100
// 2 for fatal error,
101
// -1
102
if(b==0) return b;
103     if(b==-1) return b;
104
105     if(b==1 || b==2){
106       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
107       int c;
108       do {
109     c=in.read();
110     sb.append((char)c);
111       }
112       while(c!='\n');
113       if(b==1){ // error
114
System.out.print(sb.toString());
115       }
116       if(b==2){ // fatal error
117
System.out.print(sb.toString());
118       }
119     }
120     return b;
121   }
122
123   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
124     public String JavaDoc getPassword(){ return passwd; }
125     public boolean promptYesNo(String JavaDoc str){
126       Object JavaDoc[] 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 JavaDoc passwd;
137     JTextField passwordField=(JTextField)new JPasswordField(20);
138
139     public String JavaDoc getPassphrase(){ return null; }
140     public boolean promptPassphrase(String JavaDoc message){ return true; }
141     public boolean promptPassword(String JavaDoc message){
142       Object JavaDoc[] 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 JavaDoc 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 JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
162                                               String JavaDoc name,
163                                               String JavaDoc instruction,
164                                               String JavaDoc[] 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 JavaDoc[] response=new String JavaDoc[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; // cancel
210
}
211     }
212   }
213 }
214
Popular Tags