nodeserver.service: unit_description_node.js_example_server_after_network.target_service_execstart_usr_local_bin_node_usr_local_src_node_server.js_workingdirectory_usr_local_src_node_restart_always_restartsec_10_environment_node_port_3000_install_wantedby_multi-user.target


server.js: {{ /* ----- */ const { exec } = require('child_process'); var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var piWifi = require('pi-wifi'); var fs = require('fs'); /* ----- */ app.use(bodyParser.urlencoded()); app.use(express.static('public')); /* ----- */ var statsRX = 0; var statsTX = 0; var statsRX_MAC = 0; var statsTX_MAC = 0; var autoMACpp = false; var autoMAC_limit_MB = 9; /* ----- */ app.get('/', function (req, res) { piWifi.status('wlan0', function(err, status) { // console.log(status); res.write('<!doctype html>'); res.write('<head><meta charset="utf-8">'); res.write('<meta http-equiv="refresh" content="10" />'); res.write('<link rel="stylesheet" href="/bootstrap.min.css" crossorigin="anonymous">'); res.write('<title>Main</title></head><body>'); res.write('<h1>Status:</h1>'); if (!err) { res.write('SSID: ' + status.ssid + '<br>'); res.write('MAC: ' + status.mac + '<br>'); res.write('IP: ' + status.ip + '<br>'); res.write('<hr class="separator">'); } res.write('RX gesamt: ' + statsRX + ' Bytes | seit neuer MAC: ' + statsRX_MAC + ' Bytes<br>'); res.write('TX gesamt: ' + statsTX + ' Bytes | seit neuer MAC: ' + statsTX_MAC + ' Bytes<br>'); res.write('<hr class="separator">'); res.write('<form class="form-inline" action="/toggleAutoMAC">'); res.write('<div class="form-group mx-sm-3">'); if (autoMACpp) { // Feld mit Menge ausgegraut res.write('<label for="inputLimit">AutoMAC Limit (MByte): </label>'); res.write('<input type="number" class="form-control" name="inputLimit" id="inputLimit" disabled placeholder="' + autoMAC_limit_MB + '">'); res.write('</div>'); res.write('<button type="submit" class="btn btn-primary mr-2" name="submit">AutoMAC off</button>'); } else { // Feld mit Menge res.write('<label for="inputLimit">AutoMAC Limit (MByte): </label>'); res.write('<input type="number" class="form-control" name="inputLimit" id="inputLimit">'); res.write('</div>'); res.write('<button type="submit" class="btn btn-primary mr-2" name="submit">AutoMAC on</button>'); } res.write('</form>') res.write('<hr class="separator">'); res.write('<a class="btn btn-primary mr-2" href="wifilist">WiFi-List</a>'); res.write('<a class="btn btn-primary mr-2" href="mac_plus">MAC++</a>'); //res.write('<a class="btn btn-primary mr-2" href="disconnect">Disconnect</a>'); //res.write('<a class="btn btn-primary mr-2" href="restartIF">Restart Interface</a>'); res.write('<a class="btn btn-primary mr-2" href="reload">Reload</a>'); res.write('<hr class="separator">'); res.write('<a class="btn btn-primary mr-2" href="shutdown">Shutdown</a>'); res.write('</body></html>'); res.end(); }); }); /* ----- */ app.get('/toggleAutoMAC', function (req, res) { if (!autoMACpp) { if (!req.query.inputLimit.trim()) { //console.log('leer'); } else { //console.log(req.query.inputLimit); autoMAC_limit_MB = req.query.inputLimit; autoMACpp = !autoMACpp; } } else { autoMACpp = !autoMACpp; } res.redirect("/"); }); /* ----- */ app.get('/wifilist', function (req, res) { res.write('<!doctype html>'); res.write('<head><meta charset="utf-8">'); res.write('<link rel="stylesheet" href="/bootstrap.min.css" crossorigin="anonymous">'); res.write('<title>WiFi-List</title></head><body>'); res.write('<h1>Available WiFi-Networks:</h1>'); res.write('<ul>'); piWifi.scan(function(err, networks) { //console.log(networks); networks.forEach(function(network) { if (network.flags === '[ESS]') { res.write('<li><a href="connOpen=' + network.ssid + '">' + network.ssid + '</a> (' + network.flags + ')</li>'); } else { res.write('<li><a href="connSec=' + network.ssid + '">' + network.ssid + '</a> (' + network.flags + ')</li>'); } }); res.write('</ul>'); res.write('<a class="btn btn-primary mr-2" href="/">Back</a>'); res.write('</body></html>'); res.end(); }); }); /* ----- */ app.get('/connOpen=*', function(req, res) { //console.log(req); var ssid = req.originalUrl.replace("/connOpen=", "") console.log(ssid); // new connection piWifi.connectTo({ssid: ssid}, function(err) { if (!err) { //Network created correctly setTimeout(function () { piWifi.check(ssid, function (err, status) { if (!err && status.connected) { console.log('Connected to the network ' + ssid + '!'); res.redirect("/"); } else { console.log('Unable to connect to the network ' + ssid + '!'); res.redirect("/"); } }); }, 2000); } else { console.log('Unable to create the network ' + ssid + '.'); res.redirect("/"); } }); }); /* ----- */ app.get('/connSec=*', function(req, res) { //console.log(req); var ssid = req.originalUrl.replace("/connSec=", "") // console.log('Sec: ' + ssid); if (ssid === 'Triton') { var netT = { ssid: ssid, password: 'Neptun_1846_Mond_Kryo'}; console.log('Trit'); piWifi.connectTo(netT, function(err) { if (!err) { //Network created correctly setTimeout(function () { piWifi.check(ssid, function (err, status) { if (!err && status.connected) { console.log('Connected to the network ' + ssid + '!'); res.redirect("/"); } else { console.log('Unable to connect to the network ' + ssid + '!'); res.redirect("/"); } }); }, 2000); } else { console.log('Unable to create the network ' + ssid + '.'); res.redirect("/"); } }); /* } else if (ssid === 'Ganymed') { var netT = { ssid: ssid, password: 'Jupiter_Mond_3_1610'}; console.log('Gany'); piWifi.connectTo(netT, function(err) { if (!err) { //Network created correctly setTimeout(function () { piWifi.check(ssid, function (err, status) { if (!err && status.connected) { console.log('Connected to the network ' + ssid + '!'); res.redirect("/"); } else { console.log('Unable to connect to the network ' + ssid + '!'); res.redirect("/"); } }); }, 2000); } else { console.log('Unable to create the network ' + ssid + '.'); res.redirect("/"); } }); */ } else { // console.log('unknown'); res.write('<!doctype html>'); res.write('<head><meta charset="utf-8">'); res.write('<link rel="stylesheet" href="/bootstrap.min.css" crossorigin="anonymous">'); res.write('<title>Password</title></head><body>'); res.write('<form class="form-inline" action="/connSec2">'); res.write('<div class="form-group mx-sm-3">'); res.write('<label for="inputPassword">Password for '); res.write(ssid + ':</label>'); res.write('<input type="text" class="form-control" name="password" id="inputPassword" placeholder="Password">'); res.write('</div>'); res.write('<button type="submit" class="btn btn-primary mr-2" value="' + ssid + '" name="submit">Connect</button>'); res.write('</form>'); res.write('</body></html>'); res.end(); } }); /* ----- */ app.get('/connSec2', function (req, res) { // console.log(req); console.log(req.query.password) console.log(req.query.submit) var ssid = req.query.submit; var password = req.query.password; var netT = { ssid: ssid, password: password}; piWifi.connectTo(netT, function(err) { if (!err) { //Network created correctly setTimeout(function () { piWifi.check(ssid, function (err, status) { if (!err && status.connected) { console.log('Connected to the network ' + ssid + '!'); res.redirect("/"); } else { console.log('Unable to connect to the network ' + ssid + '!'); res.redirect("/"); } }); }, 2000); } else { console.log('Unable to create the network ' + ssid + '.'); res.redirect("/"); } }); }); /* ----- */ app.get('/mac_plus', function (req, res) { // console.log("mac++"); exec('/usr/local/src/mac_pp.sh', function(error, stdout, stderr) { // console.log(stdout); statsRX_MAC = statsRX; statsTX_MAC = statsTX; res.redirect("/"); }); }); /* ----- */ app.get('/disconnect', function (req, res) { // console.log("disconnect"); piWifi.disconnect(function(err) { if (err) { console.log("Disconnect failed:" + err.message); res.redirect("/"); } console.log("Disconnect from network"); res.redirect("/"); }); }); /* ----- */ app.get('/restartIF', function (req, res) { // console.log("restartIF"); piWifi.restartInterface('wlan0', function(err) { if(err) { console.log('restartIF failed'); // res.redirect("/"); } console.log("Interface restarted"); // res.redirect("/"); }); }); /* ----- */ app.get('/reload', function (req, res) { // console.log("Reload"); res.redirect("/"); }); /* ----- */ function delayedShutdown() { exec('/sbin/halt', function(error, stdout, stderr) { console.log(stdout); }); } app.get('/shutdown', function (req, res) { // console.log("Shutdown"); res.redirect("/"); setTimeout(delayedShutdown, 1500); }); /* ----- */ function dataIntervalFunc() { exec("cat /proc/net/dev | grep wlan0 | awk '{ print $2, $10 }'", function(error, stdout, stderr) { // console.log(stdout); var res = stdout.split(" "); statsRX = res[0]; statsTX = res[1]; if (autoMACpp) { if ((statsRX_MAC == 0) && ((parseInt(statsRX, 10) + parseInt(statsTX, 10)) > (1000000 * autoMAC_limit_MB))) { console.log('neu1: ' + statsRX + ' ' + statsTX + ' ' + (parseInt(statsRX, 10) + parseInt(statsTX, 10)) ); exec('/usr/local/src/mac_pp.sh', function(error, stdout, stderr) { statsRX_MAC = statsRX; statsTX_MAC = statsTX; }); } else if ((parseInt(statsRX, 10) + parseInt(statsTX, 10) - statsRX_MAC - statsTX_MAC) > (1000000 * autoMAC_limit_MB)) { console.log('neu2: ' + statsRX + ' ' + statsTX + ' ' + + statsRX_MAC + ' ' + statsTX_MAC + ' ' + + (parseInt(statsRX, 10) + parseInt(statsTX, 10) - statsRX_MAC - statsTX_MAC) ); exec('/usr/local/src/mac_pp.sh', function(error, stdout, stderr) { statsRX_MAC = statsRX; statsTX_MAC = statsTX; }); } } }); }; setInterval(dataIntervalFunc, 5000); /* ----- */ function pingIntervalFunc() { // ping exec('ping -c 1 heise.de', function(error, stdout, stderr) { // console.log(stdout); }); }; setInterval(pingIntervalFunc, 10000); /* ----- */ app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); /* // /etc/wpa_supplicant/wpa_supplicant.conf fs.unlink('/etc/wpa_supplicant/wpa_supplicant.conf', function(error) { if (error) { console.log('Could not deleted dog.jpg!!'); } console.log('Deleted dog.jpg!!'); }); */ }} ----- mac_pp.sh: {{ #!/bin/sh mac=`ifconfig wlan0 | grep ether | awk '{ print $2 }'` echo $mac mac1=$( echo "$mac" | tr -d ':' ) #echo $mac1 mac2=$( printf "%d\n" 0x"$mac1" ) #echo $mac2 mac3=$( expr $mac2 + 1 ) #echo $mac3 mac4=$( printf "%x\n" "$mac3" ) #echo $mac4 mac5=$( echo $mac4 | sed 's/\(..\)/:/g;s/:$//' ) echo $mac5 ifconfig wlan0 down sleep 1 ifconfig wlan0 hw ether $mac5 sleep 1 ifconfig wlan0 up echo Up! }} ----- evtl. nützlich: {{ const { exec } = require('child_process'); function scan_wlan() { return new Promise(function (resolve, reject) { exec("ipconfig", (error, stdout, stderr) => { resolve(stdout); }); }); } // Promise.all() scan_wlan().then((networks) => { // res.write(networks); res.write('</body></html>'); res.end(); }); }}