如果我們談論TELNET作爲一種協議,您應該知道來自Netty示例的Telnet客戶端不支持TELNET協議。他的名字很混亂,你不能連接到任何標準的telnet服務器。你可以在這裏閱讀更多關於TELNET協議 - THE TELNET PROTOCOL。
我看到2種方式:
- 寫的Netty您的TELNET實現中使用另一個
- 實施例子阿帕奇百科全書網
例爲第一種方式 - modified netty client,我測試了他在Linux服務器上。他有幾個骯髒的黑客像計時器,但他的工作。
例爲第二 - 爪哇 - Writing An Automated Telnet Client:
import org.apache.commons.net.telnet.*;
import java.io.InputStream;
import java.io.PrintStream;
public class AutomatedTelnetClient {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "~>";
public AutomatedTelnetClient(String server) {
try {
// Connect to the specified server
telnet.connect(server, 8023);
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);
try {
telnet.addOptionHandler(ttopt);
telnet.addOptionHandler(echoopt);
telnet.addOptionHandler(gaopt);
} catch (InvalidTelnetOptionException e) {
System.err.println("Error registering option handlers: " + e.getMessage());
}
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
// public void su(String password) {
// try {
// write(「su」);
// readUntil(「Password: 「);
// write(password);
// prompt = 「#」;
// readUntil(prompt + 」 「);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
boolean found = false;
char ch = (char) in.read();
while (true) {
System.out.print(ch);
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void write(String value) {
try {
out.println(value);
out.flush();
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
}
}
public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + " ");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void disconnect() {
try {
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String user = "test";
String password = "test";
AutomatedTelnetClient telnet = new AutomatedTelnetClient("localhost");
// Log the user on
telnet.readUntil("login:");
telnet.write(user);
telnet.readUntil("Password:");
telnet.write(password);
// Advance to a prompt
telnet.readUntil(telnet.prompt + " ");
telnet.sendCommand("ps -ef");
telnet.sendCommand("ls");
telnet.sendCommand("w");
telnet.disconnect();
}
}
首先,謝謝您的幫助!我試過了。它不起作用。沒有來自服務器和頻道的響應在幾分鐘後未註冊 –
您在哪裏放置了代碼?它應該在'Channel ch = lastWriteFuture.channel();'和'lastWriteFuture = ch.writeAndFlush(「ls」+「\ r \ n」,ch.newPromise());' – Ferrybig
年。我對你的描述也一樣。在我的程序中,處理程序和telnetClientInitializer中沒有任何更改。他們和樣本一樣。 –