[英]How can I make a session in JSch to last longer?
I am new to JSch and i am using it to connect to a remote linux machine.我是 JSch 的新手,我正在使用它连接到远程 linux 机器。 I am creating a session and opening a channel to execute a command on the linux machine.
我正在创建一个会话并打开一个通道以在 linux 机器上执行命令。 This command takes nearly half an hour to execute and give an output.
这个命令需要将近半个小时来执行并给出输出。 But the session expires after 15 minutes.
但是会话会在 15 分钟后过期。 I need the session to be active till the command completes execution.
我需要会话处于活动状态,直到命令完成执行。 How can I make the session last longer?
我怎样才能使会话持续更长时间?
Even the usage of sendKeepAliveMsg()
function is not keeping the session active for not more than 15 minutes.即使使用
sendKeepAliveMsg()
函数也不会使会话保持活动状态不超过 15 分钟。
The code i am using is as below我使用的代码如下
JSch jsch=new JSch();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
Session session = null;
try
{
session = jsch.getSession(user,client_ip,22);
session.setPassword(secure_pwd);
session.setConfig(config);
session.connect();
session.sendKeepAliveMsg();
Channel channel = null;
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
InputStream in = null;
in = channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true)
{
while(in.available()>0)
{
int i=in.read(tmp, 0, 1024);
if(i<0)
break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed())
{
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
Thanks in advance提前致谢
You have to call the sendKeepAliveMsg
regularly.您必须定期调用
sendKeepAliveMsg
。
Calling it once at start has no effect.在开始时调用一次没有效果。
Though easiers is (as @Aniruddh also answered) to let JSch send the keepalives automatically.尽管更简单的是(正如@Aniruddh 也回答了)让 JSch 自动发送 keepalive。 Just call
Session.setServerAliveInterval
.只需调用
Session.setServerAliveInterval
。
Idle timeout for jsch can be adjusted using the following setServerAliveInterval可以使用以下setServerAliveInterval调整 jsch 的空闲超时
Additionally some other timeouts connected related can be specified in code as well, for example此外,还可以在代码中指定一些其他相关的超时连接,例如
session.connect(5000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.