【发布时间】:2019-01-07 16:32:17
【问题描述】:
我目前正在开发一个包含 Jsch 的 java 自动化应用程序。但是,当我运行我的代码时,它会返回一个错误,指出未设置 TERM 环境。
我已经尝试通过选择环境变量在 intellij 中手动添加环境。然后我添加 TERM=xterm。虽然当我运行它时,它仍然失败。
import com.jcraft.jsch.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Driver {
public static void main(String[] args) throws Exception {
JSch jsch = new JSch();
Session session;
try {
// Open a Session to remote SSH server and Connect.
// Set User and IP of the remote host and SSH port.
session = jsch.getSession("username", "host", 22);
// When we do SSH to a remote host for the 1st time or if key at the remote host
// changes, we will be prompted to confirm the authenticity of remote host.
// This check feature is controlled by StrictHostKeyChecking ssh parameter.
// By default StrictHostKeyChecking is set to yes as a security measure.
session.setConfig("StrictHostKeyChecking", "no");
//Set password
session.setPassword("password");
session.connect();
// create the execution channel over the session
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
// Set the command to execute on the channel and execute the command
channelExec.setCommand("./script.sh");
channelExec.connect();
// Get an InputStream from this channel and read messages, generated
// by the executing command, from the remote side.
InputStream in = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// Command execution completed here.
// Retrieve the exit status of the executed command
int exitStatus = channelExec.getExitStatus();
if (exitStatus > 0) {
System.out.println("Remote script exec error! " + exitStatus);
}
//Disconnect the Session
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
可能已经回答:stackoverflow.com/q/13748784/1531971stackoverflow.com/q/12900923/1531971(如果没有,edit 问题并告诉我们为什么这些不适用。)
-
@jdv 顺便感谢一下。虽然您发布的问题的答案只是告诉我去哪里更改或添加环境变量。我已经知道去哪里了,我只需要一种方法来修复我的代码中的一个错误,它给我一个未知的终端错误。我相信这与我的 IDE 及其与 TERM 变量的配置有关。
标签: java intellij-idea