问题描述
我正在尝试使用 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();
}
}
}```
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)