[JSch-users] Application not exiting
Status: Alpha
Brought to you by:
ymnk
From: Chandolu, Y. <YCh...@ar...> - 2006-02-27 20:56:20
|
Hi All, =20 I am using ChannelExec to run a command remotely. I have attached code at the very end. I ran the program in command prompt and the problem is after I run the program it does not exit. I have to press <ctrl>+c for the program to exit. I have taken the code from examples on the site. Please suggest what should I do for the program to exit gracefully. =20 I am using jsch 0.1.25 version java 1.4.2_03 platform is windows 2000 professional =20 Thanks in advance Yuva =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D package com.thexchange.test; =20 import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Vector; =20 import com.jcraft.*; import com.jcraft.jsch.*; import com.jcraft.jsch.jce.*; import com.thexchange.utility.Logger; =20 public class SSHExec { =20 public static class MyUserInfo implements UserInfo{ public String getPassword(){ return passwd; } public void setPassword(String password){ this.passwd=3Dpassword; } public boolean promptYesNo(String str){return true;} =20 =20 String passwd; =20 public String getPassphrase(){ return null; } public boolean promptPassphrase(String message){ return true; } public boolean promptPassword(String message){return true;} public void showMessage(String message){} } =20 public static void main(String[] args) throws Exception { =20 JSch jsch=3Dnew JSch(); =20 Session session=3Djsch.getSession("user", "host", 22); System.out.println("Session created"); session.setTimeout(31000); UserInfo ui=3Dnew MyUserInfo(); ui.setPassword("password"); session.setUserInfo(ui); =20 session.connect(); =20 Channel channel=3Dsession.openChannel("exec"); System.out.println("Channel opened"); ((ChannelExec)channel).setCommand("mycommand"); channel.setXForwarding(true); channel.setInputStream(System.in); channel.setOutputStream(System.out); channel.connect(); System.out.println("Channel connected"); =20 InputStream in =3D channel.getInputStream(); byte[] tmp=3Dnew byte[1024]; String output =3D null; while(true){ while(in.available()>0){ int i=3Din.read(tmp, 0, 1024); if(i<0) { break; } output =3D new String(tmp, 0, i); System.out.print(output); } if(channel.isEOF()){ System.out.println("exit-status: "+channel.getExitStatus()); break; } Thread.sleep(100); } channel.disconnect(); session.disconnect(); System.out.println("Disconnected"); =20 } =20 }//end SSHExec.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D |