我试图用Jsch在远程服务器上执行一个.csh脚本。我能够执行cp、mv和ls等命令。但是当我试图执行一个内部引用一些环境变量的脚本时,脚本退出时状态为1。在script.sh中引用了一个INTERNAL_ENV_VARIABLE,当我用exec运行时,它是无法访问的。有什么办法可以让我从exec中运行.csh脚本来处理这个依赖关系吗?
使用shell而不是exec不是一个选项,因为当我们打开shell时有多个认证级别,这将使我们正在开发的测试框架依赖于多个凭证。
我调用的命令是导航到脚本目录并执行脚本。
util.executeCommand(session,"cd " + script directory+";"+"./script.csh");
console output
com.jcraft.jsch.Channel$MyPipedInputStream@4bff64c2
INTERNAL_ENV_VARIABLE: Undefined variable.
exit-status: 1
执行命令的方法:executeCommand
public int executeCommand(Session session, String script) throws JSchException, IOException {
System.out.println("Execute Script " + script);
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
((ChannelExec)channelExec).setPty(true);
InputStream in = channelExec.getInputStream();
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.setCommand(script);
channelExec.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
System.out.println(channelExec.getErrStream());
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
if (channelExec.isClosed()) {
System.out.println("exit-status: " + channelExec.getExitStatus());
break;
try {
Thread.sleep(1000);
} catch (Exception ee) {
System.out.println(ee);
channelExec.disconnect();
return channelExec.getExitStatus();
创建一个会话的方法:createSession
public Session createSession(String user, String host, int Port, String Password) throws JSchException {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, Port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(Password);
session.connect(5000);
System.out.println(session.isConnected());
return session;