5 -- retun table {mode(dhcp,static), ip, netmask, broadcast, gw, dns}
8 local _t
= myread("/etc/rc.conf")
9 _
,_
,_s
= string.find(_t
, "\neth0=(.-)\n")
10 if (string.find(_s
, "dhcp")) then
12 _s
= myexec("ifconfig eth0")
13 string.gsub(_s
, "inet addr:(.-)%s*Bcast:(.-)%s*Mask:(.-)\n", function(ip
,bc
,nm
)
16 _tbl
["broadcast"] = bc
18 _s
= myexec("route -n")
19 _
,_
,_s
= string.find(_s
, "\n0.0.0.0%s*\t*(.-)%s*\t*0.0.0.0")
21 _s
= myread("/etc/resolv.conf")
23 string.gsub(_s
, "nameserver%s+(.-)\n", function(v
)
24 _tbl
["dns"] = _tbl
["dns"].." "..v
29 _tbl
["mode"] = "static"
30 string.gsub(_s
, "eth%d%s*(.-)%s*netmask%s*(%S+%d)", function(ip
,nm
)
33 -- _tbl["broadcast"] = bc
35 _
,_
,_s
= string.find(_t
, "\ngateway=\"default gw (.-)\"\n")
36 if _s
then _tbl
["gateway"]=_s
else _tbl
["gateway"]="" end
38 _s
= myread("/etc/resolv.conf")
39 string.gsub(_s
, "nameserver%s*(%S+)", function(v
)
40 _tbl
["dns"] = v
.." ".._tbl
["dns"]
46 function set_network(tbl
)
47 local _current_net_tbl
= get_network()
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 /usr/bin/killall dhcpcd; sudo /sbin/dhcpcd eth0")
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 /sbin/ifconfig ".._ip
)
61 myexec("sudo /sbin/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")
67 mywrite("/etc/resolv.conf", _s
)
79 if b
.mode
== "dhcp" then
80 r_dhcp
= "dhcp checked"
84 r_static
= "static checked"
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>
99 <td class=color_table_heading >IP 地址</td><td class=color_table_row1 >
100 <input type=text name="ip" value=]]
104 <tr><td class=color_table_heading>子网掩码</td>
105 <td class=color_table_row2>
106 <input type=text name="netmask" value=
111 <tr><td class=color_table_heading>默认网关</td>
112 <td class=color_table_row1>
113 <input type=text name="gateway" value=
118 <tr><td class=color_table_heading>DNS 服务器</td>
119 <td class=color_table_row2>
120 <input type=text name="dns" value=
126 <input type=hidden name="todo" value=todo>
127 <input type=submit value="提交">