使用 jsch session 和 channelexec 执行多个命令
execute multiple command with jsch session and channelexec
我努力让它工作,但最终让脚本在远程 unix 服务器上执行命令(执行 sh 脚本)。我正在尝试执行第二个命令,但在创建新频道或使用新频道时一直出现错误。
try {
((ChannelExec) channel).setCommand(command);
PrintStream out= new PrintStream(channel.getOutputStream());
InputStream in = channel.getInputStream();
channel.connect();
BufferedReader scriptReader= new BufferedReader(new InputStreamReader(in));
scriptOutput = scriptReader.readLine();
sb = new StringBuilder();
while ((scriptOutput = scriptReader.readLine())!= null) {
sb.append(scriptOutput + "\n");
这是频道执行的第一个片段,效果很好。现在在使用上面的输入流后立即调用下一个方法片段:
try {
StringBuilder sb = new StringBuilder();
command = new_command;
((ChannelExec) channel).setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
BufferedReader scriptReader= new BufferedReader(new InputStreamReader(in));
scriptOutput = scriptReader.readLine();
//StringBuilder sb = new StringBuilder();
for(int c=0; c < consumerList.size(); c++){
....
现在这个returns下面的错误:
com.jcraft.jsch.JSchException: channel is not opened.
现在,如果我使用相同的会话创建一个新频道,我会从返回的流中得到一个空响应。我确实在远程 shell 中测试了命令,它工作正常:
int counter = 0;
Channel channelII = session.openChannel("exec");
try {
StringBuilder sb = new StringBuilder();
command = new_command;
((ChannelExec) channelII).setCommand(command);
InputStream in = channelII.getInputStream();
channelII.connect();
BufferedReader scriptReader= new BufferedReader(
new InputStreamReader(in));
scriptOutput = scriptReader.readLine();
第二个命令如下,我希望能够针对不同的消费者群体重复执行它:
/usr/kafka/bin/kafka-consumer-groups.sh --bootstrap-server
192.xxx.xx.xxx:9092 --describe -group consumergroup1
编辑:
第二个命令的响应:
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET
LAG CONSUMER-ID HOST
CLIENT-ID
output.influxDB 1 94919 2781796
2686877 - -
-
output.influxDB 0 94919 2781798
2686879 - -
-
output.influxDB 2 94918 2781795
2686877 - -
-
感谢 Nicholas 和 Martin 的回复。我弄清楚出了什么问题并想 post 一个答案,因为我确实意识到像这样的小事情确实会突然出现在我们 'dumb' 程序员面前,他们会提出一些荒谬的问题并招致反对票。第二个命令的输出在第一行中返回警告/错误响应,并且由于不包括以下内容,我没有看到它并且读取下一行是空的。我知道这很愚蠢,应该在 posting 之前解决这个问题,因为这就是本网站的重点:post 其他人不了解的问题。但是因为我天生就应该知道这一点:
无论如何都要确保包含以下行:
((ChannelExec)channelII).setErrStream(System.err);
并且还通过循环读取流,而不仅仅是通过读取第一行进行测试。
while ((scriptOutput = scriptReader.readLine())!= null) {
sb.append(scriptOutput + "\n");
}
我希望这即使不是解决方案,至少也能给一些人一个教训。
我努力让它工作,但最终让脚本在远程 unix 服务器上执行命令(执行 sh 脚本)。我正在尝试执行第二个命令,但在创建新频道或使用新频道时一直出现错误。
try {
((ChannelExec) channel).setCommand(command);
PrintStream out= new PrintStream(channel.getOutputStream());
InputStream in = channel.getInputStream();
channel.connect();
BufferedReader scriptReader= new BufferedReader(new InputStreamReader(in));
scriptOutput = scriptReader.readLine();
sb = new StringBuilder();
while ((scriptOutput = scriptReader.readLine())!= null) {
sb.append(scriptOutput + "\n");
这是频道执行的第一个片段,效果很好。现在在使用上面的输入流后立即调用下一个方法片段:
try {
StringBuilder sb = new StringBuilder();
command = new_command;
((ChannelExec) channel).setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
BufferedReader scriptReader= new BufferedReader(new InputStreamReader(in));
scriptOutput = scriptReader.readLine();
//StringBuilder sb = new StringBuilder();
for(int c=0; c < consumerList.size(); c++){
....
现在这个returns下面的错误:
com.jcraft.jsch.JSchException: channel is not opened.
现在,如果我使用相同的会话创建一个新频道,我会从返回的流中得到一个空响应。我确实在远程 shell 中测试了命令,它工作正常:
int counter = 0;
Channel channelII = session.openChannel("exec");
try {
StringBuilder sb = new StringBuilder();
command = new_command;
((ChannelExec) channelII).setCommand(command);
InputStream in = channelII.getInputStream();
channelII.connect();
BufferedReader scriptReader= new BufferedReader(
new InputStreamReader(in));
scriptOutput = scriptReader.readLine();
第二个命令如下,我希望能够针对不同的消费者群体重复执行它:
/usr/kafka/bin/kafka-consumer-groups.sh --bootstrap-server
192.xxx.xx.xxx:9092 --describe -group consumergroup1
编辑:
第二个命令的响应:
TOPIC PARTITION CURRENT-OFFSET LOG-END-OFFSET
LAG CONSUMER-ID HOST
CLIENT-ID
output.influxDB 1 94919 2781796
2686877 - -
-
output.influxDB 0 94919 2781798
2686879 - -
-
output.influxDB 2 94918 2781795
2686877 - -
-
感谢 Nicholas 和 Martin 的回复。我弄清楚出了什么问题并想 post 一个答案,因为我确实意识到像这样的小事情确实会突然出现在我们 'dumb' 程序员面前,他们会提出一些荒谬的问题并招致反对票。第二个命令的输出在第一行中返回警告/错误响应,并且由于不包括以下内容,我没有看到它并且读取下一行是空的。我知道这很愚蠢,应该在 posting 之前解决这个问题,因为这就是本网站的重点:post 其他人不了解的问题。但是因为我天生就应该知道这一点:
无论如何都要确保包含以下行:
((ChannelExec)channelII).setErrStream(System.err);
并且还通过循环读取流,而不仅仅是通过读取第一行进行测试。
while ((scriptOutput = scriptReader.readLine())!= null) {
sb.append(scriptOutput + "\n");
}
我希望这即使不是解决方案,至少也能给一些人一个教训。