SSH 与 JSch 用于具有中间输出的多个命令
SSH with JSch for multiple commands with intermediate Output
我对 JSch 很陌生。我正在尝试 运行 服务器上的一些命令,这些命令是我从其他系统获取的输入。
我正在做的是获取这些命令并将它们作为参数传递给 java 方法。
例如:
public String readFileFromPath(String server, String path,
String fileName);
这里我们首先要cd到'path',然后我们需要从路径上的文件中读取一些特定的内容。
为了实现这一点,我做了以下工作:
Session session = sshOperations.getSessionWithTimeout(USER,server,SSHPORT,1000);
Channel shellChannel = sshOperations.getShellChannel(session);
InputStream in = new PipedInputStream();
PipedOutputStream consoleInput = new PipedOutputStream((PipedInputStream) in);
OutputStream out = new PipedOutputStream();
BufferedReader consoleOutput = new BufferedReader(new InputStreamReader(new PipedInputStream((PipedOutputStream) out)));
shellChannel.setInputStream(in);
shellChannel.setOutputStream(out);
shellChannel.connect(1000);
consoleInput.write(("cd "+path).getBytes());
// first While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
// execute second command
consoleInput.write("cat some.properties".getBytes());
// second While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
现在我所知道的是,每当我连接到该服务器时,我都会收到一条欢迎文字:
"You are using <serverName> server.
Please contact admin for any issues"
因此,在第一个 while 循环之后,我的 cd 命令 运行 打印了上面提到的消息。但是,在此之后它等待输出流的更多输出(此时卡住)并且输出流无法产生任何东西,直到我 运行 另一个命令。
我想以某种方式退出第一个 while 循环而不编写消耗 2 行(固定行)的逻辑。至于下一个命令,我不知道流中会输出多少行。
请建议获得所需输出的逻辑,即我 运行 一个命令,一些逻辑使用它,然后我得到 运行 另一个命令,依此类推,直到所有命令执行参数时出现。
还有其他方法可以达到同样的效果吗?
谢谢
不要使用 "shell" 频道。 "shell" 频道旨在实现交互式会话(因此欢迎消息),而不是自动执行命令。
要自动执行命令,请使用 "exec" 频道。参见 Multiple commands through JSch shell。
尽管您实际上不需要多个命令。不需要 cd
。只需在 cat
命令中使用完整路径
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("cat " + path + "/some.properties");
channel.connect();
尽管实际上,如果您想读取文件内容,请使用 SFTP,而不是 运行 控制台命令,如 cat
。 SFTP 是通过 SFTP 访问文件的标准化 API。
参见 SFTP file transfer using Java JSch。
我对 JSch 很陌生。我正在尝试 运行 服务器上的一些命令,这些命令是我从其他系统获取的输入。 我正在做的是获取这些命令并将它们作为参数传递给 java 方法。 例如:
public String readFileFromPath(String server, String path,
String fileName);
这里我们首先要cd到'path',然后我们需要从路径上的文件中读取一些特定的内容。 为了实现这一点,我做了以下工作:
Session session = sshOperations.getSessionWithTimeout(USER,server,SSHPORT,1000);
Channel shellChannel = sshOperations.getShellChannel(session);
InputStream in = new PipedInputStream();
PipedOutputStream consoleInput = new PipedOutputStream((PipedInputStream) in);
OutputStream out = new PipedOutputStream();
BufferedReader consoleOutput = new BufferedReader(new InputStreamReader(new PipedInputStream((PipedOutputStream) out)));
shellChannel.setInputStream(in);
shellChannel.setOutputStream(out);
shellChannel.connect(1000);
consoleInput.write(("cd "+path).getBytes());
// first While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
// execute second command
consoleInput.write("cat some.properties".getBytes());
// second While
while ((line = consoleOutput.readLine()) != null)
{
System.out.println("check "+ line);
}
现在我所知道的是,每当我连接到该服务器时,我都会收到一条欢迎文字:
"You are using <serverName> server.
Please contact admin for any issues"
因此,在第一个 while 循环之后,我的 cd 命令 运行 打印了上面提到的消息。但是,在此之后它等待输出流的更多输出(此时卡住)并且输出流无法产生任何东西,直到我 运行 另一个命令。
我想以某种方式退出第一个 while 循环而不编写消耗 2 行(固定行)的逻辑。至于下一个命令,我不知道流中会输出多少行。
请建议获得所需输出的逻辑,即我 运行 一个命令,一些逻辑使用它,然后我得到 运行 另一个命令,依此类推,直到所有命令执行参数时出现。
还有其他方法可以达到同样的效果吗?
谢谢
不要使用 "shell" 频道。 "shell" 频道旨在实现交互式会话(因此欢迎消息),而不是自动执行命令。
要自动执行命令,请使用 "exec" 频道。参见 Multiple commands through JSch shell。
尽管您实际上不需要多个命令。不需要 cd
。只需在 cat
命令中使用完整路径
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("cat " + path + "/some.properties");
channel.connect();
尽管实际上,如果您想读取文件内容,请使用 SFTP,而不是 运行 控制台命令,如 cat
。 SFTP 是通过 SFTP 访问文件的标准化 API。
参见 SFTP file transfer using Java JSch。