gpt4 book ai didi

java - Jsch - 执行 Shell 脚本后退出

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:41 25 4
gpt4 key购买 nike

我正在尝试使用 Jsch 编写 Java 程序,我想使用该程序开始执行 shell 脚本,并在 shell 脚本执行完成后退出程序。

    String userName = "";
String hostName = "";
String password = "";

JSch javaSecureChannel = new JSch();

Session jschSession = null;
Channel jschChannel = null;

try {

jschSession = javaSecureChannel.getSession(userName, hostName, 22);
Properties configurationProperties = new Properties();
configurationProperties.put("StrictHostKeyChecking", "no");
jschSession.setConfig(configurationProperties);
jschSession.setPassword(password);
jschSession.connect();
jschChannel = null;
jschChannel = jschSession.openChannel("shell");
jschChannel.setOutputStream(System.out);

File shellScript = createShellScript();

FileInputStream fin = new FileInputStream(shellScript);
byte fileContent[] = new byte[(int) shellScript.length()];
fin.read(fileContent);
InputStream in = new ByteArrayInputStream(fileContent);
jschChannel.setInputStream(in);
jschChannel.connect();

while(true){

//if(jschChannel.isClosed)
if(jschChannel.getExitStatus() == 0){
System.out.println("exit-status: " + jschChannel.getExitStatus());
break;
}

try{
Thread.sleep(1000);
}catch(Exception ee){
ee.printStackTrace();
}

}

} catch (Exception e) {
e.printStackTrace();
}

jschChannel.disconnect();
jschSession.disconnect();
System.out.println("Done...!!!");

createShellScript方法如下。

    String temporaryShellFileName = "shellscript.sh";
File fileStream = new File(temporaryShellFileName);

try {

PrintStream outStream = new PrintStream(new FileOutputStream(fileStream));
outStream.println("#!/bin/bash");
outStream.println("cd /u01/app/java/gids4x/Test");
outStream.println("Test_with_NULL.sh");
outStream.close();

} catch (Exception e) {

System.err.println("Error: " + e.getMessage());

}

使用上面的代码,我可以开始执行 shell 脚本。但是,即使程序执行完成后,即脚本执行完成后,我也不会终止程序执行。

您能否建议具体需要做什么?

最佳答案

我也遇到了同样的问题,并通过以下两个步骤解决了这个问题:1.发送最后一个命令为“exit”2. 让我的邮件线程 hibernate 直到 channel 打开

请找到如下代码:

public static void main(String[] arg){

Channel channel=null;
Session session=null;
try{
JSch.setLogger(new MyLogger());
JSch jsch=new JSch();
jsch.addIdentity("C:\\testLinuxKey.key");

String host=null;
if(arg.length>0){
host=arg[0];
}
else{
host=JOptionPane.showInputDialog("Enter username@hostname",
System.getProperty("user.name")+
"@localhost");
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);

session=jsch.getSession(user, host, 22);

// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);

session.connect();

channel=session.openChannel("shell");

//channel.setInputStream(System.in);
channel.setOutputStream(System.out);

String temporaryShellFileName = "shellscript.sh";
File fileStream = new File(temporaryShellFileName);

try {

PrintStream outStream = new PrintStream(new FileOutputStream(fileStream));
outStream.println("id");
outStream.println("sudo bash");
outStream.println("cd /tmp/");
outStream.println("/tmp/wasinfo.sh");
outStream.println("exit");
outStream.println("exit");
outStream.close();
FileInputStream fin = new FileInputStream(fileStream);
byte fileContent[] = new byte[(int) fileStream.length()];
fin.read(fileContent);
InputStream in = new ByteArrayInputStream(fileContent);
channel.setInputStream(in);

} catch (Exception e) {

System.err.println("Error: " + e.getMessage());

}

channel.connect();
while(channel.isConnected())
{
System.out.println("----- Channel On ----");
Thread.currentThread().sleep(5000);
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
channel.disconnect();
session.disconnect();
}

System.out.println("Main End");
}

关于java - Jsch - 执行 Shell 脚本后退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com