KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ScpFrom


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 ScpFrom{
8   public static void main(String JavaDoc[] 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 JavaDoc user=arg[0].substring(0, arg[0].indexOf('@'));
18       arg[0]=arg[0].substring(arg[0].indexOf('@')+1);
19       String JavaDoc host=arg[0].substring(0, arg[0].indexOf(':'));
20       String JavaDoc rfile=arg[0].substring(arg[0].indexOf(':')+1);
21       String JavaDoc lfile=arg[1];
22
23       String JavaDoc 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       // username and password will be given via UserInfo interface.
32
UserInfo ui=new MyUserInfo();
33       session.setUserInfo(ui);
34       session.connect();
35
36       // exec 'scp -f rfile' remotely
37
String JavaDoc command="scp -f "+rfile;
38       Channel channel=session.openChannel("exec");
39       ((ChannelExec)channel).setCommand(command);
40
41       // get I/O streams for remote scp
42
OutputStream out=channel.getOutputStream();
43       InputStream in=channel.getInputStream();
44
45       channel.connect();
46
47       byte[] buf=new byte[1024];
48
49       // send '\0'
50
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         // read '0644 '
59
in.read(buf, 0, 5);
60
61         long filesize=0L;
62         while(true){
63           if(in.read(buf, 0, 1)<0){
64             // error
65
break;
66           }
67           if(buf[0]==' ')break;
68           filesize=filesize*10L+(long)(buf[0]-'0');
69         }
70
71         String JavaDoc file=null;
72         for(int i=0;;i++){
73           in.read(buf, i, 1);
74           if(buf[i]==(byte)0x0a){
75             file=new String JavaDoc(buf, 0, i);
76             break;
77       }
78         }
79
80     //System.out.println("filesize="+filesize+", file="+file);
81

82         // send '\0'
83
buf[0]=0; out.write(buf, 0, 1); out.flush();
84
85         // read a content of lfile
86
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             // error
94
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         // send '\0'
108
buf[0]=0; out.write(buf, 0, 1); out.flush();
109       }
110
111       session.disconnect();
112
113       System.exit(0);
114     }
115     catch(Exception JavaDoc e){
116       System.out.println(e);
117       try{if(fos!=null)fos.close();}catch(Exception JavaDoc ee){}
118     }
119   }
120
121   static int checkAck(InputStream in) throws IOException{
122     int b=in.read();
123     // b may be 0 for success,
124
// 1 for error,
125
// 2 for fatal error,
126
// -1
127
if(b==0) return b;
128     if(b==-1) return b;
129
130     if(b==1 || b==2){
131       StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
132       int c;
133       do {
134     c=in.read();
135     sb.append((char)c);
136       }
137       while(c!='\n');
138       if(b==1){ // error
139
System.out.print(sb.toString());
140       }
141       if(b==2){ // fatal error
142
System.out.print(sb.toString());
143       }
144     }
145     return b;
146   }
147
148   public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
149     public String JavaDoc getPassword(){ return passwd; }
150     public boolean promptYesNo(String JavaDoc str){
151       Object JavaDoc[] 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 JavaDoc passwd;
162     JTextField passwordField=(JTextField)new JPasswordField(20);
163
164     public String JavaDoc getPassphrase(){ return null; }
165     public boolean promptPassphrase(String JavaDoc message){ return true; }
166     public boolean promptPassword(String JavaDoc message){
167       Object JavaDoc[] 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 JavaDoc 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 JavaDoc[] promptKeyboardInteractive(String JavaDoc destination,
187                                               String JavaDoc name,
188                                               String JavaDoc instruction,
189                                               String JavaDoc[] 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 JavaDoc[] response=new String JavaDoc[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; // cancel
235
}
236     }
237   }
238 }
239
Popular Tags