Hi I am trying to create a putty like interface in my web application. 1. It will have a text box to enter a command (Temporarily hard coding the server user credentials) and If the user hits return key, it will send ajax request to the server. 2. Server will creates jsch & session and channle object and execute that user command in the remote shell. 3. And I will populate the response in user browser screen. I don’t want the point number two as above for further request. I want it as “Server will check for existing channel and using that channel it will execute”. To achieve this I tried storing the channel object in session. But I need to execute the .connect() method of the channel object on every request (Which return Last Login time…., It seem it is doing login process using the older credentials) ie, Only the state is store in terms of user name & password and not the connect and server session. Could someone suggest me a way to get a solution for my problem with JSch. Or suggest me any other way to achieve my requirement. (Putty like interface in Browser window)
ie, I am looking for a way to create asynchronous & stateless ssh connection using JSch ?
This is my code
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String createSession = request.getParameter("createSession");
String logOff = request.getParameter("logOff");
userVoice = request.getParameter("string");
userVoice = userVoice == null ? "" : userVoice;
userVoice = userVoice + "\n";
writer = response.getWriter();
try {
HttpSession httpSession = request.getSession();
//channel = (Channel) httpSession.getAttribute("channel");
if(channel!= null && channel.isConnected())
{
/*
* channelOutput = (InputStream) httpSession
* .getAttribute("channelOutput"); channelInput = (OutputStream)
* httpSession .getAttribute("channelInput");
*/
channelOutput = channel.getInputStream();
channelInput = channel.getOutputStream();
}
if (createSession != null && logOff == null) {
String username = request.getParameter("username"); // "bninet";
String password = request.getParameter("password"); // "password";
String host = request.getParameter("host"); // "10.77.246.120";
// // sample ip
// address
int port = Integer.parseInt(request.getParameter("port"));
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
session.setConfig(properties);
session.setPassword(password);
session.connect(30000);
channel = session.openChannel("shell");
setIOforChannel(channel, httpSession);
// httpSession.setAttribute("channel", channel);
} else if (channelOutput != null && channelInput != null) {
if (logOff != null) {
userVoice = "exit";
}
channelInput.write((userVoice + "\n").getBytes());
//channel.connect();
if (logOff != null) {
channel.disconnect();
// httpSession.removeAttribute("channelOutput");
// httpSession.removeAttribute("channelInput");
}
} else {
writer.write("No session Available.\n Please create a session using createSession tool ");
return;
}
Thread.sleep(1000);
String returnData = streamToString(channelOutput);
int i = 0;
while (!returnData.isEmpty() && i < 5) {
writer.write(returnData);
Thread.sleep(1000);
returnData = streamToString(channelOutput);
i++;
}
} catch (Exception e) {
writer.write("Error Occured -- " + e.getMessage());
} finally {
writer.flush();
writer.close();
}
}
If you reuse a Channel, it reuses the session, which holds your credentials.
In order to use different credentials, you would need to disconnect the session, change its settings and reconnect it.
How to disconnect the session.
If you are wanting to reuse the session, you dont need to reconect the channel each time. Connect it once as a shell, plugging an input and output stream into it. Use the streams to pass commands and capture output.