HTTP-GET to Wakeup and Shutdown Linux Box

Two flows really, one uses HTTP-GET as an input and the issues a Wake-On-LAN (WOL), after a lookup of MAC address for the server name passed from the HTTP-GET.

The second uses SSH with shared keys to allow passwordless logon to a server to shut it down.

Instructions as to how to make a JavaScript http-get, also how to configure a Linux box for paswordless login included.

[{"id":"74ea4337.49c43c","type":"http in","name":"Recieve WOL name","url":"/doWakeOnLAN.html","method":"get","swaggerDoc":"","x":117.10450744628906,"y":116.07008361816406,"z":"cdc403fd.bbede","wires":[["7e28230a.6be708"]]},{"id":"c01bd2ea.60fef","type":"debug","name":"","active":false,"console":"false","complete":"mac","x":732.1419711642795,"y":161.957522922092,"z":"cdc403fd.bbede","wires":[]},{"id":"dc8ffad5.37a728","type":"comment","name":"Wake On LAN","info":"","x":91.66004943847656,"y":68.87004089355469,"z":"cdc403fd.bbede","wires":[]},{"id":"7e28230a.6be708","type":"function","name":"Format Message for WOL","func":"// Set MAC to wake based on supplied hostname\nswitch (msg.payload.value) {\n\tcase \"servername\":\n\tvar MACAddr = \"01:23:45:67:89:ab\";\n\tbreak;\n\n\tdefault:\n\tvar MAC = \"00:00:00:00:00:00\";\n}\n\n\n// Build these into a message with the name setValue.\nvar returnMsg={topic: \"WOL\", mac: MACAddr};\nreturn returnMsg;\n\n","outputs":1,"noerr":0,"x":449.1863742404514,"y":114.95750766330295,"z":"cdc403fd.bbede","wires":[["c01bd2ea.60fef","5f192c5c.ae22d"]]},{"id":"5f192c5c.ae22d","type":"wake on lan","mac":"","name":"","x":730.1419559054905,"y":113.15758090549045,"z":"cdc403fd.bbede","wires":[]},{"id":"d55d9e5f.1fd9b","type":"http in","name":"Recieve Shutdown Name","url":"/doShutdownOnSSH.html","method":"get","swaggerDoc":"","x":123.94439697265625,"y":305.35235595703125,"z":"cdc403fd.bbede","wires":[["de10a133.23589"]]},{"id":"4d4dc2aa.e16a3c","type":"debug","name":"","active":false,"console":"false","complete":"true","x":775.7485080295139,"y":396.4509921603733,"z":"cdc403fd.bbede","wires":[]},{"id":"30d08198.805e9e","type":"comment","name":"Shutdown on SSH","info":"","x":109.9888916015625,"y":249.7301025390625,"z":"cdc403fd.bbede","wires":[]},{"id":"de10a133.23589","type":"function","name":"Format Command for Shutdown","func":"var fqdn=\"fqdn.com\";\n\nvar host=msg.payload.value+\".\"+fqdn;\n\nvar returnMsg={topic: \"Shutdown\", payload: host+\" poweroff\"};\n\nreturn returnMsg;\n","outputs":1,"noerr":0,"x":483.60408359103735,"y":301.17312961154516,"z":"cdc403fd.bbede","wires":[["4d4dc2aa.e16a3c","a0908da3.8a9fa"]]},{"id":"a0908da3.8a9fa","type":"exec","command":"ssh ","addpay":true,"append":"","useSpawn":"","name":"SSH msg.payload","x":765.5997043185764,"y":301.30200534396704,"z":"cdc403fd.bbede","wires":[["4d4dc2aa.e16a3c"],["4d4dc2aa.e16a3c"],["4d4dc2aa.e16a3c"]]},{"id":"af7e956d.087938","type":"comment","name":"Notes about setup","info":"1:\nMessages from HTTP-GET should be formatted to deliver \"value\" as\nthe MAC address to start, or the shortname to shutdown.\n\nHere is a javascript http get example for WOL:\n$.get(\"http://node-red.fqdn.com:1880/doWakeOnLAN.html\", {value:\"servername\", time:Date()});\n\nAnd shutdown:\n$.get(\"http://node-red.fqdn.com:1880/doShutdownOnSSH.html\", {value:\"servername\", time:Date()});\n\nNote the addition of a datetime stamp, this is used to prevent caching of\nthe URL that would prevent the HTTP-GET working after a couple of times\non some of the more aggressive caching web browsers, such as Windowphone IE.\n\n2:\nEdit the switch/case statement in \"Format Message for WOL\" to contain\nthe shornames of all the machines you want to start with the\ncorresponding MAC.\n\n3:\nEdit the \"Format Command for Shutdown\" to contain the FQDN of the network\nthat the machines are in.\n\n4:\nFinally, the biggie: The machine to be shutdown must have passwordless SSH\nsetup with the machine running Node Red. Here is a quick and dirty setup,\nrun the following commands on the Node Red machine, use blank passwords\nand accept any defults:\n\nssh-keygen\nssh-copy-id -i ~/.ssh/id_rsa.pub target-machine.domain.com\n\nDANGER! I'm using root here anything with access to the node-red machine\nwill have root access to the machine being shutdown!\n","x":396.1907958984375,"y":67.95195007324219,"z":"cdc403fd.bbede","wires":[]}]
fraser73

Flow Info

created 1 year, 11 months ago

Node Types

Core
  • comment (x3)
  • debug (x2)
  • exec (x1)
  • function (x2)
  • http in (x2)
Other

Tags

  • shutdown
  • wol
  • linux
  • start
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option