网关及DNS配置(网关,DNS)

小编:芯水 更新时间:2022-05-03

--lua脚本还有比较好用的功能,就是单独使用的脚本。相比shell会比较好看一些。特别是还有强大的字符串处理 ,会C的基本很容易看懂

app={}

app.ip = "192.168.8.156" --目标IP 这里将网关和DNS全部配置相同的一个IP

app.dns_file = "/etc/resolv.conf"

function dns_is_ok(ip,file)


local f = io.open(file, "r")

if f == nil then

return nil

end

local txt = f:read("*all")

local dest_dns = string.format("nameserver %s",ip)

f:close()

print("dns_is_ok")

print(txt)

pos = string.find(txt,dest_dns,1)

return pos

end

function set_network(ip)

local del_cmd = string.format("route del default")

local dest_cmd = string.format("route add default gw %s",ip)

local dns_set_cmd = string.format("echo \"nameserver %s\" > %s",ip, app.dns_file)


print(del_cmd)

os.execute(del_cmd)


print(dest_cmd)

os.execute(dest_cmd)


local pos = dns_is_ok(ip,app.dns_file)


if pos==nil then

print("set dns"..ip)

os.execute(dns_set_cmd)

end

end

set_network(app.ip)