英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论