与 Java 的 SSH 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3071760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SSH connection with Java
提问by user348041
How can I connect to an SSH server in Java? I don't need/want a shell. I just want to connect to the SSH server and get the content of, say, file.txt
. How can I do that?
如何使用 Java 连接到 SSH 服务器?我不需要/想要一个外壳。我只想连接到 SSH 服务器并获取file.txt
. 我怎样才能做到这一点?
回答by Michael Mrozek
回答by Petar Minchev
回答by Thorbj?rn Ravn Andersen
http://www.ganymed.ethz.ch/ssh2/implements a ssh2 client in Java. I use it for port forwarding.
http://www.ganymed.ethz.ch/ssh2/在 Java 中实现了 ssh2 客户端。我用它来进行端口转发。
回答by World
Use JSch
使用JSch
import com.jcraft.jsch.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* @author World
*/
public class SSHReadFile {
public static void main(String args[]) {
String user = "john";
String password = "mypassword";
String host = "192.168.100.23";
int port = 22;
String remoteFile = "/home/john/test.txt";
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
System.out.println("SFTP Channel created.");
InputStream inputStream = sftpChannel.get(remoteFile);
try (Scanner scanner = new Scanner(new InputStreamReader(inputStream))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
}
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
}
output:
输出:
Establishing Connection...
Connection established.
Crating SFTP Channel.
SFTP Channel created.
This is content from file /home/john/test.txt
回答by Zyad
I used this and worked for me
我使用了这个并为我工作
Channel channel=session.openChannel("exec");
String command = "Your Command here";
((ChannelExec)channel).setCommand(command);
InputStream in=channel.getInputStream();
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
回答by Taurean Wooley
Not 100% sure but I believe that the sockets have a great deal to do with ssh connections unless you are attempting to do a pre-installed command manipulation application. There are numerous outside packages that you can use to achieve this goal, but if you want to do your own socket creation, it's going to take a large amount of reading and deciphering/encrypting to get this done.
不是 100% 肯定,但我相信套接字与 ssh 连接有很大关系,除非您尝试执行预安装的命令操作应用程序。您可以使用许多外部软件包来实现此目标,但是如果您想创建自己的套接字,则需要大量阅读和解密/加密才能完成此任务。
Also creating your own socket is dangerous if not done correctly with the right protocols and security specifications, so be wary about doing so until you are 100% sure you're capable of committing to learning enough.
如果没有使用正确的协议和安全规范正确地创建自己的套接字也是危险的,所以在你 100% 确定你有能力进行足够的学习之前要小心这样做。