Jsch Java progam to pass arguements to a linux process

72 Views Asked by At

I have a binary executable file named as "datasimulator" that runs in a remote linux machine which starts an interactive process where a user can provide input and get its corresponding output like below -

[root@host]$ ./datasimulator 
> set country USA    //user passing this command 
Country set to USA   //response from process  
> set country Canada  
Country set to Canada  
>exit  
Thank you for using datasimulator.  
[root@host]$

I am trying to automate this process using java jsch utility where through my java program I can pass input and get the output back to my program. Though I am able to start the datasimulator through jsch ssh connection but not able to pass input to this simulator process. Kindly help. Below is the java program which I am trying to use -

JSch jsch = new JSch(); session = jsch.getSession(username, host, 22);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();

channel=session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
ps = new PrintStream(ops, true);
channel.connect();
ps.println("cd /path/To/datasimulator/ && ./datasimulator");
ps.println("Country set to USA");
Thread.sleep(5000);
ps.println("Country set to Canada");
Thread.sleep(5000);
ps.close();
channel.disconnect();
session.disconnect();

This program starts the datasimulator but not able to execute next consecutive commands successfully.

0

There are 0 best solutions below