我正试图通过单jSCH session
打开多个频道
这是我的代码。
public class PoolTest {
private static Hashtable<String, Session> pool = new Hashtable<>();
/**
* @param args
* @throws InterruptedException
* @throws IOException
* @throws JSchException
*/
public static void main(String[] args) throws JSchException, IOException, InterruptedException {
PoolTest test = new PoolTest();
test.runCommand("show system");
test.runCommand("show version");
}
private void runCommand(String command) throws JSchException, IOException, InterruptedException{
Session session = createNewSession();
System.out.println(session.isConnected());
Channel channel = session.openChannel("shell");
((ChannelShell) channel).setPtyType("dumb");
channel.connect();
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);
channel.connect();
ps.println(command);
ps.close();
InputStream in=channel.getInputStream();
byte[] bt=new byte[1024];
while(true)
{
while(in.available()>0)
{
int i=in.read(bt, 0, 1024);
if(i<0)
break;
String str=new String(bt, 0, i);
//displays the output of the command executed.
System.out.print(str);
}
if(channel.isClosed())
{
break;
}
Thread.sleep(1000);
channel.disconnect();
}
channel.disconnect();
}
private Session createNewSession() throws JSchException{
if(pool.get("10.94.101.235")==null){
System.out.println("creating new session");
JSch jsch = new JSch();
Session session;
session = jsch.getSession("unknown", "10.94.101.235", 22);
session.setPassword("wont_tell");
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(60000);
session.connect();
pool.put("10.94.101.235", session);
return session;
}
else
return pool.get("10.94.101.235");
}
}
正如所料,第一个命令提供了适当的数据。但是,第二个命令对isConnected
调用显示为false。
线程中的异常&#34; main&#34; com.jcraft.jsch.JSchException:会话已关闭 在com.jcraft.jsch.Session.openChannel(Session.java:844) 在com.dell.supportassist.collector.test.PoolTest.runCommand(PoolTest.java:40) 在com.dell.supportassist.collector.test.PoolTest.main(PoolTest.java:34)
关于为什么会话断开连接的任何想法?