123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- 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();
- }
- Application{
- id: worker
- onAppFinished: {
- console.log("WorkerDone");
- }
- onAppStarted: {
- console.debug("WorkerappStarted :");
- }
- onAppError: {
- console.debug("WorkerappError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
- }
- }
- Application {
- id: writeInterfaces
- outFName: outfile
- onAppFinished: {
- console.log("IfaceDone");
- }
- onAppStarted: {
- console.debug("IFACEappStarted :");
- }
- onAppError: {
- console.debug("IFACEappError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
- }
- }
- Application {
- 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 );
- }
- }
- }
|