View Javadoc

1   package expectj;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.OutputStream;
6   import com.jcraft.jsch.Channel;
7   import com.jcraft.jsch.JSch;
8   import com.jcraft.jsch.JSchException;
9   import com.jcraft.jsch.Session;
10  
11  /***
12   * A Spawnable for controlling an SSH session using ExpectJ.
13   */
14  public class SshSpawn extends AbstractSpawnable implements Spawnable {
15      /***
16       * A reference to the remote host.
17       */
18      private String m_remoteHost;
19  
20      /***
21       * The port we're talking to on the remote host.
22       */
23      private int m_remotePort;
24  
25      /***
26       * Our communications channel to the remote host.
27       */
28      private Session m_session = null ;
29  
30      /***
31       * Use this to read data from the remote host.
32       */
33      private OutputStream m_fromSocket;
34  
35      /***
36       * Use this to write data to the remote host.
37       */
38      private InputStream m_toSocket;
39  
40      /***
41       * The username with which to authenticate
42       */
43      private String m_username = null ;
44  
45      /***
46       * The password with which to authenticate
47       */
48      private String m_password = null ;
49  
50      /***
51       * The JSch Channel of type "shell"
52       */
53      private Channel m_channel = null ;
54  
55      /***
56       * Construct a new SSH spawn.
57       * @param remoteHostName The remote host to connect to.
58       * @param remotePort The remote port to connect to.
59       * @param username The user name with which to authenticate
60       * @param password The password with which to authenticate
61       */
62      public SshSpawn(String remoteHostName, int remotePort, String username, String password) {
63          m_remotePort = remotePort;
64          m_remoteHost = remoteHostName ;
65          this.m_username = username ;
66          this.m_password = password ;
67      }
68  
69      /***
70       * Takes control over an existing SSH channel.
71       *
72       * @param channel The channel we should control.  If this channel isn't
73       * already connected, {@link Channel#connect()} will be called.
74       *
75       * @throws IOException If connecting the channel fails.
76       */
77      public SshSpawn(Channel channel) throws IOException {
78          if (!channel.isConnected()) {
79              try {
80                  channel.connect();
81              } catch (JSchException e) {
82                  throw new IOException("Failed connecting the channel", e) ;
83              }
84          }
85  
86          this.m_channel = channel;
87          m_toSocket = m_channel.getInputStream();
88          m_fromSocket = m_channel.getOutputStream();
89      }
90  
91      public void start() throws IOException {
92          if (m_toSocket != null) {
93              // We've probably been created by the SshSpawn(Channel) constructor,
94              // or start() has already been called.  No need to do anything
95              // anyway.
96              return;
97          }
98  
99      	try {
100 			m_session = new JSch().getSession(m_username, m_remoteHost, m_remotePort) ;
101 			m_session.setPassword(m_password) ;
102 			m_session.setConfig("StrictHostKeyChecking", "no");
103 			m_session.connect() ;
104 			m_channel = m_session.openChannel("shell") ;
105 			m_channel.connect() ;
106 		} catch (JSchException e) {
107 			throw new IOException("Unable to establish SSH session/channel", e) ;
108 		}
109         m_toSocket = m_channel.getInputStream() ;
110         m_fromSocket = m_channel.getOutputStream();
111     }
112 
113     public InputStream getStdout() {
114         return m_toSocket;
115     }
116 
117     public OutputStream getStdin() {
118         return m_fromSocket;
119     }
120 
121     public InputStream getStderr() {
122         return null;
123     }
124 
125     public boolean isClosed() {
126         if (m_channel != null) {
127             if (m_channel.isClosed()) {
128                 // We've been disconnected, shut down
129                 stop();
130             }
131         }
132         return m_channel == null;
133     }
134 
135     public int getExitValue() {
136         return 0;
137     }
138 
139     public void stop() {
140         if (m_channel == null) {
141             return;
142         }
143 
144         m_channel.disconnect();
145 		m_channel = null;
146 
147 		if (m_session != null) {
148 		    m_session.disconnect();
149 		    m_session = null;
150 		}
151         m_toSocket = null;
152         m_fromSocket = null;
153     }
154 }