我从google上获取了一个示例程序,并执行了该程序,但获得了预期的结果,但是该程序没有自动终止,并且每次都以eclipse手动终止。
我关闭了频道,会话,输入流。
在我的程序中,我使用字节数组获取控制台输入,但无法关闭。
感谢您的帮助。
代码在日食中运行:
import java.io.InputStream;
import java.util.Properties;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SSHConnectionJava {
public static void main(String[] args) {
String host = "10.140.20.23"; ///custom server
String user = "demo";
String password = "password";
String scriptname = "/home/ababu/create.sh";
StringBuilder sb = new StringBuilder();
sb.append(scriptname);
sb.append(" ");
sb.append("T12345");
String command = sb.toString();
try {
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
// Create a JSch session to connect to the server
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
// Establish the connection
session.connect();
System.out.println("Connected...");
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setErrStream(System.err);
channel.setInputStream(System.in);
InputStream 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;
}
Thread.sleep(1000);
}
channel.disconnect();
session.disconnect();
System.out.println("DONE!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
在/home/ababu/create.sh内部: mkdir $ 1