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

痞子三分冷 提交于 2019-12-09 16:51:58

问题


Can someone tell me the differences betweenChannelExec & ChannelShell?


回答1:


The shell and exec channels are 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.

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 shell on 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 script on 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.)

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 setInputStream method, or by getting a Java OutputStream from the getOutputStream method and writing to it.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/6265278/whats-the-exact-differences-between-jsch-channelexec-and-channelshell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!