问题
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