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 | |
|
13 | |
|
14 | |
public class SshSpawn extends AbstractSpawnable implements Spawnable { |
15 | |
|
16 | |
|
17 | |
|
18 | |
private String m_remoteHost; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
private int m_remotePort; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 1 | private Session m_session = null ; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
private OutputStream m_fromSocket; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
private InputStream m_toSocket; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 1 | private String m_username = null ; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 1 | private String m_password = null ; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 1 | private Channel m_channel = null ; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | 0 | public SshSpawn(String remoteHostName, int remotePort, String username, String password) { |
63 | 0 | m_remotePort = remotePort; |
64 | 0 | m_remoteHost = remoteHostName ; |
65 | 0 | this.m_username = username ; |
66 | 0 | this.m_password = password ; |
67 | 0 | } |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 1 | public SshSpawn(Channel channel) throws IOException { |
78 | 1 | if (!channel.isConnected()) { |
79 | |
try { |
80 | 1 | channel.connect(); |
81 | 0 | } catch (JSchException e) { |
82 | 0 | throw new IOException("Failed connecting the channel", e) ; |
83 | 1 | } |
84 | |
} |
85 | |
|
86 | 1 | this.m_channel = channel; |
87 | 1 | m_toSocket = m_channel.getInputStream(); |
88 | 1 | m_fromSocket = m_channel.getOutputStream(); |
89 | 1 | } |
90 | |
|
91 | |
public void start() throws IOException { |
92 | 1 | if (m_toSocket != null) { |
93 | |
|
94 | |
|
95 | |
|
96 | 1 | return; |
97 | |
} |
98 | |
|
99 | |
try { |
100 | 0 | m_session = new JSch().getSession(m_username, m_remoteHost, m_remotePort) ; |
101 | 0 | m_session.setPassword(m_password) ; |
102 | 0 | m_session.setConfig("StrictHostKeyChecking", "no"); |
103 | 0 | m_session.connect() ; |
104 | 0 | m_channel = m_session.openChannel("shell") ; |
105 | 0 | m_channel.connect() ; |
106 | 0 | } catch (JSchException e) { |
107 | 0 | throw new IOException("Unable to establish SSH session/channel", e) ; |
108 | 0 | } |
109 | 0 | m_toSocket = m_channel.getInputStream() ; |
110 | 0 | m_fromSocket = m_channel.getOutputStream(); |
111 | 0 | } |
112 | |
|
113 | |
public InputStream getStdout() { |
114 | 1 | return m_toSocket; |
115 | |
} |
116 | |
|
117 | |
public OutputStream getStdin() { |
118 | 1 | return m_fromSocket; |
119 | |
} |
120 | |
|
121 | |
public InputStream getStderr() { |
122 | 1 | return null; |
123 | |
} |
124 | |
|
125 | |
public boolean isClosed() { |
126 | 2 | if (m_channel != null) { |
127 | 1 | if (m_channel.isClosed()) { |
128 | |
|
129 | 0 | stop(); |
130 | |
} |
131 | |
} |
132 | 2 | return m_channel == null; |
133 | |
} |
134 | |
|
135 | |
public int getExitValue() { |
136 | 1 | return 0; |
137 | |
} |
138 | |
|
139 | |
public void stop() { |
140 | 1 | if (m_channel == null) { |
141 | 0 | return; |
142 | |
} |
143 | |
|
144 | 1 | m_channel.disconnect(); |
145 | 1 | m_channel = null; |
146 | |
|
147 | 1 | if (m_session != null) { |
148 | 0 | m_session.disconnect(); |
149 | 0 | m_session = null; |
150 | |
} |
151 | 1 | m_toSocket = null; |
152 | 1 | m_fromSocket = null; |
153 | 1 | } |
154 | |
} |