使用纯 Java 与 SSH 服务器通信以进行文件访问

发布于 2024-12-02 15:04:35 字数 383 浏览 0 评论 0原文

我正在用 Java 编写一个程序,使用套接字与 Telnet 服务器进行通信,该服务器允许用户访问 UNIX 操作系统中的文件目录。

当使用 Putty 与该服务器通信时,它会提示我输入用户名和密码,但使用我的套接字时,除了表明它使用 SSH 2.0 的字符串之外,服务器没有任何信息 - 我认为。

我确信这与 Telnet 协议有关,但是如何让服务器询问我的用户名和密码。为了访问 UNIX 环境中的文件目录,我需要向服务器提供哪组命令?

更正: 我认为它实际上在端口 22 上使用 SSH。可以使用 Putty 或 Microsoft Windows 的 Telnet 程序访问它,但它实际上并不使用 Telnet 协议,而是使用 SSH 协议。

I'm writing a program in Java using the sockets to communicate with a Telnet server which allows the users to access the file directory in an UNIX OS.

When using Putty to communicate with this server, it prompts me for my username and password, but using my sockets there is nothing from the server except for a string which states that it uses SSH 2.0 - I think.

I'm sure that this has to do with the Telnet protocol, but how do I get the server to ask me for my username and password. What set of commands would I need to give the server in order to access the file directory in an UNIX environment?

Correction:
I figured that it's actually using SSH on port 22. It can be accessed using Putty or Microsoft Windows' Telnet program, but it doesn't actually use the Telnet protocol but the SSH protocol.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烦人精 2024-12-09 15:04:35

SSH 不是远程登录。 SSH 是一种很像 telnet 的协议,但它是加密的并且具有许多其他功能。所以看起来你期待的是纯文本交换,但你得到的是 ssh 协议试图进行握手。

Telnet 在端口 23 上运行,SSH 在 22 上运行。我想您想使用 23。注意:Telnet 很旧且未加密,在互联网上使用很危险(除非您通过 VPN 或加密会话的东西)。

SSH isn't telnet. SSH is a protocol that is a lot like telnet, but is encrypted and has a slew of other features. So it looks like youre expecting a plain-text exchange, but what you're getting is the ssh protocol trying to do a handshake.

Telnet runs on port 23, SSH on 22. I imagine you want to use 23. Note: Telnet is old and unencrypetd and dangerous to use over the internet (unless youre going over a VPN or something that encrypts the session).

笑叹一世浮沉 2024-12-09 15:04:35

对于大多数用途来说,Telnet 协议确实没有什么用处...有关详细信息,请参阅此页面。如果另一端的服务器正在尝试协商 SSL 连接(这是目前最有可能发生的情况),请尝试使用 java.net.ssl.SSLSocket 而不是裸露的 TCP 套接字。

一旦您协商了连接(请参阅上面链接的文档),您实际上应该将 UNIX CLI 命令打印到套接字并读取(并解析)结果。如果您只想访问文件,也许可以使用 FTP。大多数现代服务器将支持SFTP

编辑

经过一番探索,我发现直接使用 SSLSocket 连接到 SSH 服务器非常麻烦,因为 SSH 有自己的协议。您可能不想重新发明轮子。查看这个问题的答案,了解一些纯 Java SSH 客户端库。您可能至少可以使用其中之一来比通过 SSH 发送文本命令更直接地解决您的问题。

There is really nothing to the Telnet protocol for most uses...see this page for details. If the server on the other end is trying to negotiate a SSL connection, which is by far the most likely thing these days, try using a java.net.ssl.SSLSocket instead of a bare TCP socket.

Once you negotiate the connection (see the docs linked above) you should essentially print UNIX CLI commands to the socket and read (& parse) the results. If you just want to access files, maybe use FTP instead. Most modern servers are going to support SFTP.

Edit

With a little poking I found that using SSLSocket directly to connect to a SSH server is cumbersome at best because SSH has its own protocol. You probably don't want to reinvent the wheel on that one. Check out the answers to this question for some pure Java SSH client libraries. You can probably use at least one of these to solve your problem more directly than sending text commands over SSH.

猫腻 2024-12-09 15:04:35

如果它是一个 telnet 服务器,那就很简单了。

但您拥有的是 ssh 服务器,这已经很好了。 telnet 已被严重弃用,因为它未加密。

您现在有两个选择:使用 ssh 库或通过其 stdio 访问 ssh 命令行客户端(或在 Windows 下:plink 程序)。

Would it be a telnet server, it would be simple.

But what you have is a ssh server, and that is good as it is. telnet is heavily deprecated, as it is not encrypted.

You now have two options: Either use a ssh library or access the ssh command line client (or under Windows: the plink program) via its stdio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文