[ UP ] update cgi's manager_user.lua
[archserver.git] / cgi / system / network.lua
blobc97cda1e345442441c2121ac7e9d386e12807873
1 #!/usr/bin/lua
3 dofile "libutil.lua"
5 -- retun table {mode(dhcp,static), ip, netmask, broadcast, gw, dns}
6 function get_network()
7 local _tbl = {}
8 local _t = myread("/etc/rc.conf")
9 _,_,_s = string.find(_t, "\neth0=(.-)\n")
10 if (string.find(_s, "dhcp")) then
11 _tbl["mode"] = "dhcp"
12 _s = myexec("ifconfig eth0")
13 string.gsub(_s, "inet addr:(.-)%s*Bcast:(.-)%s*Mask:(.-)\n", function(ip,bc,nm)
14 _tbl["ip"] = ip
15 _tbl["netmask"] = nm
16 _tbl["broadcast"] = bc
17 end)
18 _s = myexec("route -n")
19 _,_,_s = string.find(_s, "\n0.0.0.0%s*\t*(.-)%s*\t*0.0.0.0")
20 _tbl["gateway"] = _s
21 _s = myread("/etc/resolv.conf")
22 _tbl["dns"]=""
23 string.gsub(_s, "nameserver%s+(.-)\n", function(v)
24 _tbl["dns"] = _tbl["dns"].." "..v
25 end)
28 else
29 _tbl["mode"] = "static"
30 string.gsub(_s, "eth%d%s*(.-)%s*netmask%s*(%S+%d)", function(ip,nm)
31 _tbl["ip"] = ip
32 _tbl["netmask"] = nm
33 -- _tbl["broadcast"] = bc
34 end)
35 _,_,_s = string.find(_t, "\ngateway=\"default gw (.-)\"\n")
36 if _s then _tbl["gateway"]=_s else _tbl["gateway"]="" end
37 _tbl["dns"]=""
38 _s = myread("/etc/resolv.conf")
39 string.gsub(_s, "nameserver%s*(%S+)", function(v)
40 _tbl["dns"] = v.." ".._tbl["dns"]
41 end )
42 end
43 return _tbl
44 end
46 function set_network(tbl)
47 local _current_net_tbl = get_network()
48 local _t = tbl
49 if (_t.mode == _current_net_tbl.mode) then print("equal mode") return end
50 if _t.mode == "dhcp" then
51 file_gsub("/etc/rc.conf", "(\neth0=.-\n)", "\neth0=\"dhcp\"\n")
52 file_gsub("/etc/rc.conf", "(\ngateway=)", "\n#gateway=")
53 myexec("sudo killall dhcpcd; sudo dhcpcd eth0")
54 else
55 -- local _ip= "eth0 ".._t.ip.." netmask ".._t.netmask.." broadcast ".._t.broadcast
56 local _ip= "eth0 ".._t.ip.." netmask ".._t.netmask
57 local _gw= "default gw ".._t.gateway
58 file_gsub("/etc/rc.conf", "(\neth0=.-\n)", "\neth0=\"".._ip.."\"\n")
59 file_gsub("/etc/rc.conf", "(\n#gateway.-\n)", "\ngateway=\"".._gw.."\"\n")
60 myexec("sudo ifconfig ".._ip)
61 myexec("sudo route add -net ".._gw)
62 local _s = myread("/etc/resolv.conf")
63 _s = string.gsub(_s, "nameserver .-\n", "")
64 for _dns in string.gmatch(_t.dns, "%S+") do
65 _s = string.gsub(_s, "$", "nameserver ".._dns.."\n")
66 end
67 mywrite("/etc/resolv.conf", _s)
68 end
70 end
72 local qp = ui_getqp()
73 local b = {}
75 if qp.todo then
76 set_network(qp)
77 else
78 b = get_network()
79 if b.mode == "dhcp" then
80 r_dhcp = "dhcp checked"
81 r_static= "static"
82 else
83 r_dhcp = "dhcp"
84 r_static= "static checked"
85 end
87 print([[<h3>网络设置</h3>
88 <form action="system_network.cgi">
89 <div class="table_input_body">
91 <table cellspacing="2" cellpadding="8" border="0" width="100%">
93 <tr><td class=color_table_heading>Mode</td>
94 <td class=color_table_row2>
95 <input type=radio align=left name="mode" value=]]..r_dhcp..[[>自动获得 IP 地址
96 <input type=radio name="mode" value=]]..r_static..[[>使用下面的 IP 地址</td></tr>
98 <tr>
99 <td class=color_table_heading >IP 地址</td><td class=color_table_row1 >
100 <input type=text name="ip" value=]]
101 ..b.ip..
103 [[></td></tr>
104 <tr><td class=color_table_heading>子网掩码</td>
105 <td class=color_table_row2>
106 <input type=text name="netmask" value=
108 ..b.netmask..
110 [[></td></tr>
111 <tr><td class=color_table_heading>默认网关</td>
112 <td class=color_table_row1>
113 <input type=text name="gateway" value=
115 ..b.gateway..
117 [[></td></tr>
118 <tr><td class=color_table_heading>DNS 服务器</td>
119 <td class=color_table_row2>
120 <input type=text name="dns" value=
122 ..b.dns..
124 [[></td></tr>
125 </table></div>
126 <input type=hidden name="todo" value=todo>
127 <input type=submit value="提交">
128 </form>]])