你究竟是什么意思“telnet进入我的应用程序,他们可以输入命令等等”?
由于telnet和ssh都需要在远程计算机上运行某些应用程序(通常是命令shell的实例),因此它们实际上只提供命令的传输机制。特别是Telnet可以(并且已被)滥用以通过tcp连接发送通用文本命令。您可以使用命令telnet www.target-domain.com 80浏览网页并手动输入所有http协议内容,但我不推荐它。 ssh是一样的,虽然它为通道添加了ssl / tls安全性。
因此,我想你想要的是这样的:
import java.io.*;
import java.net.*;
public class telnettest {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
//Open a listening socket on port 8022 and wait for a connection
echoSocket = new ServerSocket(8022).accept();
System.out.println("connection established");
//Get input and output streams
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection");
System.exit(1);
}
//Read lines from the input stream (corresponding to user commands)
String userInput;
while ((userInput = in.readLine()) != null) {
//For each line entered, just output it to both remote and local terminal
out.println("echo: " + userInput);
System.out.println("echo: " + userInput);
}
//Clean up connections.
out.close();
in.close();
echoSocket.close();
}
}
我不确定ssh登录,但我怀疑你可以使用javax.net.ssl.SSLServerSocket而不是ServerSocket来到达那里。
剩下的就是用用户输入做一些合理的事情,而不是仅仅把它扔回到他们的脸上。
取决于命令及其参数的数量,您可以自己执行命令解析,也可以找到一个为您处理命令的库。