1
/* -*-mode:java; c-basic-offset:2; -*- */
2
import com.jcraft.jsch.*;
4
import java.awt.event.*;
7
public class UserAuthPubKey{
8
public static void main(String[] arg){
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()
25
String host=JOptionPane.showInputDialog("Enter username@hostname",
26
System.getProperty("user.name")+
28
String user=host.substring(0, host.indexOf('@'));
29
host=host.substring(host.indexOf('@')+1);
31
Session session=jsch.getSession(user, host, 22);
33
// username and passphrase will be given via UserInfo interface.
34
UserInfo ui=new MyUserInfo();
35
session.setUserInfo(ui);
38
Channel channel=session.openChannel("shell");
40
channel.setInputStream(System.in);
41
channel.setOutputStream(System.out);
46
System.out.println(e);
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,
58
JOptionPane.DEFAULT_OPTION,
59
JOptionPane.WARNING_MESSAGE,
60
null, options, options[0]);
65
JTextField passphraseField=(JTextField)new JPasswordField(20);
67
public String getPassphrase(){ return passphrase; }
68
public boolean promptPassphrase(String message){
69
Object[] ob={passphraseField};
71
JOptionPane.showConfirmDialog(null, ob, message,
72
JOptionPane.OK_CANCEL_OPTION);
73
if(result==JOptionPane.OK_OPTION){
74
passphrase=passphraseField.getText();
79
public boolean promptPassword(String message){ return true; }
80
public void showMessage(String message){
81
JOptionPane.showMessageDialog(null, message);