第二次调用时 JSch 不提供输出
JSch doesn't provide output when called a 2nd time
我正在使用 JSch 通过 SSH 连接到服务器并使用扫描命令在那里传输文件。
代码已经过测试并且运行良好。我从命令行获得了所有输出。然而 - 当我再次使用相同的方法时,一切仍然 "works" 但我没有看到任何输出。我需要输出以查看是否一切正常。
在主应用程序中,按下按钮时会发生这种情况:
Scan s = new Scan(getHost(),getUser(),getPassword());
s.scan("scanscript \"" + getScancommand() + "\" \"" + getFilename() + "\"");
scanscript
需要两个参数:一个是整个命令 scanimage blah blah
- 另一个是所需的文件名。
Scanscript 还 returns 一个必需的退出代码 - 但并非总能获得。
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSchException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
public class Scan {
private final String scan_host;
private final String scan_user;
private final String password;
public Scan(String host, String user, String password) {
this.scan_host = host;
this.scan_user = user;
this.password = password;
}
public void scan(String command) {
boolean b_success = true;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(scan_user, scan_host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
Logger.getLogger(Scan.class.getName()).log(Level.INFO, "Command: {0}", command);
System.out.println("Connected"); //This here is displayed always
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
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) {
b_success = false;
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.ERROR, "Scan failed\n - Exit-Status: " + channel.getExitStatus(), ButtonType.OK);
alert.showAndWait();
}
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ee) {
Logger.getLogger(Scan.class.getName()).log(Level.SEVERE, null, ee);
}
}
channel.disconnect();
session.disconnect();
if (b_success) {
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Scan successful", ButtonType.OK);
alert.showAndWait();
}
} catch (JSchException | IOException e) {
Logger.getLogger(Scan.class.getName()).log(Level.SEVERE, null, e);
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.ERROR, "Scan failed: " + e.getLocalizedMessage(), ButtonType.OK);
alert.showAndWait();
}
}
}
这是我的控制台输出:
Jun 15, 2017 9:12:03 PM com.schwaiger.kanva.scan.Scan scan
INFORMATION: Befehl: scanscript "scanimage --device='brother4:net1;dev0' --format tiff --resolution=150 --source 'Automatic Document Feeder(left aligned,Duplex)' -l 0mm -t 0mm -x210mm -y297mm --batch=$(date +%Y%m%d_%H%M%S)_p%04d.tiff" "testscan1.pdf"
Connected
scanimage: rounded value of br-x from 210 to 209.981
scanimage: rounded value of br-y from 297 to 296.973
Scanning -1 pages, incrementing by 1, numbering from 1
Scanning page 1
Scanned page 1. (scanner status = 5)
Scanning page 2
Scanned page 2. (scanner status = 5)
Scanning page 3
Scanned page 3. (scanner status = 5)
Scanning page 4
Scanned page 4. (scanner status = 5)
Scanning page 5
scanimage: sane_start: Document feeder out of documents
exit-status: 0
Connected
exit-status: 0
如您所见,在我第一次调用命令后,我获得了有关正在发生的事情的全部信息。第二次我刚得到 Connected
和退出状态。但我无法确定该状态是指我的脚本还是整个操作。
您的命令在错误流上提供所有输出。
您将通道错误流通过管道传输到 Java 控制台应用程序的错误输出:
((ChannelExec) channel).setErrStream(System.err);
当第一个命令执行的通道关闭时,它会记录控制台错误输出。
所以下一次,错误输出已经关闭,任何写入它的尝试都会被默默地忽略。
您必须以相同的方式读取错误流,您正在读取正常输出(while (true)
循环)。
我的答案中显示了一种方法,以查看我对 How to read JSch command output?
的回答
其他方式:Log stdout and stderr from ssh command in the same order it was created.
我正在使用 JSch 通过 SSH 连接到服务器并使用扫描命令在那里传输文件。
代码已经过测试并且运行良好。我从命令行获得了所有输出。然而 - 当我再次使用相同的方法时,一切仍然 "works" 但我没有看到任何输出。我需要输出以查看是否一切正常。
在主应用程序中,按下按钮时会发生这种情况:
Scan s = new Scan(getHost(),getUser(),getPassword());
s.scan("scanscript \"" + getScancommand() + "\" \"" + getFilename() + "\"");
scanscript
需要两个参数:一个是整个命令 scanimage blah blah
- 另一个是所需的文件名。
Scanscript 还 returns 一个必需的退出代码 - 但并非总能获得。
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSchException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
public class Scan {
private final String scan_host;
private final String scan_user;
private final String password;
public Scan(String host, String user, String password) {
this.scan_host = host;
this.scan_user = user;
this.password = password;
}
public void scan(String command) {
boolean b_success = true;
try {
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(scan_user, scan_host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
Logger.getLogger(Scan.class.getName()).log(Level.INFO, "Command: {0}", command);
System.out.println("Connected"); //This here is displayed always
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
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) {
b_success = false;
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.ERROR, "Scan failed\n - Exit-Status: " + channel.getExitStatus(), ButtonType.OK);
alert.showAndWait();
}
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException ee) {
Logger.getLogger(Scan.class.getName()).log(Level.SEVERE, null, ee);
}
}
channel.disconnect();
session.disconnect();
if (b_success) {
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Scan successful", ButtonType.OK);
alert.showAndWait();
}
} catch (JSchException | IOException e) {
Logger.getLogger(Scan.class.getName()).log(Level.SEVERE, null, e);
javafx.scene.control.Alert alert = new Alert(Alert.AlertType.ERROR, "Scan failed: " + e.getLocalizedMessage(), ButtonType.OK);
alert.showAndWait();
}
}
}
这是我的控制台输出:
Jun 15, 2017 9:12:03 PM com.schwaiger.kanva.scan.Scan scan
INFORMATION: Befehl: scanscript "scanimage --device='brother4:net1;dev0' --format tiff --resolution=150 --source 'Automatic Document Feeder(left aligned,Duplex)' -l 0mm -t 0mm -x210mm -y297mm --batch=$(date +%Y%m%d_%H%M%S)_p%04d.tiff" "testscan1.pdf"
Connected
scanimage: rounded value of br-x from 210 to 209.981
scanimage: rounded value of br-y from 297 to 296.973
Scanning -1 pages, incrementing by 1, numbering from 1
Scanning page 1
Scanned page 1. (scanner status = 5)
Scanning page 2
Scanned page 2. (scanner status = 5)
Scanning page 3
Scanned page 3. (scanner status = 5)
Scanning page 4
Scanned page 4. (scanner status = 5)
Scanning page 5
scanimage: sane_start: Document feeder out of documents
exit-status: 0
Connected
exit-status: 0
如您所见,在我第一次调用命令后,我获得了有关正在发生的事情的全部信息。第二次我刚得到 Connected
和退出状态。但我无法确定该状态是指我的脚本还是整个操作。
您的命令在错误流上提供所有输出。
您将通道错误流通过管道传输到 Java 控制台应用程序的错误输出:
((ChannelExec) channel).setErrStream(System.err);
当第一个命令执行的通道关闭时,它会记录控制台错误输出。
所以下一次,错误输出已经关闭,任何写入它的尝试都会被默默地忽略。
您必须以相同的方式读取错误流,您正在读取正常输出(while (true)
循环)。
我的答案中显示了一种方法,以查看我对 How to read JSch command output?
的回答其他方式:Log stdout and stderr from ssh command in the same order it was created.