SetNetworkInterface.qml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import QtQuick 2.5
  2. import ApplicationLauncher 1.0
  3. Item {
  4. property string mode: "static"
  5. property string ifacename: "eth0"
  6. property string address: "192.168.0.125"
  7. property string netmask: "255.255.255.0"
  8. property string gateway: "192.168.0.10"
  9. property string nameserver: "192.168.0.10"
  10. property string infile: "/etc/network/interfaces"
  11. property string outfile: "/etc/network/interfaces"
  12. function doSetInterface() {
  13. setInterface.interfaceMode = "Set";
  14. setInterface.launchScript();
  15. }
  16. function doGetInterface() {
  17. setInterface.interfaceMode = "Get";
  18. setInterface.launchScript();
  19. }
  20. function doRestartNetwork() {
  21. worker.appName = "/etc/init.d/S40network";
  22. worker.arguments = "restart";
  23. worker.launchScript();
  24. }
  25. Application{
  26. id: worker
  27. onAppFinished: {
  28. console.log("WorkerDone");
  29. }
  30. onAppStarted: {
  31. console.debug("WorkerappStarted :");
  32. }
  33. onAppError: {
  34. console.debug("WorkerappError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
  35. }
  36. }
  37. Application {
  38. id: writeInterfaces
  39. outFName: outfile
  40. onAppFinished: {
  41. console.log("IfaceDone");
  42. }
  43. onAppStarted: {
  44. console.debug("IFACEappStarted :");
  45. }
  46. onAppError: {
  47. console.debug("IFACEappError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
  48. }
  49. }
  50. Application {
  51. property string interfaceMode:""
  52. id: setInterface
  53. appName: "cat"
  54. arguments: infile
  55. onAppFinished: {
  56. console.debug("AppFinished :");
  57. console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
  58. console.debug("stdERR :" + stdERR);
  59. console.debug("stdOUT :" + stdOUT);
  60. console.debug("======================================");
  61. var lines = stdOUT.split(/[\r\n]+/g);
  62. var idx = 0;
  63. var iface = [];
  64. var inIface = 0;
  65. lines.forEach(function(entry) {
  66. if (entry.length > 0) {
  67. if(entry.charAt(0) === '#'){
  68. //console.log(idx + ":: " + "=======>:" + JSON.stringify(entry));
  69. } else {
  70. if (entry.indexOf("iface") !== -1){ //Interface Entry start
  71. var ifaceEntry = entry.split(" ");
  72. if ((ifaceEntry.length >=2) && (ifaceEntry[1] === "eth0")) {
  73. iface.push({index: idx, txt: entry});
  74. inIface = 1;
  75. } else {
  76. inIface = 0;
  77. }
  78. } else {
  79. if(inIface === 1) {
  80. iface.push({index: idx, txt: entry});
  81. }
  82. }
  83. }
  84. }
  85. ++idx;
  86. });
  87. var interfaceCMD = interfaceMode;
  88. if (interfaceCMD === "Get"){ // Auslesen der aktuellen Einstellungen
  89. console.log(">>>>>>>>>:: " + JSON.stringify(interfaceCMD));
  90. mode = "";
  91. address = "";
  92. netmask = "";
  93. nameserver = "";
  94. gateway = "";
  95. if (iface[0].txt.indexOf("dhcp") !== -1) {
  96. mode = "dhcp";
  97. }
  98. if (iface[0].txt.indexOf("static") !== -1) {
  99. mode = "static";
  100. var i;
  101. var wdata;
  102. for ( i = 1; i < iface.length; i++) {
  103. if ( iface[i].txt.indexOf("address") !== -1) {
  104. wdata = iface[i].txt.split(" ");
  105. address = wdata[1];
  106. }
  107. if ( iface[i].txt.indexOf("netmask") !== -1) {
  108. wdata = iface[i].txt.split(" ");
  109. netmask = wdata[1];
  110. }
  111. if ( iface[i].txt.indexOf("gateway") !== -1) {
  112. wdata = iface[i].txt.split(" ");
  113. gateway = wdata[1];
  114. }
  115. if ( iface[i].txt.indexOf("nameserver") !== -1) {
  116. wdata = iface[i].txt.split(" ");
  117. wdata[3] = wdata[3].replace(/\'/g, '').replace(/\"/g, '');
  118. nameserver = wdata[3];
  119. }
  120. }
  121. }
  122. }
  123. if (interfaceCMD === "Set") { // Setzen der Gewälten Einstellungen
  124. console.log(">>>>>>>>>:: " + JSON.stringify(interfaceCMD));
  125. lines.splice(iface[0].index, iface[iface.length - 1].index - iface[0].index + 1);
  126. var insertIdx = iface[0].index;
  127. var insertText;
  128. if(mode === "static") {
  129. insertText = "iface " + ifacename + " inet " + mode;
  130. lines.splice(insertIdx, 0, insertText);
  131. ++insertIdx;
  132. insertText = "\taddress " + address;
  133. lines.splice(insertIdx, 0, insertText);
  134. ++insertIdx;
  135. insertText = "\tnetmask " + netmask;
  136. lines.splice(insertIdx, 0, insertText);
  137. ++insertIdx;
  138. insertText = "\tgateway " + gateway;
  139. lines.splice(insertIdx, 0, insertText);
  140. ++insertIdx;
  141. //Broadcast Adresse ausrechnen
  142. var wrkAddr = address.split(".");
  143. var ipAddress = new Uint8Array(4);
  144. var i;
  145. for (i = 0; i< 4; i++) {
  146. ipAddress[i] = wrkAddr[i];
  147. }
  148. wrkAddr = netmask.split(".");
  149. var ipMask = new Uint8Array(4);
  150. for (i = 0; i< 4; i++) {
  151. ipMask[i] = wrkAddr[i];
  152. }
  153. var broadcastUINT = new Uint8Array(4);
  154. for (i = 0; i< 4; i++) {
  155. broadcastUINT[i] = ipAddress[i] | (~ipMask[i]);
  156. }
  157. var broadcast = broadcastUINT[0] +"." +
  158. broadcastUINT[1] +"." + broadcastUINT[2] +"." +
  159. broadcastUINT[3];
  160. insertText = "\tbroadcast " + broadcast;
  161. lines.splice(insertIdx, 0, insertText);
  162. ++insertIdx;
  163. insertText = "\tpost-up echo " + "'nameserver " + nameserver + "' > /etc/resolv.conf";
  164. lines.splice(insertIdx, 0, insertText);
  165. } else if (mode === "dhcp") {
  166. insertText = "iface " + ifacename + " inet " + mode;
  167. lines.splice(insertIdx, 0, insertText);
  168. ++insertIdx;
  169. }
  170. var cmd = "echo -e " + "\"";
  171. lines.forEach(function(entry){
  172. cmd = cmd + entry + "\\n"
  173. //console.log(JSON.stringify(entry));
  174. });
  175. cmd = cmd + "\"";
  176. writeInterfaces.appName = cmd;
  177. writeInterfaces.launchScript();
  178. }
  179. console.log(">> mode :: " + JSON.stringify(mode));
  180. console.log(">> address :: " + JSON.stringify(address));
  181. console.log(">> netmask :: " + JSON.stringify(netmask));
  182. console.log(">> gateway :: " + JSON.stringify(gateway));
  183. console.log(">> nameserver :: " + JSON.stringify(nameserver));
  184. }
  185. onAppStarted: {
  186. console.debug("appStarted :");
  187. console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
  188. }
  189. onAppError: {
  190. console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
  191. }
  192. }
  193. }