我正在尝试使用 SSH 执行一些 Cutrite 命令。这些命令在直接在目标机器上执行时可以正常工作,但不能通过代码工作。这些命令不返回任何输出或错误,通过这些输出或错误我可以找到问题的根本原因。我也尝试过运行类似的命令cd
并且pwd
工作正常。
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import net.sf.expectit.Expect;
import net.sf.expectit.ExpectBuilder;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
public class SSHTest {
public static void main(String[] args) {
String fileNameWithOutExt = "cutrite1614835697251";
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host);
session.setPassword(password);
Hashtable<String, String> config = new Hashtable<String, String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(60000);
ChannelExec channel = (ChannelExec) session.openChannel("exec");
Expect expect = new ExpectBuilder()
.withInputs(channel.getExtInputStream())
.withOutput(channel.getOutputStream())
.build();
channel.connect();
List<String> lstCmds = new ArrayList<String>();
lstCmds.add("cd C:\\V11\\Inch");
lstCmds.add("C:\\V11\\IMPORT.EXE " + fileNameWithOutExt + " /AUTO /OVERWRITE | Out-Null");
lstCmds.add("C:\\V11\\BATCH.EXE \"" + fileNameWithOutExt + ".PRL\" /AUTO /OPTIMISE | Out-Null");
lstCmds.add("C:\\V11\\OUTPUT.EXE /XLS /REPORTS=E");
for (String strCmd : lstCmds) {
expect.sendLine(strCmd);
expect.sendLine("\r");//enter character
}
expect.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}```