java jsch ChannelExec 和 ChannelShell 之间的确切区别是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6265278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:06:11  来源:igfitidea点击:

what's the exact differences between jsch ChannelExec and ChannelShell?

javasshjsch

提问by Han Zheng

Can someone tell me the differences betweenChannelExec& ChannelShell?

有人能告诉我ChannelExec&之间的区别ChannelShell吗?

回答by Pa?lo Ebermann

The shell and exec channelsare quite similar - both execute commands with the remote shell (at least, conceptually - the server might be configured to treat them differently, of course). RFC 4254 groups them in the section "Interactive Sessions", and they both (as well as subsystem, see below) use the channel type "session" in the SSH protocol.

shell 和 exec 通道非常相似——两者都使用远程 shell 执行命令(至少在概念上——当然,服务器可能被配置为以不同的方式对待它们)。RFC 4254 将它们分组在“交互式会话”部分,它们(以及子系统,见下文)都使用 SSH 协议中的通道类型“会话”。

There is one important difference:

有一个重要的区别:

  • For ChannelShell, the input stream provides both the commands and input to these commands. This is like using an interactive shellon your local computer. (And it is normally used just for that: interactive use.)

  • For ChannelExec, the commands are given with setCommand() before connect(), and the input stream will be forwarded to these commands as input. (Most often, you will have only one command, but you can provide multiple ones using the normal shell separators &, &&, |, ||, ;, newline, and compound commands.) This is like executing a shell scripton your local computer. (Of course, if one of the commands itself is an interactive shell, this will behave like a ChannelShell.)

  • There is a third similar one, ChannelSubsystem, which executes a subsystem of the ssh server - here the server's configuration decides what to do, not the remote user's shell. (The most often used subsystem is sftp, but for this JSch provides a specialized channel, which understands the protocol.)

  • 对于ChannelShell,输入流提供命令和这些命令的输入。这就像在本地计算机上使用交互式 shell。(它通常仅用于:交互式使用。)

  • 对于ChannelExec,命令在 setCommand() 之前给出connect(),输入流将作为输入转发给这些命令。(大多数情况下,您只有一个命令,但您可以使用普通的 shell 分隔符&&&|||;、换行符和复合命令提供多个命令。)这就像在本地计算机上执行 shell 脚本一样。(当然,如果其中一个命令本身是一个交互式 shell,它的行为就像一个ChannelShell.)

  • 还有第三个类似的,ChannelSubsystem,它执行 ssh 服务器的一个子系统——这里是服务器的配置决定要做什么,而不是远程用户的 shell。(最常用的子系统是sftp,但为此 JSch 提供了一个专门的通道,它理解协议。)

Note that what I call "input stream" here is the stream of data in the channel from the local to the remote host – that can be actually be done by passing a Java InputStream to the setInputStreammethod, or by getting a Java OutputStream from the getOutputStreammethod and writing to it.

请注意,我在这里所说的“输入流”是通道中从本地主机到远程主机的数据流——这实际上可以通过将 Java InputStream 传递给该setInputStream方法或通过从该getOutputStream方法获取 Java OutputStream 来完成并写信给它。

回答by Jamie

There is still one more important difference between exec channel and shell channel: The shell channel will establish the shell environment, for example, the environment variables, while the exec channel won't.

exec 通道和shell 通道还有一个更重要的区别:shell 通道会建立shell 环境,例如环境变量,而exec 通道不会。

回答by Laxmi Baghel

Exec channel will only supports Kch commands, for example, ls -l. If you will try to run any shell job, it will throw an error such as ksh: run_pass: not found

Exec 通道将只支持 Kch 命令,例如,ls -l. 如果您尝试运行任何 shell 作业,它会抛出一个错误,例如ksh: run_pass: not found