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

« back to all changes in this revision

Viewing changes to examples/UserAuthPubKey.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-01-13 16:07:20 UTC
  • Revision ID: james.westby@ubuntu.com-20050113160720-7ohwkg0rbifgq3lu
Tags: upstream-0.1.19
ImportĀ upstreamĀ versionĀ 0.1.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*-mode:java; c-basic-offset:2; -*- */
 
2
import com.jcraft.jsch.*;
 
3
import java.awt.*;
 
4
import java.awt.event.*;
 
5
import javax.swing.*;
 
6
 
 
7
public class UserAuthPubKey{
 
8
  public static void main(String[] arg){
 
9
 
 
10
    try{
 
11
      JSch jsch=new JSch();
 
12
 
 
13
      JFileChooser chooser = new JFileChooser();
 
14
      chooser.setDialogTitle("Choose your privatekey(ex. ~/.ssh/id_dsa)");
 
15
      chooser.setFileHidingEnabled(false);
 
16
      int returnVal = chooser.showOpenDialog(null);
 
17
      if(returnVal == JFileChooser.APPROVE_OPTION) {
 
18
        System.out.println("You chose "+
 
19
                           chooser.getSelectedFile().getAbsolutePath()+".");
 
20
        jsch.addIdentity(chooser.getSelectedFile().getAbsolutePath()
 
21
//                       , "passphrase"
 
22
                         );
 
23
      }
 
24
 
 
25
      String host=JOptionPane.showInputDialog("Enter username@hostname",
 
26
                                              System.getProperty("user.name")+
 
27
                                              "@localhost"); 
 
28
      String user=host.substring(0, host.indexOf('@'));
 
29
      host=host.substring(host.indexOf('@')+1);
 
30
 
 
31
      Session session=jsch.getSession(user, host, 22);
 
32
 
 
33
      // username and passphrase will be given via UserInfo interface.
 
34
      UserInfo ui=new MyUserInfo();
 
35
      session.setUserInfo(ui);
 
36
      session.connect();
 
37
 
 
38
      Channel channel=session.openChannel("shell");
 
39
 
 
40
      channel.setInputStream(System.in);
 
41
      channel.setOutputStream(System.out);
 
42
 
 
43
      channel.connect();
 
44
    }
 
45
    catch(Exception e){
 
46
      System.out.println(e);
 
47
    }
 
48
  }
 
49
 
 
50
 
 
51
  public static class MyUserInfo implements UserInfo{
 
52
    public String getPassword(){ return null; }
 
53
    public boolean promptYesNo(String str){
 
54
      Object[] options={ "yes", "no" };
 
55
      int foo=JOptionPane.showOptionDialog(null, 
 
56
             str,
 
57
             "Warning", 
 
58
             JOptionPane.DEFAULT_OPTION, 
 
59
             JOptionPane.WARNING_MESSAGE,
 
60
             null, options, options[0]);
 
61
       return foo==0;
 
62
    }
 
63
  
 
64
    String passphrase;
 
65
    JTextField passphraseField=(JTextField)new JPasswordField(20);
 
66
 
 
67
    public String getPassphrase(){ return passphrase; }
 
68
    public boolean promptPassphrase(String message){
 
69
      Object[] ob={passphraseField};
 
70
      int result=
 
71
        JOptionPane.showConfirmDialog(null, ob, message,
 
72
                                      JOptionPane.OK_CANCEL_OPTION);
 
73
      if(result==JOptionPane.OK_OPTION){
 
74
        passphrase=passphraseField.getText();
 
75
        return true;
 
76
      }
 
77
      else{ return false; }
 
78
    }
 
79
    public boolean promptPassword(String message){ return true; }
 
80
    public void showMessage(String message){
 
81
      JOptionPane.showMessageDialog(null, message);
 
82
    }
 
83
  }
 
84
}