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();
Let's say we want to "Create directory and subdirectory", then exit.
Once the channel is ready, we have to:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With