Search code examples
node.jspowershelloutputtelnet

What is the proper way to clear telnet streams with nodejs and powershell?


I have a neat little nodejs script to log into raw sockets in text mode (telnet, mostly). I have noticed some servers will send print relocations, but my program must be overlooking some sort of cls equivalent; because relocated text simply prints over existing text, leaving a garbled mess of new and old output. How can I accept and process whatever the telnet character sequence is for clearing output with the following program?

var net = require("net");
var argv = process.argv.slice(2);
var client = null;
var echo = false;
var hostName = null;
var port = null;
var charSet = "utf-8";

var init = function(print, error, quit, argv, keyboard, argParser) {
    argv.forEach(argParser);
    checkVars();
    client = new net.Socket();
    client.on("data", print);
    client.on("error", error);
    client.on("close", quit);
    client.connect(port, hostName, function() { });
    process.stdin.resume();
    process.stdin.setRawMode(true);
    process.stdin.setEncoding(charSet);
    process.stdin.on("data", keyboard);
};

var print = function(text) {
    process.stdout.write(text);
};

var keyboard = function(key) {
    if (echo) print(key);
    client.write(key);
};

var error = function(e) {
    print(e.toString() + "\n");
    quit();
};

var quit = function() {
    print("\n");
    process.exit();
};

var argParser = function(arg) {
    if (arg.startsWith("echo")) echo = true;
    else if(arg.startsWith("host:")) hostName = arg.split(":")[1];
    else if (arg.startsWith("port:")) port = parseInt(arg.split(":")[1]);
    else if (arg.startsWith("charSet:")) charSet = arg.split(":")[1];
}

var checkVars = function() {
    if (hostName == null ||
        port == null) {
      print("\nNEVER RUN FOREIGN CODE WITHOUT FIRST READING IT\n");
      quit();
    }
}

init(print, error, quit, argv, keyboard, argParser);

Solution

  • If the remote host is moving your cursor around, it's probably doing it with ANSI escape codes. If so, Esc[2J is the clear-screen escape code.

    Here are the rest of escape codes.