简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM