View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed out the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.ssh;
24  
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import ch.ethz.ssh2.ChannelCondition;
29  import ch.ethz.ssh2.Connection;
30  import ch.ethz.ssh2.Session;
31  import net.smartlab.web.BusinessException;
32  
33  /**
34   * This class represents a secure remote shell obtained through the SSH
35   * protocol.
36   * 
37   * @author gperrone
38   * @author rlogiacco
39   */
40  public class Shell {
41  
42  	/**
43  	 * TODO documentation
44  	 */
45  	private String username;
46  
47  	/**
48  	 * TODO documentation
49  	 */
50  	private String password;
51  
52  	/**
53  	 * TODO documentation
54  	 */
55  	private String host;
56  
57  	/**
58  	 * TODO documentation
59  	 */
60  	private int port = 22;
61  	
62  	/**
63  	 * TODO documentation
64  	 */
65  	private long timeout = 1000;
66  
67  	/**
68  	 * TODO documentation
69  	 */
70  	private Connection connection;
71  
72  	/**
73  	 * TODO documentation
74  	 */
75  	private Session session;
76  
77  	/**
78  	 * TODO documentation
79  	 */
80  	private OutputStream out;
81  
82  	/**
83  	 * TODO documentation
84  	 */
85  	private InputStream in;
86  
87  	/**
88  	 * TODO documentation
89  	 */
90  	private byte[] buffer = new byte[4096];
91  
92  
93  	/**
94  	 * Returns the username.
95  	 * 
96  	 * @return the username.
97  	 */
98  	public String getUsername() {
99  		return username;
100 	}
101 
102 	/**
103 	 * Sets the username.
104 	 * 
105 	 * @param username the username to set.
106 	 */
107 	public void setUsername(String username) {
108 		this.username = username;
109 	}
110 
111 	/**
112 	 * Returns the password.
113 	 * 
114 	 * @return the password.
115 	 */
116 	public String getPassword() {
117 		return password;
118 	}
119 
120 	/**
121 	 * Sets the password.
122 	 * 
123 	 * @param password the password to set.
124 	 */
125 	public void setPassword(String password) {
126 		this.password = password;
127 	}
128 
129 	/**
130 	 * Returns the host.
131 	 * 
132 	 * @return the host.
133 	 */
134 	public String getHost() {
135 		return host;
136 	}
137 
138 	/**
139 	 * Sets the host.
140 	 * 
141 	 * @param host the host to set.
142 	 */
143 	public void setHost(String host) {
144 		this.host = host;
145 	}
146 
147 	/**
148 	 * Returns the port.
149 	 * 
150 	 * @return the port.
151 	 */
152 	public int getPort() {
153 		return port;
154 	}
155 
156 	/**
157 	 * Sets the port.
158 	 * 
159 	 * @param port the port to set.
160 	 */
161 	public void setPort(int port) {
162 		this.port = port;
163 	}
164 
165 	
166 	/**
167 	 * Returns the timeout.
168 	 * @return the timeout.
169 	 */
170 	public long getTimeout() {
171 		return timeout;
172 	}
173 
174 	
175 	/**
176 	 * Sets the timeout.
177 	 * @param timeout the timeout to set.
178 	 */
179 	public void setTimeout(long timeout) {
180 		this.timeout = timeout;
181 	}
182 
183 	/**
184 	 * TODO documentation
185 	 * @param host
186 	 * @param port
187 	 * @param username
188 	 * @param password
189 	 * @throws BusinessException 
190 	 * @throws IOException
191 	 */
192 	public void open() throws BusinessException, IOException {
193 		connection = new Connection(host, port);
194 		connection.connect();
195 		if (!connection.authenticateWithPassword(username, password)) {
196 			throw new BusinessException("Authentication failed");
197 		}
198 		session = connection.openSession();
199 		session.requestPTY("dumb", 80, 200, 0, 0, null);
200 		session.startShell();
201 		out = session.getStdin();
202 		in = session.getStdout();
203 	}
204 
205 	/**
206 	 * TODO documentation
207 	 * @return
208 	 * @throws IOException
209 	 */
210 	public String update() throws IOException {
211 		StringBuffer result = new StringBuffer();
212 		int status = session.waitForCondition(ChannelCondition.STDOUT_DATA, timeout);
213 		while (status != 1) {
214 			int read = in.read(buffer);
215 			result.append(new String(buffer, 0, read));
216 			status = session.waitForCondition(ChannelCondition.STDOUT_DATA, timeout);
217 		}
218 		return result.toString();
219 	}
220 
221 	/**
222 	 * TODO documentation
223 	 * @param command
224 	 * @return
225 	 * @throws IOException
226 	 */
227 	public String execute(String command) throws IOException {
228 		out.write(command.getBytes());
229 		out.write('\n');
230 		out.flush();
231 		return this.update();
232 	}
233 	
234 	/**
235 	 * TODO documentation
236 	 * @return
237 	 * @throws IOException
238 	 */
239 	public String interrupt() throws IOException {
240 		out.write(0x03); // CTRL+C
241 		out.write(0x04); // CTRL+D
242 		out.flush();
243 		return this.update();
244 	}
245 
246 	/**
247 	 * TODO documentation
248 	 */
249 	public void close() {
250 		session.close();
251 		connection.close();
252 	}
253 	
254 	/**
255 	 * @see java.lang.Object#toString()
256 	 */
257 	public String toString() {
258 		 return username + "@" + host + ":" + port;
259 	}
260 }