~ubuntu-branches/ubuntu/trusty/jsch/trusty-proposed

« back to all changes in this revision

Viewing changes to examples/KnownHosts.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2008-03-21 11:41:01 UTC
  • mfrom: (3.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080321114101-2g9w5l8ynwf6pbsq
Tags: 0.1.37-3
Add OSGi manifest patch. Thanks to Roberto Jimenoca. Closes: #471209.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2
2
import com.jcraft.jsch.*;
3
3
import java.awt.*;
4
 
import java.awt.event.*;
5
4
import javax.swing.*;
6
5
 
7
 
import java.io.*;
8
 
 
9
6
public class KnownHosts{
10
7
  public static void main(String[] arg){
11
8
 
35
32
        System.out.println("");
36
33
      }
37
34
 
38
 
      String host=JOptionPane.showInputDialog("Enter username@hostname",
39
 
                                              System.getProperty("user.name")+
40
 
                                              "@localhost"); 
 
35
      String host=null;
 
36
      if(arg.length>0){
 
37
        host=arg[0];
 
38
      }
 
39
      else{
 
40
        host=JOptionPane.showInputDialog("Enter username@hostname",
 
41
                                         System.getProperty("user.name")+
 
42
                                         "@localhost"); 
 
43
      }
41
44
      String user=host.substring(0, host.indexOf('@'));
42
45
      host=host.substring(host.indexOf('@')+1);
43
46
 
47
50
      UserInfo ui=new MyUserInfo();
48
51
      session.setUserInfo(ui);
49
52
 
 
53
      /*
 
54
      // In adding to known_hosts file, host names will be hashed. 
 
55
      session.setConfig("HashKnownHosts",  "yes");
 
56
      */
 
57
 
50
58
      session.connect();
51
59
 
52
60
      {
69
77
    }
70
78
  }
71
79
 
72
 
  public static class MyUserInfo implements UserInfo{
 
80
  public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
73
81
    public String getPassword(){ return passwd; }
74
82
    public boolean promptYesNo(String str){
75
83
      Object[] options={ "yes", "no" };
101
109
    public void showMessage(String message){
102
110
      JOptionPane.showMessageDialog(null, message);
103
111
    }
 
112
    final GridBagConstraints gbc = 
 
113
      new GridBagConstraints(0,0,1,1,1,1,
 
114
                             GridBagConstraints.NORTHWEST,
 
115
                             GridBagConstraints.NONE,
 
116
                             new Insets(0,0,0,0),0,0);
 
117
    private Container panel;
 
118
    public String[] promptKeyboardInteractive(String destination,
 
119
                                              String name,
 
120
                                              String instruction,
 
121
                                              String[] prompt,
 
122
                                              boolean[] echo){
 
123
      panel = new JPanel();
 
124
      panel.setLayout(new GridBagLayout());
 
125
 
 
126
      gbc.weightx = 1.0;
 
127
      gbc.gridwidth = GridBagConstraints.REMAINDER;
 
128
      gbc.gridx = 0;
 
129
      panel.add(new JLabel(instruction), gbc);
 
130
      gbc.gridy++;
 
131
 
 
132
      gbc.gridwidth = GridBagConstraints.RELATIVE;
 
133
 
 
134
      JTextField[] texts=new JTextField[prompt.length];
 
135
      for(int i=0; i<prompt.length; i++){
 
136
        gbc.fill = GridBagConstraints.NONE;
 
137
        gbc.gridx = 0;
 
138
        gbc.weightx = 1;
 
139
        panel.add(new JLabel(prompt[i]),gbc);
 
140
 
 
141
        gbc.gridx = 1;
 
142
        gbc.fill = GridBagConstraints.HORIZONTAL;
 
143
        gbc.weighty = 1;
 
144
        if(echo[i]){
 
145
          texts[i]=new JTextField(20);
 
146
        }
 
147
        else{
 
148
          texts[i]=new JPasswordField(20);
 
149
        }
 
150
        panel.add(texts[i], gbc);
 
151
        gbc.gridy++;
 
152
      }
 
153
 
 
154
      if(JOptionPane.showConfirmDialog(null, panel, 
 
155
                                       destination+": "+name,
 
156
                                       JOptionPane.OK_CANCEL_OPTION,
 
157
                                       JOptionPane.QUESTION_MESSAGE)
 
158
         ==JOptionPane.OK_OPTION){
 
159
        String[] response=new String[prompt.length];
 
160
        for(int i=0; i<prompt.length; i++){
 
161
          response[i]=texts[i].getText();
 
162
        }
 
163
        return response;
 
164
      }
 
165
      else{
 
166
        return null;  // cancel
 
167
      }
 
168
    }
104
169
  }
105
 
 
106
170
}
107
171
 
108
172