[JSch-users] Session.setTimeout...
Status: Alpha
Brought to you by:
ymnk
|
From: Bubba M. <me...@ya...> - 2008-01-16 23:39:13
|
So I had a version of my own SSHExec util that uses
the session.setTimeout method to set the timeout for
the SSHExec session/command which from back in the
0.1.28 version appeared to work as I expected. If I
set a timeout of 120 seconds in 120 seconds (if no
output came back)it would timeout. However I note now
that if I set a timeout of 120 seconds that after 120
seconds the timeout doesn't timeout.
Is there something that I am missing as in regards to
how to set a timeout for the command that SSHExec is
running to timeout on? It appears that the
session.setTimeout is a socket timeout now, which
perhaps in the 0.1.28 version maybe this was the
reason that my code was working there too. I looked
at the code to try and understand why I was seeing the
different behavior here, but can't explain it. In
regards to the setTimeout the code looks the same and
it looks like that both of the versions 0.1.28 and
0.1.34 use the timeout during connect to set the
socket timeout. For my test code I provided above for
the SSHExec util my command I am calling for testing
both cases would be "sleep 2400;ls- al" and I would
set the timeout to be 120 seconds. This sleep is the
unix sleep which is a command to wait 2400 seconds.
In the 0.1.28 case it times out at or near 120
seconds, in the 0.1.34 case it does not timeout.
Below is my code that I am using:
-----------
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import
org.apache.tools.ant.taskdefs.optional.ssh.SSHUserInfo;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.RuntimeException;
import java.nio.charset.Charset;
public class SSHExec {
/**
* @param args
*/
public static void main(String[] args) {
try {
// TODO Auto-generated method stub
// Check if all parameters are on the command
line
if (args.length < 5) {
throw new RuntimeException(
"usage: SSHExec.jar <host>
<username> <password> <command> <timeout(secs)>
[-suppress-output]");
}
// Host to connect execute command via SSH
on.
String host = args[0];
// Username to connect to remote machine.
String username = args[1];
// Password to connect to remote machine.
String password = args[2];
// Command to execute on remote machine.
String cmd = args[3];
// Timeout value in seconds (double
parameter)
Double timeout = new Double(args[4]);
// Default non-changable trust value to
use in SSHUserInfo object
boolean trust = true;
boolean suppress_output = false;
if(args.length == 6) {
suppress_output =
(args[5].equalsIgnoreCase("-suppress-output")?
true:false);
}
try {
JSch jsch = new JSch();
// Set username, host and port to
connect the session with.
Session session =
jsch.getSession(username, host, 22);
// Set the timeout value for the
session.
session.setTimeout(timeout.intValue()
* 1000);
// Set the password and the trust
certificates parameters
UserInfo ui = new
SSHUserInfo(password, trust);
// Set all of the UserInfo parameters
to the session.
session.setUserInfo(ui);
// Connect the session.
session.connect();
// Create an exec channel for the
session.
Channel channel =
session.openChannel("exec");
// Set the command to be executed on
the channel.
((ChannelExec)
channel).setCommand(cmd);
// Set the InputStream to null and
don't close the stream on channel disconnect/close.
channel.setInputStream(null,true);
// Set the OutputStream to System.out
(standard output) and don't close the stream on
channel disconnect/close.
channel.setOutputStream(System.out,true);
// Set the ErrStream to System.err
(standard error) and don't close the stream on channel
disconnect/close.
channel.setExtOutputStream(System.err,
true);
// Grab the channel input stream to
grab the ssh input.
InputStream in =
channel.getInputStream();
// Connect the channel to the session
with the parameters set previous.
channel.connect();
// This is where the output from the
SSH command gets read back across the network to the
local java program
byte[] tmp = new byte[1024];
String output = new String();
Thread.sleep(1000);
while (true) {
Thread.sleep(100);
if (session.isConnected() ==
false) {
throw new
RuntimeException("Connection Timed Out");
}
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
output = output + new
String(tmp, 0, i);
}
if (channel.isClosed() ||
(channel.getExitStatus() != -1)) {
break;
}
}
// Disconnect the channel and the
session.
channel.disconnect();
session.disconnect();
// Output the stream returned back
from the SSH exec command on the standard output.
if(!suppress_output) {
System.out.print(output.replaceAll("\n", "\r\n"));
}
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
System.exit(-1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------
____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
|