telnet 1.0.4
telnet: ^1.0.4 copied to clipboard
Telnet client implemented in dart language. Supports text transmission and negotiate options.
example/telnet_example.dart
import 'package:telnet/telnet.dart';
const host = "127.0.0.1";
const port = 23;
const username = "root";
const password = "admin";
var hasLogin = false;
const echoEnabled = true;
void main() async {
// Create a Telnet connection task.
final task = TelnetClient.startConnect(
host: host,
port: port,
onError: onError,
onDone: onDone,
onEvent: onEvent,
);
// Cancel the connection task.
// task.cancel();
// Wait the connection task finished.
await task.waitDone();
// Get the `TelnetClient` instance. It will be `null` if connect failed.
final client = task.client;
if (client == null) {
print("Fail to connect to $host:$port");
} else {
print("Successfully connect to $host:$port");
}
await Future.delayed(const Duration(seconds: 15));
// Close the Telnet connection.
await client?.terminate();
}
void onError(TelnetClient? client, dynamic error) {
print("[ERROR] $error");
}
void onDone(TelnetClient? client) {
print("[DONE]");
}
void onEvent(TelnetClient? client, TLMsgEvent event) {
if (event.type == TLMsgEventType.write) {
print("[WRITE] ${event.msg}");
} else if (event.type == TLMsgEventType.read) {
print("[READ] ${event.msg}");
if (event.msg is TLOptMsg) {
final cmd = (event.msg as TLOptMsg).cmd; // Telnet Negotiation Command.
final opt = (event.msg as TLOptMsg).opt; // Telnet Negotiation Option.
if (cmd == TLCmd.wont) {
client?.write(TLOptMsg(TLCmd.doNot, opt)); // Write [IAC DO opt].
} else if (cmd == TLCmd.doNot) {
client?.write(TLOptMsg(TLCmd.wont, opt)); // Write [IAC WON'T opt].
} else {
switch(opt.code) {
case 0x01: { // ECHO
if (cmd == TLCmd.doIt) {
if (echoEnabled) {
client?.write(TLOptMsg(TLCmd.will, opt)); // Write [IAC WILL ECHO].
} else {
client?.write(TLOptMsg(TLCmd.wont, opt)); // Write [IAC WON'T ECHO].
}
} else if (cmd == TLCmd.will) {
if (echoEnabled) {
client?.write(TLOptMsg(TLCmd.doIt, opt)); // Write [IAC DO ECHO].
} else {
client?.write(TLOptMsg(TLCmd.doNot, opt)); // Write [IAC DON'T ECHO].
}
}
} break;
case 0x03: { // SUPPRESS GO AHEAD
if (cmd == TLCmd.will) {
client?.write(TLOptMsg(TLCmd.doIt, opt)); // Write [IAC DO SUPPRESS_GO_AHEAD].
}
} break;
case 0x12: { // LOGOUT
// Pass
} break;
case 0x18: { // TERMINAL TYPE
if (cmd == TLCmd.doIt) {
client?.writeAll([
TLOptMsg(TLCmd.will, opt), // Write [IAC WILL TERMINAL_TYPE].
TLSubMsg(opt, [0x00, 0x41, 0x4E, 0x53, 0x49]), // Write [IAC SB TERMINAL_TYPE IS ANSI IAC SE].
]);
}
} break;
case 0x1F: { // WINDOW SIZE
if (cmd == TLCmd.doIt) {
client?.writeAll([
TLOptMsg(TLCmd.will, opt), // Write [IAC WILL WINDOW_SIZE].
TLSubMsg(opt, [0x00, 0x5A, 0x00, 0x18]), // Write [IAC SB WINDOW_SIZE 90 24 IAC SE].
]);
}
} break;
default: {
if (cmd == TLCmd.doIt) {
client?.write(TLOptMsg(TLCmd.wont, opt)); // Write [IAC WON'T opt].
} else if (cmd == TLCmd.will) {
client?.write(TLOptMsg(TLCmd.doNot, opt)); // Write [IAC DON'T opt].
}
}
}
}
} else if (!hasLogin && event.msg is TLTextMsg) {
final text = (event.msg as TLTextMsg).text.toLowerCase();
if (text.contains("welcome")) {
hasLogin = true;
print("[INFO] Login OK!");
} else if (text.contains("login:") || text.contains("username:")) {
client!.write(TLTextMsg("$username\r\n")); // Write [username].
} else if (text.contains("password:")) {
client!.write(TLTextMsg("$password\r\n")); // Write [password].
}
}
}
}