import QtQuick 2.5 import ApplicationLauncher 1.0 Item { property string mode: "static" property string ifacename: "eth0" property string address: "192.168.0.125" property string netmask: "255.255.255.0" property string gateway: "192.168.0.10" property string nameserver: "192.168.0.10" property string infile: "/etc/network/interfaces" property string outfile: "/etc/network/interfaces" function doSetInterface() { setInterface.interfaceMode = "Set"; setInterface.launchScript(); } function doGetInterface() { setInterface.interfaceMode = "Get"; setInterface.launchScript(); } function doRestartNetwork() { worker.appName = "/etc/init.d/S40network"; worker.arguments = "restart"; worker.launchScript(); } ApplicationLaunch{ id: worker onAppFinished: { console.log("WorkerDone"); } onAppStarted: { console.debug("WorkerappStarted :"); } onAppError: { console.debug("WorkerappError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode ); } } ApplicationLaunch { id: writeInterfaces outFName: outfile onAppFinished: { console.log("IfaceDone"); } onAppStarted: { console.debug("IFACEappStarted :"); } onAppError: { console.debug("IFACEappError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode ); } } ApplicationLaunch { property string interfaceMode:"" id: setInterface appName: "cat" arguments: infile onAppFinished: { console.debug("AppFinished :"); console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode ); console.debug("stdERR :" + stdERR); console.debug("stdOUT :" + stdOUT); console.debug("======================================"); var lines = stdOUT.split(/[\r\n]+/g); var idx = 0; var iface = []; var inIface = 0; lines.forEach(function(entry) { if (entry.length > 0) { if(entry.charAt(0) === '#'){ //console.log(idx + ":: " + "=======>:" + JSON.stringify(entry)); } else { if (entry.indexOf("iface") !== -1){ //Interface Entry start var ifaceEntry = entry.split(" "); if ((ifaceEntry.length >=2) && (ifaceEntry[1] === "eth0")) { iface.push({index: idx, txt: entry}); inIface = 1; } else { inIface = 0; } } else { if(inIface === 1) { iface.push({index: idx, txt: entry}); } } } } ++idx; }); var interfaceCMD = interfaceMode; if (interfaceCMD === "Get"){ // Auslesen der aktuellen Einstellungen console.log(">>>>>>>>>:: " + JSON.stringify(interfaceCMD)); mode = ""; address = ""; netmask = ""; nameserver = ""; gateway = ""; if (iface[0].txt.indexOf("dhcp") !== -1) { mode = "dhcp"; } if (iface[0].txt.indexOf("static") !== -1) { mode = "static"; var i; var wdata; for ( i = 1; i < iface.length; i++) { if ( iface[i].txt.indexOf("address") !== -1) { wdata = iface[i].txt.split(" "); address = wdata[1]; } if ( iface[i].txt.indexOf("netmask") !== -1) { wdata = iface[i].txt.split(" "); netmask = wdata[1]; } if ( iface[i].txt.indexOf("gateway") !== -1) { wdata = iface[i].txt.split(" "); gateway = wdata[1]; } if ( iface[i].txt.indexOf("nameserver") !== -1) { wdata = iface[i].txt.split(" "); wdata[3] = wdata[3].replace(/\'/g, '').replace(/\"/g, ''); nameserver = wdata[3]; } } } } if (interfaceCMD === "Set") { // Setzen der Gewälten Einstellungen console.log(">>>>>>>>>:: " + JSON.stringify(interfaceCMD)); lines.splice(iface[0].index, iface[iface.length - 1].index - iface[0].index + 1); var insertIdx = iface[0].index; var insertText; if(mode === "static") { insertText = "iface " + ifacename + " inet " + mode; lines.splice(insertIdx, 0, insertText); ++insertIdx; insertText = "\taddress " + address; lines.splice(insertIdx, 0, insertText); ++insertIdx; insertText = "\tnetmask " + netmask; lines.splice(insertIdx, 0, insertText); ++insertIdx; insertText = "\tgateway " + gateway; lines.splice(insertIdx, 0, insertText); ++insertIdx; //Broadcast Adresse ausrechnen var wrkAddr = address.split("."); var ipAddress = new Uint8Array(4); var i; for (i = 0; i< 4; i++) { ipAddress[i] = wrkAddr[i]; } wrkAddr = netmask.split("."); var ipMask = new Uint8Array(4); for (i = 0; i< 4; i++) { ipMask[i] = wrkAddr[i]; } var broadcastUINT = new Uint8Array(4); for (i = 0; i< 4; i++) { broadcastUINT[i] = ipAddress[i] | (~ipMask[i]); } var broadcast = broadcastUINT[0] +"." + broadcastUINT[1] +"." + broadcastUINT[2] +"." + broadcastUINT[3]; insertText = "\tbroadcast " + broadcast; lines.splice(insertIdx, 0, insertText); ++insertIdx; insertText = "\tpost-up echo " + "'nameserver " + nameserver + "' > /etc/resolv.conf"; lines.splice(insertIdx, 0, insertText); } else if (mode === "dhcp") { insertText = "iface " + ifacename + " inet " + mode; lines.splice(insertIdx, 0, insertText); ++insertIdx; } var cmd = "echo -e " + "\""; lines.forEach(function(entry){ cmd = cmd + entry + "\\n" //console.log(JSON.stringify(entry)); }); cmd = cmd + "\""; writeInterfaces.appName = cmd; writeInterfaces.launchScript(); } console.log(">> mode :: " + JSON.stringify(mode)); console.log(">> address :: " + JSON.stringify(address)); console.log(">> netmask :: " + JSON.stringify(netmask)); console.log(">> gateway :: " + JSON.stringify(gateway)); console.log(">> nameserver :: " + JSON.stringify(nameserver)); } onAppStarted: { console.debug("appStarted :"); console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode ); } onAppError: { console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode ); } } }