Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
- ; #Warn ; Enable warnings to assist with detecting common errors.
- SetWorkingDir, %A_ScriptDir% ; Ensures a consistent starting directory.
- #SingleInstance, Force
- If %0% ; The script was called with at least 1 argument, expectedly the user drag'n'dropped something onto the script.
- Loop, %0%
- input := %A_Index%
- If FileExist(input) ; Not sure if this needed, since user can't drag'n'drop an unexisting file.
- FileRead, input, %input%
- ; Loop, %inputFileName%
- ; inputFileName := A_LoopFileName
- ; 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":"ПУ ЦУС"}]
- If input
- {
- Try
- machines := JSON.parse(input)
- Catch E
- MsgBox, % e
- }
- Else
- machines := [{"Name": "", "DefRoute": "", "ifaces":[ {"Name": "em0", "Descr": "external", "Net": "vnet1", "Addrs":[ {"IP": "0.0.0.0", "Mask": "24"} ] }] }]
- DrawGUI:
- ; Menu, menuToolbar, Add, Add node, AddNode
- Menu, menuToolbar, Add, Open, Open
- Menu, menuToolbar, Add, Save, Save
- Menu, menuToolbar, Add, Parse JSON, ParseTextJSON
- Gui, Menu, menuToolbar
- If machines
- GoSub, ParseJSON
- Gui, Show
- Return
- ParseJSON:
- For node In machines
- {
- margin := 10 + (node-1) * 190
- Gui, Add, Text, x%margin% y0, Name:
- Gui, Add, Edit, x+0 w120 h16 vn%node%Name, % machines[node].Name
- If !(node = 1 && machines.MaxIndex() = 1) ; Forbid to remove the last existing node.
- Gui, Add, Button, x+0 w16 h16 vdelN%node% gDeleteNode, -
- Gui, Add, Button, x+0 w16 h16 vaddN%node% gAddNode, +
- Gui, Add, Text, x%margin% y+0, Default route:
- Gui, Add, Edit, x+0 w87 h16 vn%node%DefRoute, % machines[node].defRoute
- For iface In machines[node].ifaces
- {
- Gui, Add, Text, x%margin% y+0, If %iface%:
- Gui, Add, Edit, x+0 w29 h16 vn%node%If%iface%Name, % machines[node].ifaces[iface].Name
- Gui, Add, Text, x+0, Type:
- Gui, Add, Edit, x+0 w77 h16 vn%node%If%iface%Descr, % machines[node].ifaces[iface].Descr
- If !(iface = 1 && machines[node].ifaces.MaxIndex() = 1) ; Forbid to remove the last existing iface.
- Gui, Add, Button, x+0 w16 h16 vn%node%DelIf%iface% gDeleteIf, -
- Gui, Add, Button, x+0 w16 h16 vn%node%AddIf%iface% gAddIf, +
- Gui, Add, Text, x%margin% y+0, Net:
- Gui, Add, Edit, x+0 w131 h16 vn%node%If%iface%Net, % machines[node].ifaces[iface].Net
- For addr In machines[node].ifaces[iface].addrs
- {
- Gui, Add, Text, x%margin% y+0, Addr%addr%:
- Gui, Add, Edit, x+5 w89 h16 vn%node%If%iface%a%addr%addr, % machines[node].ifaces[iface].addrs[addr].IP
- Gui, Add, Text, x+0, /
- Gui, Add, Edit, x+0 w21 h16 vn%node%if%iface%a%addr%mask, % machines[node].ifaces[iface].addrs[addr].mask
- If !(addr = 1 && machines[node].ifaces[iface].addrs.MaxIndex() = 1) ; Forbid to remove the last existing addr.
- Gui, Add, Button, x+0 w16 h16 vn%node%If%iface%DelA%addr% gDeleteAddr, -
- Gui, Add, Button, x+0 w16 h16 vn%node%If%iface%AddA%addr% gAddAddr, +
- }
- }
- }
- Return
- Open:
- FileSelectFile, input,,, Select a file with your saved stand, Stand (*.json)
- If (!ErrorLevel && input)
- {
- FileRead, input, %input%
- If input
- {
- Try
- machines := JSON.parse(input)
- Catch E
- MsgBox, % e
- }
- GoSub, Repaint
- }
- Return
- Save:
- FileSelectFile, output, S, % (output ? output : A_WorkingDir) "\" A_Now ".json", Where to save this stand to?
- If (!ErrorLevel && output)
- {
- jsoned := JSON.stringify(machines)
- If FileExist(output)
- FileDelete, %output%
- FileAppend, %jsoned%, %output%, UTF-8
- ; GoSub, Repaint
- }
- Return
- ParseTextJSON:
- Gui, InputJSON: New
- Gui, Add, Text,, Paste your JSON'ified stand:
- Gui, Add, Edit, w300 h250 vinputJSON
- Gui, Add, Button, x250, Parse
- Gui, InputJSON: Show
- Gui, 1: Default
- Return
- Repaint:
- Gui, Destroy
- GoSub, DrawGUI
- Return
- DeleteNode:
- machines.Remove(SubStr(A_GuiControl, 5))
- GoSub, Repaint
- Return
- AddNode:
- RegExMatch(A_GuiControl, "Si)^addN(\d+)$", this)
- ; machines.Insert(this1+1, machines[machines.MaxIndex()]) ; Duplicate
- machines.Insert(this1+1, {"name": "", "defroute": "", "ifaces":[ {"name":"","type":"","addrs":[ {"IP": "", "mask": ""} ] } ] })
- GoSub, Repaint
- Return
- DeleteIf:
- RegExMatch(A_GuiControl, "Si)^n(\d+)DelIf(\d+)$", this)
- machines[this1].ifaces.Remove(this2)
- GoSub, Repaint
- Return
- AddIf:
- RegExMatch(A_GuiControl, "Si)^n(\d+)AddIf(\d+)$", this)
- ; machines[this1].ifaces.Insert(this2+1, machines[this1].ifaces[machines[this1].ifaces.MaxIndex()]) ; Duplicate
- machines[this1].ifaces.Insert(this2+1, {"name":"","type":"","addrs":[ {"IP": "", "mask": ""} ] })
- GoSub, Repaint
- Return
- DeleteAddr:
- RegExMatch(A_GuiControl, "Si)^n(\d+)If(\d+)DelA(\d+)$", this)
- machines[this1].ifaces[this2].addrs.Remove(this3)
- GoSub, Repaint
- Return
- AddAddr:
- RegExMatch(A_GuiControl, "Si)^n(\d+)If(\d+)AddA(\d+)$", this)
- ; machines[this1].ifaces[this2].addrs.Insert(this3+1, machines[this1].ifaces[this2].addrs[machines[this1].ifaces[this2].addrs.MaxIndex()]) ; Duplicate
- machines[this1].ifaces[this2].addrs.Insert(this3+1, {"IP": "", "mask": ""})
- GoSub, Repaint
- Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement