【发布时间】:2021-08-10 18:39:54
【问题描述】:
在我的 Java 程序中,我想做的是:
首先,我连接到 Unix 服务器并执行一个 shell 脚本。但是在那个 shell 脚本中,您必须选择该选项才能在选择该选项后执行不同的操作。
例如:
Please select from below menu options
1. Create directory and subdirectory
2. Copy files
3. Update paths
4. Press 9 to exit
这里每个选项执行不同的操作,并且在选择任何要求进一步输入时。 例如:如果我选择选项 1,它将询问路径:
Please enter the path where you want to create a directory
现在我的问题是:如何在从 Java 代码运行这个 shell 脚本时输入这个输入?
下面的代码用于连接unix服务器和执行shell脚本:
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();
【问题讨论】:
-
嗨,有人可以帮忙吗?
-
你想如何获取输入?图形用户界面?你的程序有什么定制的吗?标准输入(你的程序)?最后一种情况,你可以试试
channel.setInputStream(System.in);。 -
您为什么要读取频道的输入流而不是写入?
-
顺便说一句,如果不是依赖于
myscript.sh没有人,但你有,那会更好 minimal reproducible example,你执行类似String[]{"bash", "-c", "read -p 'need input: ' input; echo \"got input: $input\""}的东西——这样你的代码就不需要依赖它不包含。 -
可能你不能,使用shell select创建一个交互式shell并保存stdin,你仍然试图用java粘回来,为什么?哈哈
标签: java shell unix automation