连接到服务器并使用Java – JSch执行SSH,然后发出shell命令。

huangapple go评论78阅读模式
英文:

Connecting to server via SSH using Java - JSch and issuing shell commands

问题

我正在尝试编写一个程序,使用Java通过SSH连接到远程Linux服务器。

我在网上找到了许多使用JSch包来实现这一目标的示例,但它们大多是在身份验证后立即向服务器发出单个命令的情况。

然而,是否有办法首先对SSH服务器进行身份验证,然后通过连接从用户输入发出类似于通过远程Shell执行命令的操作呢?

英文:

I'm trying to write a program that connects via SSH to a remote Linux server using Java.

I've found a number of examples online to do this using the JSch package, but they're mostly for issuing a single command to the server right after authentication.

Is there any way, however, to first authenticate with the SSH server, then, over the connection, issue commands from the user input like you would via a remote shell?

答案1

得分: 1

如果您想实现一个交互式shell,您需要使用SSH的“shell”通道。

JSch提供了一个简单的示例,展示了如何实现这一点:
http://www.jcraft.com/jsch/examples/Shell.java.html

但请注意,完全实现一个交互式shell是一项庞大的工作。因此,您可能会发现上述的简单实现无法如您所希望的那样工作。

有关背景信息,还可以参考:
https://stackoverflow.com/q/6770206/850848#56713547

英文:

If you want to implement an interactive shell, you need to use SSH "shell" channel.

JSch has a simple example showing how to do that:
http://www.jcraft.com/jsch/examples/Shell.java.html

Though note that fully implementing an interactive shell is an immense work. So you will likely find out that a simple implementation like above will not work as you might hope.

For some background, see also:
https://stackoverflow.com/q/6770206/850848#56713547

huangapple
  • 本文由 发表于 2020年10月2日 11:46:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64165946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定