SSH is a protocol that is defined to run on top of TCP, and you can make TCP connections in java using the java.net.Socket
class.
(SSH是一种定义为在TCP之上运行的协议,您可以使用java.net.Socket
类在Java中建立TCP连接。)
So, yes, it can be done.
(因此,是的,可以做到。)
You'd have to implement the protocol yourself (or use JSch to do it, but given that all these tools already exist, and it's security, where in general relying on widely used tools is orders of magnitude more intelligent than relying on hand-written stuff, given that a security bug is very hard to test for – I assume this is some sort of exercise in sheer dogged arrogance, or, more likely, a learning exercise, which'd mean that you wouldn't want to use jsch here).
(您必须自己实现协议(或使用JSch来实现,但是鉴于所有这些工具已经存在,并且它是安全性),通常,广泛使用的工具比依赖手写的要聪明得多。的东西,因为一个安全漏洞是非常困难的测试-我想这是在纯粹的顽强傲慢某种运动,或者更可能的是,一个学习锻炼,which'd意味着你不会想在这里使用jsch )。)
ssh works by sending any number of streams across the network, encrypted.
(ssh通过在网络上发送任意数量的加密流来工作。)
By default, there is one stream (a two-way stream), with the user's shell being on one end (so, the /bin/bash
executable, for example), and a terminal (where what you type forms the input, and any output is echoed to the terminal window) on the other. (默认情况下,有一个流(双向流),用户的外壳位于一端(例如, /bin/bash
可执行文件),而一个终端(由您输入的内容构成输入),以及任何输出都将回显到另一个终端窗口上。)
ssh does NOT wait for you type a command then run it;
(ssh不会等待您键入命令然后运行它;)
that's /bin/bash
doing this. (/bin/bash
正在执行此操作。)
This is basic linux info and has no direct bearing on ssh (the protocol), that's just what you're used to, because sshd (the linux executable that forms an ssh server) by default hooks your terminal up to the target server's shell executable. (这是基本的Linux信息,与ssh(协议)没有直接关系,这就是您所习惯的,因为sshd(构成ssh服务器的linux可执行文件)默认情况下会将您的终端挂接到目标服务器的shell可执行文件上。)
Usually bash. (通常是bash。)
Given that you know nothing of this, this sounds like a project that's a bit too far fetched as a starter experiment, but I'm going by incomplete information to make this assessment.
(鉴于您对此一无所知,这听起来像一个项目,作为入门实验来说有些牵强,但是我要借助不完整的信息来进行此评估。)
I'd start by making a trivial telnet client and server.
(我将从制作一个简单的telnet客户端和服务器开始。)
telnet is at this point extremely outdated but it is basically ssh without the multiplexing of streams nor the encryption. (telnet在这一点上已经过时了,但是它基本上是ssh,没有流的多路复用或加密。)
If you can't write a telnet client, you can't write an ssh client either. (如果您不能编写telnet客户端,也不能编写ssh客户端。)
And you can still start telnet servers on linux machines for testing purposes. (而且,您仍然可以在Linux机器上启动telnet服务器以进行测试。)
Once you've got that down, start figuring out the crypto and the multiplexing*.
(一旦发现问题,就开始弄清楚加密和多路复用*。)
*) With ssh you can, whilst having a shell open, also port forward and such;
(*)使用ssh,您可以在打开外壳的同时向前端口等;)
check out the -R
and -L
and -D
options in standard ssh. (查看标准ssh中的-R
和-L
和-D
选项。)
Each such option allows for another stream to be sent along. (每个这样的选项都允许发送另一个流。)
(whilst you're sending shell commands and seeing their results, at the same time, over the same ssh connection, you are, say, sending a print job to a printer in the network of the other machine). ((同时,您正在通过相同的ssh连接发送shell命令并查看其结果,也就是说,您正在将打印作业发送到另一台计算机的网络中的打印机)。)
Sending multiple streams through a single connection is called 'multiplexing', and it is part of the ssh specification. (通过单个连接发送多个流称为“多路复用”,它是ssh规范的一部分。)