sodiska saw
2015-02-07 09:28:07 UTC
Hi,
I am trying to implement an apps that send multiple commands one at a time to a remote system. However, after I execute the first command, I get error. From the debug trace, the error found:
DEBUG: Caught an exception, leaving main loop due to End of IO Stream ReadDEBUG: Disconnecting from cheesoon.revenuenetwork.net port 22
Below is part of the codes. Summary of my codes is that my second command, I re-initiate channel object and re-used the session object. Is that the right way to things? I test it and its not working says session has been closed due to the above DEBUG reason. If I re-initiate the session, then its ok. Is this the right way?
//FIRST commandSession session=jsch.getSession(user, host, 22); session.connect();
command = "display version"; Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in=channel.getInputStream(); OutputStream out = channel.getOutputStream(); String versionDesc = null; String outgoing = null; String temp = null; channel.connect(); byte[] tmp=new byte[1024]; while(true){ while(in.available()>0){ int i=in.read(tmp, 0, 1024); if(i<0) break; temp = new String(tmp, 0, i); if (versionDesc == null) { temp.replaceAll("^\\s+", ""); temp.replaceAll("^(\\r\\n)+", ""); versionDesc = temp; System.out.println(versionDesc); } else versionDesc += temp; outgoing = new String (" "); // keeps listing the config values out.write(outgoing.getBytes()); out.flush(); } if(channel.isClosed()){ if(in.available()>0) continue; System.out.println("exit-status: "+channel.getExitStatus()); break; } try{Thread.sleep(1000);}catch(Exception ee){} } channel.disconnect(); //session.disconnect();
//SECOND COMMANDcommand = "display current configuration"; Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in=channel.getInputStream(); OutputStream out = channel.getOutputStream(); String versionDesc = null; String outgoing = null; String temp = null; channel.connect(); byte[] tmp=new byte[1024]; while(true){ while(in.available()>0){ int i=in.read(tmp, 0, 1024); if(i<0) break; temp = new String(tmp, 0, i); if (versionDesc == null) { temp.replaceAll("^\\s+", ""); temp.replaceAll("^(\\r\\n)+", ""); versionDesc = temp; System.out.println(versionDesc); } else versionDesc += temp; outgoing = new String (" "); // keeps listing the config values out.write(outgoing.getBytes()); out.flush(); } if(channel.isClosed()){ if(in.available()>0) continue; System.out.println("exit-status: "+channel.getExitStatus()); break; } try{Thread.sleep(1000);}catch(Exception ee){} }
I am trying to implement an apps that send multiple commands one at a time to a remote system. However, after I execute the first command, I get error. From the debug trace, the error found:
DEBUG: Caught an exception, leaving main loop due to End of IO Stream ReadDEBUG: Disconnecting from cheesoon.revenuenetwork.net port 22
Below is part of the codes. Summary of my codes is that my second command, I re-initiate channel object and re-used the session object. Is that the right way to things? I test it and its not working says session has been closed due to the above DEBUG reason. If I re-initiate the session, then its ok. Is this the right way?
//FIRST commandSession session=jsch.getSession(user, host, 22); session.connect();
command = "display version"; Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in=channel.getInputStream(); OutputStream out = channel.getOutputStream(); String versionDesc = null; String outgoing = null; String temp = null; channel.connect(); byte[] tmp=new byte[1024]; while(true){ while(in.available()>0){ int i=in.read(tmp, 0, 1024); if(i<0) break; temp = new String(tmp, 0, i); if (versionDesc == null) { temp.replaceAll("^\\s+", ""); temp.replaceAll("^(\\r\\n)+", ""); versionDesc = temp; System.out.println(versionDesc); } else versionDesc += temp; outgoing = new String (" "); // keeps listing the config values out.write(outgoing.getBytes()); out.flush(); } if(channel.isClosed()){ if(in.available()>0) continue; System.out.println("exit-status: "+channel.getExitStatus()); break; } try{Thread.sleep(1000);}catch(Exception ee){} } channel.disconnect(); //session.disconnect();
//SECOND COMMANDcommand = "display current configuration"; Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); channel.setInputStream(null); ((ChannelExec)channel).setErrStream(System.err); InputStream in=channel.getInputStream(); OutputStream out = channel.getOutputStream(); String versionDesc = null; String outgoing = null; String temp = null; channel.connect(); byte[] tmp=new byte[1024]; while(true){ while(in.available()>0){ int i=in.read(tmp, 0, 1024); if(i<0) break; temp = new String(tmp, 0, i); if (versionDesc == null) { temp.replaceAll("^\\s+", ""); temp.replaceAll("^(\\r\\n)+", ""); versionDesc = temp; System.out.println(versionDesc); } else versionDesc += temp; outgoing = new String (" "); // keeps listing the config values out.write(outgoing.getBytes()); out.flush(); } if(channel.isClosed()){ if(in.available()>0) continue; System.out.println("exit-status: "+channel.getExitStatus()); break; } try{Thread.sleep(1000);}catch(Exception ee){} }