Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide input selection in shell script when running though Java program?

In my Java program what I am trying to do is:

First, I connect to a Unix server and execute one shell script. But in that shell script, you have to select the option to perform the different operation once the option is selected.

For example:

Please select from below menu options
1. Create directory and subdirectory
2. Copy files
3. Update paths
4. Press 9 to exit

Here each option performs different operations and upon selecting any asks for further input. For ex: If I select an option 1 it will ask for the path:

Please enter the path where you want to create a directory

Now my question is: How can I enter this input while running this shell script from Java code?

Below code is written For connecting to unix server and execution of shell script:

JSch jsch = new JSch();

String command = "/tmp/myscript.sh";
Session session = jsch.getSession(user, host, 22);
session.connect();

Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);

channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
channel.connect();

byte[] tmp = new byte[1024];
while (true) {
  while (in.available() > 0) {
      int i = in.read(tmp, 0, 1024);
      if (i < 0) {
          break;
      }
      System.out.print(new String(tmp, 0, i));
  }
  if (channel.isClosed()) {
      if (channel.getExitStatus() == 0) {
          System.out.println("Command executed successully.");
      }
      break;
  }
}
channel.disconnect();
session.disconnect();
like image 421
Sh87 Avatar asked Mar 21 '26 05:03

Sh87


1 Answers

Let's say we want to "Create directory and subdirectory", then exit.
Once the channel is ready, we have to:

  1. send create command "1"
  2. send path "/path/to/directory"
  3. send exit command "9"
JSch jsch = new JSch();

String command = "/tmp/myscript.sh";
Session session = jsch.getSession(user, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(passwd);
session.connect();

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
InputStream is = channel.getInputStream();
// We will send commands through this stream
OutputStream os = channel.getOutputStream();
channel.connect();

int step = 1;
byte[] tmp = new byte[1024];
int read;
while (true) {
    // Wait for available data
    while(is.available() == 0) {
        Thread.sleep(100);
    }
    // Read the script/command output
    while(is.available() > 0) {
        read = is.read(tmp);
        if (read < 0) {
            break;
        }
        System.out.print(new String(tmp, 0, read));
    }
    // Send a command depending on current step
    switch(step) {
    case 1:
        // 1. Create directory command
        os.write("1\n".getBytes());
        os.flush();
        break;
    case 2:
        // 2. Path to create
        os.write("/path/to/directory\n".getBytes());
        os.flush();
        break;
    case 3:
        // 3. Exit command
        os.write("9\n".getBytes());
        os.flush();
        break;
    }
    step++;
    if (channel.isClosed()) {
        if (channel.getExitStatus() == 0) {
            System.out.println("Command executed successully.");
        }
        break;
    }
}
channel.disconnect();
session.disconnect();

The final "\n" and a call to os.flush are mandatory for each command.

like image 135
Tigger Avatar answered Mar 22 '26 20:03

Tigger