Please send the answer to the list, as I am interested in this case too.
On 3/17/10, Vinay S (vinas) <[email protected]> wrote: > Hi, > > My apologies if this a repetitive post. I searched for similar > issues and couldn't find any in the forum. However, I could have missed. > > > > I have a usecase where I would want to programmatically fire a bunch of > commands one after the other to a router which is SSH enabled in a > single session. > > > > I tried the following implementation > > * Maintaining a single session, I created a new channel of type > "exec" for every command sent, but when it came to creating the second > channel, I got an exception that the session got disconnected although I > never disconnected the session. Please advise how I can rectify this. I > do not want to create a session everytime a new command is sent. Would > using shell help? If so, how could I implement it. > > > > public void initSession() { > > JSch jsch = new JSch(); > > String host = "10.10.10.10"; > > String username = "abc"; > > String password = "abc123"; > > try { > > session = jsch.getSession(username, host, 22); > > session.setPassword(password); > > java.util.Properties config = new > java.util.Properties(); > > config.put("StrictHostKeyChecking", "no"); > > session.setConfig(config); > > session.setPassword(password); > > session.connect(); > > } catch (Exception e) { > > e.printStackTrace(); > > } > > } > > > > public void disconnectSession(){ > > session.disconnect(); > > } > > > > public void runCommand(String command) { > > try { > > > > Channel channel = session.openChannel("exec"); > > channel.setInputStream(null); > > channel.setOutputStream(System.out); > > > > ((ChannelExec) channel).setCommand(command); > > channel.connect(); > > InputStream in = channel.getInputStream(); > > 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; > > } > > try { > > Thread.sleep(1000); > > } catch (Exception ee) { > > } > > } > > channel.disconnect(); > > } catch (Exception e) { > > e.printStackTrace(); > > } > > } > > > > public static void main(String[] args) { > > String command = "sh flash"; > > TestSSH test = new TestSSH(); > > test.initSession(); > > test.runCommand("sh ip route"); > > test.runCommand("sh flash"); > > test.disconnectSession(); > > } > > > > Thanks in Advnace, > > Vinay > > -- Sent from my mobile device ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev _______________________________________________ JSch-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/jsch-users
