简体   繁体   English

带有JSch的SSH用于带有中间输出的多个命令

[英]SSH with JSch for multiple commands with intermediate Output

I am very new to JSch. 我对JSch很陌生。 I am trying to run few commands on a server which I am getting as input from some other system. 我试图在服务器上运行一些命令,这些命令是我从其他系统获得的输入。 What I am doing is taking those commands and passing them as a parameter to a java method. 我正在做的就是接受这些命令,并将它们作为参数传递给java方法。 For Eg: 例如:

public String readFileFromPath(String server, String path,
            String fileName);

Here first we have to cd to 'path', then we need to read some particular content from the file present on the path. 在这里,首先我们必须cd到'path',然后我们需要从路径中存在的文件中读取一些特定内容。 To implement this I did following : 为了实现这一点,我做了以下工作:

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);
}

Now what I know is whenever I connect to that server I get a welcome text : 现在我知道的是,每当我连接到该服务器时,我都会收到欢迎文本:

"You are using <serverName> server. Please contact admin for any issues" So, after the first while loop my cd command ran and it prints the message mentioned above. "You are using <serverName> server. Please contact admin for any issues""You are using <serverName> server. Please contact admin for any issues"因此,在第一个while循环后,我的cd命令运行了,并打印了上述消息。 But, after this it waits for more output from the output stream (it is stuck at this point )and the output stream can't product anything until I run another command. 但是,在此之后,它等待输出流中的更多输出(此时卡住了),直到我运行另一个命令,输出流才能产生任何结果。

Somehow I want to exit from the first while loop without writing the logic for consuming the 2 lines(fixed lines). 我不知何故想从第一个while循环中退出,而无需编写消耗2行(固定行)的逻辑。 As for the next command I will not know how many lines will come as output in stream. 至于下一个命令,我将不知道流中将输出多少行。

Please suggest the logic to the get the desired output ie I ran a command and some logic consumes it and then I get get to run another command and so on until all the commands which came as parameter are executed. 请建议逻辑以获取所需的输出,即我运行了一个命令,某些逻辑消耗了它,然后我开始运行另一个命令,依此类推,直到所有作为参数出现的命令都被执行为止。

Is there any other way to achive the same? 还有其他方法可以达到同样的效果吗?

Thanks 谢谢

Do not use "shell" channel. 不要使用“外壳”通道。 The "shell" channel is intended to implement an interactive session (hence the welcome message), not to automate command execution. “ shell”通道旨在实现交互式会话(因此出现欢迎消息),而不是自动执行命令。

To automate command execution, use "exec" channel. 要自动执行命令,请使用“ exec”通道。 See Multiple commands through JSch shell . 请参阅通过JSch shell的多个命令

Though you actually do not need multiple commands. 虽然您实际上不需要多个命令。 There's no need for cd . 不需要cd Just use a full path in the cat command 只需在cat命令中使用完整路径

ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("cat " + path + "/some.properties");
channel.connect();

Though actually, if you want to read contents of files, use SFTP, instead of running console commands like cat . 尽管实际上,如果您想读取文件的内容,请使用SFTP,而不要运行诸如cat类的控制台命令。 SFTP is a standardized API to access files over SFTP. SFTP是用于通过SFTP访问文件的标准化API。

See SFTP file transfer using Java JSch . 请参阅使用Java JSch进行SFTP文件传输

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM