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

« back to all changes in this revision

Viewing changes to examples/Shell.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
import java.io.*;
 
8
 
 
9
public class Shell{
 
10
  public static void main(String[] arg){
 
11
 
 
12
    try{
 
13
      JSch jsch=new JSch();
 
14
 
 
15
      //jsch.setKnownHosts("/home/foo/.ssh/known_hosts");
 
16
 
 
17
      String host=JOptionPane.showInputDialog("Enter username@hostname",
 
18
                                              System.getProperty("user.name")+
 
19
                                              "@localhost"); 
 
20
      String user=host.substring(0, host.indexOf('@'));
 
21
      host=host.substring(host.indexOf('@')+1);
 
22
 
 
23
      Session session=jsch.getSession(user, host, 22);
 
24
      //session.setPassword("your password");
 
25
 
 
26
      // username and password will be given via UserInfo interface.
 
27
      UserInfo ui=new MyUserInfo();
 
28
      session.setUserInfo(ui);
 
29
 
 
30
      //java.util.Hashtable config=new java.util.Hashtable();
 
31
      //config.put("StrictHostKeyChecking", "no");
 
32
      //session.setConfig(config);
 
33
 
 
34
      session.connect();
 
35
 
 
36
      Channel channel=session.openChannel("shell");
 
37
 
 
38
      channel.setInputStream(System.in);
 
39
      channel.setOutputStream(System.out);
 
40
 
 
41
      channel.connect();
 
42
    }
 
43
    catch(Exception e){
 
44
      System.out.println(e);
 
45
    }
 
46
  }
 
47
 
 
48
  public static class MyUserInfo implements UserInfo{
 
49
    public String getPassword(){ return passwd; }
 
50
    public boolean promptYesNo(String str){
 
51
      Object[] options={ "yes", "no" };
 
52
      int foo=JOptionPane.showOptionDialog(null, 
 
53
             str,
 
54
             "Warning", 
 
55
             JOptionPane.DEFAULT_OPTION, 
 
56
             JOptionPane.WARNING_MESSAGE,
 
57
             null, options, options[0]);
 
58
       return foo==0;
 
59
    }
 
60
  
 
61
    String passwd;
 
62
    JTextField passwordField=(JTextField)new JPasswordField(20);
 
63
 
 
64
    public String getPassphrase(){ return null; }
 
65
    public boolean promptPassphrase(String message){ return true; }
 
66
    public boolean promptPassword(String message){
 
67
      Object[] ob={passwordField}; 
 
68
      int result=
 
69
          JOptionPane.showConfirmDialog(null, ob, message,
 
70
                                        JOptionPane.OK_CANCEL_OPTION);
 
71
      if(result==JOptionPane.OK_OPTION){
 
72
        passwd=passwordField.getText();
 
73
        return true;
 
74
      }
 
75
      else{ return false; }
 
76
    }
 
77
    public void showMessage(String message){
 
78
      JOptionPane.showMessageDialog(null, message);
 
79
    }
 
80
  }
 
81
 
 
82
}
 
83
 
 
84