Advertisement
Guest User

Untitled

a guest
Dec 25th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. ; #Warn ; Enable warnings to assist with detecting common errors.
  3. SetWorkingDir, %A_ScriptDir% ; Ensures a consistent starting directory.
  4. #SingleInstance, Force
  5.  
  6. If %0% ; The script was called with at least 1 argument, expectedly the user drag'n'dropped something onto the script.
  7. Loop, %0%
  8. input := %A_Index%
  9. If FileExist(input) ; Not sure if this needed, since user can't drag'n'drop an unexisting file.
  10. FileRead, input, %input%
  11. ; Loop, %inputFileName%
  12. ; inputFileName := A_LoopFileName
  13.  
  14. ; input = [{"defroute":"123.123.123.123","ifaces":[{"addrs":[{"IP":"1.0.0.2","mask":"24"},{"IP":"321.123.123.123","mask":"23"}],"name":"em0","descr":"external","Net":"vnet2"},{"addrs":[{"IP":"2.0.0.1","mask":"24"},{"IP":"2.0.0.3","mask":"23"}],"name":"em1","descr":"internal","Net":"vnet3"}],"name":"NCC"},{"defroute":"2.0.0.1","ifaces":[{"addrs":[{"IP":"2.0.0.2","mask":"24"}],"name":"em0","descr":"external","Net":"vnet4"},{"addrs":[{"IP":"172.17.6.6","mask":"22"},{"IP":"10.0.0.3","mask":"24"}],"name":"em1","descr":"external","Net":"vnet5"}],"name":"ПУ ЦУС"}]
  15.  
  16. If input
  17. {
  18. Try
  19. machines := JSON.parse(input)
  20. Catch E
  21. MsgBox, % e
  22. }
  23. Else
  24. machines := [{"Name": "", "DefRoute": "", "ifaces":[ {"Name": "em0", "Descr": "external", "Net": "vnet1", "Addrs":[ {"IP": "0.0.0.0", "Mask": "24"} ] }] }]
  25.  
  26. DrawGUI:
  27. ; Menu, menuToolbar, Add, Add node, AddNode
  28. Menu, menuToolbar, Add, Open, Open
  29. Menu, menuToolbar, Add, Save, Save
  30. Menu, menuToolbar, Add, Parse JSON, ParseTextJSON
  31. Gui, Menu, menuToolbar
  32. If machines
  33. GoSub, ParseJSON
  34. Gui, Show
  35. Return
  36.  
  37. ParseJSON:
  38. For node In machines
  39. {
  40. margin := 10 + (node-1) * 190
  41. Gui, Add, Text, x%margin% y0, Name:
  42. Gui, Add, Edit, x+0 w120 h16 vn%node%Name, % machines[node].Name
  43. If !(node = 1 && machines.MaxIndex() = 1) ; Forbid to remove the last existing node.
  44. Gui, Add, Button, x+0 w16 h16 vdelN%node% gDeleteNode, -
  45. Gui, Add, Button, x+0 w16 h16 vaddN%node% gAddNode, +
  46. Gui, Add, Text, x%margin% y+0, Default route:
  47. Gui, Add, Edit, x+0 w87 h16 vn%node%DefRoute, % machines[node].defRoute
  48. For iface In machines[node].ifaces
  49. {
  50. Gui, Add, Text, x%margin% y+0, If %iface%:
  51. Gui, Add, Edit, x+0 w29 h16 vn%node%If%iface%Name, % machines[node].ifaces[iface].Name
  52. Gui, Add, Text, x+0, Type:
  53. Gui, Add, Edit, x+0 w77 h16 vn%node%If%iface%Descr, % machines[node].ifaces[iface].Descr
  54. If !(iface = 1 && machines[node].ifaces.MaxIndex() = 1) ; Forbid to remove the last existing iface.
  55. Gui, Add, Button, x+0 w16 h16 vn%node%DelIf%iface% gDeleteIf, -
  56. Gui, Add, Button, x+0 w16 h16 vn%node%AddIf%iface% gAddIf, +
  57. Gui, Add, Text, x%margin% y+0, Net:
  58. Gui, Add, Edit, x+0 w131 h16 vn%node%If%iface%Net, % machines[node].ifaces[iface].Net
  59. For addr In machines[node].ifaces[iface].addrs
  60. {
  61. Gui, Add, Text, x%margin% y+0, Addr%addr%:
  62. Gui, Add, Edit, x+5 w89 h16 vn%node%If%iface%a%addr%addr, % machines[node].ifaces[iface].addrs[addr].IP
  63. Gui, Add, Text, x+0, /
  64. Gui, Add, Edit, x+0 w21 h16 vn%node%if%iface%a%addr%mask, % machines[node].ifaces[iface].addrs[addr].mask
  65. If !(addr = 1 && machines[node].ifaces[iface].addrs.MaxIndex() = 1) ; Forbid to remove the last existing addr.
  66. Gui, Add, Button, x+0 w16 h16 vn%node%If%iface%DelA%addr% gDeleteAddr, -
  67. Gui, Add, Button, x+0 w16 h16 vn%node%If%iface%AddA%addr% gAddAddr, +
  68. }
  69. }
  70. }
  71. Return
  72.  
  73. Open:
  74. FileSelectFile, input,,, Select a file with your saved stand, Stand (*.json)
  75. If (!ErrorLevel && input)
  76. {
  77. FileRead, input, %input%
  78. If input
  79. {
  80. Try
  81. machines := JSON.parse(input)
  82. Catch E
  83. MsgBox, % e
  84. }
  85. GoSub, Repaint
  86. }
  87. Return
  88.  
  89. Save:
  90. FileSelectFile, output, S, % (output ? output : A_WorkingDir) "\" A_Now ".json", Where to save this stand to?
  91. If (!ErrorLevel && output)
  92. {
  93. jsoned := JSON.stringify(machines)
  94. If FileExist(output)
  95. FileDelete, %output%
  96. FileAppend, %jsoned%, %output%, UTF-8
  97. ; GoSub, Repaint
  98. }
  99. Return
  100.  
  101. ParseTextJSON:
  102. Gui, InputJSON: New
  103. Gui, Add, Text,, Paste your JSON'ified stand:
  104. Gui, Add, Edit, w300 h250 vinputJSON
  105. Gui, Add, Button, x250, Parse
  106. Gui, InputJSON: Show
  107. Gui, 1: Default
  108. Return
  109.  
  110. Repaint:
  111. Gui, Destroy
  112. GoSub, DrawGUI
  113. Return
  114.  
  115. DeleteNode:
  116. machines.Remove(SubStr(A_GuiControl, 5))
  117. GoSub, Repaint
  118. Return
  119.  
  120. AddNode:
  121. RegExMatch(A_GuiControl, "Si)^addN(\d+)$", this)
  122. ; machines.Insert(this1+1, machines[machines.MaxIndex()]) ; Duplicate
  123. machines.Insert(this1+1, {"name": "", "defroute": "", "ifaces":[ {"name":"","type":"","addrs":[ {"IP": "", "mask": ""} ] } ] })
  124. GoSub, Repaint
  125. Return
  126.  
  127. DeleteIf:
  128. RegExMatch(A_GuiControl, "Si)^n(\d+)DelIf(\d+)$", this)
  129. machines[this1].ifaces.Remove(this2)
  130. GoSub, Repaint
  131. Return
  132.  
  133. AddIf:
  134. RegExMatch(A_GuiControl, "Si)^n(\d+)AddIf(\d+)$", this)
  135. ; machines[this1].ifaces.Insert(this2+1, machines[this1].ifaces[machines[this1].ifaces.MaxIndex()]) ; Duplicate
  136. machines[this1].ifaces.Insert(this2+1, {"name":"","type":"","addrs":[ {"IP": "", "mask": ""} ] })
  137. GoSub, Repaint
  138. Return
  139.  
  140. DeleteAddr:
  141. RegExMatch(A_GuiControl, "Si)^n(\d+)If(\d+)DelA(\d+)$", this)
  142. machines[this1].ifaces[this2].addrs.Remove(this3)
  143. GoSub, Repaint
  144. Return
  145.  
  146. AddAddr:
  147. RegExMatch(A_GuiControl, "Si)^n(\d+)If(\d+)AddA(\d+)$", this)
  148. ; machines[this1].ifaces[this2].addrs.Insert(this3+1, machines[this1].ifaces[this2].addrs[machines[this1].ifaces[this2].addrs.MaxIndex()]) ; Duplicate
  149. machines[this1].ifaces[this2].addrs.Insert(this3+1, {"IP": "", "mask": ""})
  150. GoSub, Repaint
  151. Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement