Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active November 30, 2018 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/fbedf1c6c4d9781ab7c897756bead886 to your computer and use it in GitHub Desktop.
Save Hermann-SW/fbedf1c6c4d9781ab7c897756bead886 to your computer and use it in GitHub Desktop.
Node.js script allowing for remote MicroPython command execution
#!/usr/bin/env node
[scpt,pwd,host] = process.argv.slice(1);
cmd = process.argv.slice(4).join(" ");
out = 0;
last = "";
if (process.argv.length < 5) {
console.log((require("path")).basename(scpt) + " pwd host cmd [...]");
process.exit(1);
}
var ws = new (require('ws'))("ws://" + host + ":8266/");
ws.onopen = function () { ws.send(pwd + "\r\n" + cmd + "\r\n"); }
ws.onmessage = function (ev) {
if ((last == "\r\n") && (ev.data == ">>> ")) { out = 0; ws.terminate(); }
if (out == 2) { process.stdout.write(ev.data); }
if (ev.data == "\r\nWebREPL connected\r\n>>> ") { out = 1; }
last = ev.data.slice(-2);
if ((out == 1) && (last == "\r\n")) { out = 2; }
}
@Hermann-SW
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment