I am trying to execute some Cutrite commands using SSH. These commands work fine when executed directly on the target machine but do not work through the code. The commands do not return any output or error through which I could find the root cause of the issue. I have also tried running commands like cd
and pwd
which work fine.
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();
}
}
}```