SetNetworkInterface.qml 9.1 KB

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