1
package org.eclipse.linuxtools.systemtap.ui.consolelog;
3
import com.jcraft.jsch.*;
7
import org.eclipse.linuxtools.systemtap.ui.consolelog.dialogs.ErrorMessage;
8
import org.eclipse.linuxtools.systemtap.ui.consolelog.internal.ConsoleLogPlugin;
9
import org.eclipse.linuxtools.systemtap.ui.consolelog.preferences.ConsoleLogPreferenceConstants;
14
@SuppressWarnings("deprecation")
15
public class ScpClient {
17
private Session session;
19
public ScpClient() throws Exception {
20
//public static void main(String[] arg){
23
String user=ConsoleLogPlugin.getDefault().getPluginPreferences().getString(ConsoleLogPreferenceConstants.SCP_USER);
24
String host=ConsoleLogPlugin.getDefault().getPluginPreferences().getString(ConsoleLogPreferenceConstants.HOST_NAME);
27
//System.out.println(lfile + " " + rfile);
32
session=jsch.getSession(user, host, 22);
34
// username and password will be given via UserInfo interface.
35
//UserInfo ui=new MyUserInfo();
36
//session.setUserInfo(ui);
37
session.setPassword(ConsoleLogPlugin.getDefault().getPluginPreferences().getString(ConsoleLogPreferenceConstants.SCP_PASSWORD));
38
java.util.Properties config = new java.util.Properties();
39
config.put("StrictHostKeyChecking", "no");
40
session.setConfig(config);
44
e.printStackTrace(System.err);
45
new ErrorMessage("Error in connection", "File Transfer failed.\n See stderr for more details").open();
50
public void transfer(String fromFile, String toFile) throws Exception {
51
// exec 'scp -t rfile' remotely
52
FileInputStream fis=null;
54
String lfile=fromFile;
55
String command="scp -t "+rfile;
57
Channel channel=session.openChannel("exec");
58
((ChannelExec)channel).setCommand(command);
60
// get I/O streams for remote scp
61
OutputStream out=channel.getOutputStream();
62
InputStream in=channel.getInputStream();
68
System.out.println("err");
71
// send "C0644 filesize filename", where filename should not include '/'
72
long filesize=(new File(lfile)).length();
73
command="C0644 "+filesize+" ";
74
if(lfile.lastIndexOf('/')>0){
75
command+=lfile.substring(lfile.lastIndexOf('/')+1);
82
out.write(command.getBytes()); out.flush();
84
System.out.println("err");
89
// send a content of lfile
90
fis=new FileInputStream(lfile);
91
byte[] buf=new byte[1024];
93
int len=fis.read(buf, 0, buf.length);
95
out.write(buf, 0, len); //out.flush();
101
buf[0]=0; out.write(buf, 0, 1); out.flush();
103
System.out.println("err");
107
channel.disconnect();
108
session.disconnect();
112
try{if(fis!=null)fis.close();}catch(Exception ee){}
117
static int checkAck(InputStream in) throws IOException{
119
// b may be 0 for success,
121
// 2 for fatal error,
127
StringBuffer sb=new StringBuffer();
135
//System.out.print(sb.toString());
137
if(b==2){ // fatal error
138
//System.out.print(sb.toString());
144
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
145
public String getPassword(){ return passwd; }
146
public boolean promptYesNo(String str){
147
Object[] options={ "yes", "no" };
148
int foo=JOptionPane.showOptionDialog(null,
151
JOptionPane.DEFAULT_OPTION,
152
JOptionPane.WARNING_MESSAGE,
153
null, options, options[0]);
158
JTextField passwordField=(JTextField)new JPasswordField(20);
160
public String getPassphrase(){ return null; }
161
public boolean promptPassphrase(String message){ return true; }
162
public boolean promptPassword(String message){
163
Object[] ob={passwordField};
165
JOptionPane.showConfirmDialog(null, ob, message,
166
JOptionPane.OK_CANCEL_OPTION);
167
if(result==JOptionPane.OK_OPTION){
168
passwd=passwordField.getText();
171
else{ return false; }
173
public void showMessage(String message){
174
JOptionPane.showMessageDialog(null, message);
176
final GridBagConstraints gbc =
177
new GridBagConstraints(0,0,1,1,1,1,
178
GridBagConstraints.NORTHWEST,
179
GridBagConstraints.NONE,
180
new Insets(0,0,0,0),0,0);
181
private Container panel;
182
public String[] promptKeyboardInteractive(String destination,
187
panel = new JPanel();
188
panel.setLayout(new GridBagLayout());
191
gbc.gridwidth = GridBagConstraints.REMAINDER;
193
panel.add(new JLabel(instruction), gbc);
196
gbc.gridwidth = GridBagConstraints.RELATIVE;
198
JTextField[] texts=new JTextField[prompt.length];
199
for(int i=0; i<prompt.length; i++){
200
gbc.fill = GridBagConstraints.NONE;
203
panel.add(new JLabel(prompt[i]),gbc);
206
gbc.fill = GridBagConstraints.HORIZONTAL;
209
texts[i]=new JTextField(20);
212
texts[i]=new JPasswordField(20);
214
panel.add(texts[i], gbc);
218
if(JOptionPane.showConfirmDialog(null, panel,
219
destination+": "+name,
220
JOptionPane.OK_CANCEL_OPTION,
221
JOptionPane.QUESTION_MESSAGE)
222
==JOptionPane.OK_OPTION){
223
String[] response=new String[prompt.length];
224
for(int i=0; i<prompt.length; i++){
225
response[i]=texts[i].getText();
230
return null; // cancel
b'\\ No newline at end of file'