Search code examples
node.jssshxterm

nodejs ssh client disable colors


anybody knows how to turn off color tags in the terminal?

var conn = require('ssh2').Client();
var onShell = function(err, stream) { 
    ...
    var start = true;
    var data_out = '';
    stream.on('data', function(data) {

          if (/ > $/.test(data)) {
            console.log('test oK');
            if ( start ) {
                stream.write('export\r\n');
                start = false;
            }
            else {
                console.log(data_out);  
                require('fs').writeFileSync('conf.txt', data_out)
                stream.end();
                conn.end();
            }   
          }
          data_out += data;
    });
}

сonn.on('ready', function() {
      console.log('Client :: ready');
      conn.shell(onShell);
}).connect({
      host: '62.117.93.138', // my Mikrotik Router
      port: 22,
});

in conf.txt i see color tags like this:

[m[36m/system[m [m[36mntp[m [m[36mserver [m[35mset[m [m[32menabled[m[33m=[m[32myes [m[36m/tool[m [m[36mromon[m [m[36mport [m[35madd[m [m[32mdisabled[m[33m=[m[32mno

instead:

/system ntp server
   set enabled=yes
/tool romon port
   add disabled=no

Solution

  • Shell sessions use pseudo-TTYs, so the remote end thinks there is a real terminal. You're probably better off trying to use exec() if at all possible, otherwise just use a simple regexp to remove all of the escape sequences from the output.