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

倖福魔咒の 提交于 2021-02-20 03:50:46

问题


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:


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:
What is the difference between the 'shell' channel and the 'exec' channel in JSch



来源:https://stackoverflow.com/questions/64165946/connecting-to-server-via-ssh-using-java-jsch-and-issuing-shell-commands

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