V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
OpenWrt 是一个专门面向嵌入式设备的 Linux 发行版。你可以将 OpenWrt 支持的型号的嵌入式设备,比如各种路由器上的系统,换成一个有更多可能性可以折腾的 Linux 系统。
OpenWrt 官方网站
Kaiyuan
V2EX  ›  OpenWrt

两个路由互发心跳包自动改 hosts 怎么实现好?

  •  
  •   Kaiyuan · Oct 8, 2020 · 881 views
    This topic created in 2028 days ago, the information mentioned may be changed or developed.

    就是隧道里保持相互通信的办法。

    我现在研究到的就是 shell 定时执行,然后 lua 接收然后修改 hosts 文件,但是有些坑。

    不知道是我固件问还是怎么,没法刷新 DNS,重启 dnsmasq 都没用。

    根据知乎的一个问题回答为基础实现 lua 接收 Post 。

    之前完全没接触过 lua,看着手册和 Google 弄了下面的代码。

    ##接收端

    /www/cgi-bin/edithosts

    #!/usr/bin/lua
    local edithosts= require 'edithosts'
    edithosts.Run()
    

    /usr/lib/lua/edithosts

    local edithosts= {}
    
    function edithosts.Run()
        local client = os.getenv("REMOTE_ADDR")
        local GET = os.getenv("QUERY_STRING")
        local POST = nil
        local Domain = nil
        local ifedit = "0"
        local hostsFile = "/www/edithosts"
        local hostsLog = "/www/edithosts.log"
        local POSTLength = tonumber(os.getenv("CONTENT_LENGTH")) or 0
        if (POSTLength > 0) then
            POST = io.read(POSTLength)
            Domain = string.sub(POST, 3)
        end
        
        if (Domain ~= nil) then
            -- 修改 Hosts
            local hostsMain = string.format("%s    %s", client or '-', Domain or '-')
            local hostsR = io.open(hostsFile ,"a+")
            local hostsOld = hostsR:read()
            hostsR:close()
            ifedit = "2"
            if (hostsMain ~= hostsOld) then
                -- 如果内容不一样则修改 hosts 文件
                local hostsIO = io.open(hostsFile, "w")
                hostsIO:write(hostsMain)
                hostsIO:close()
                ifedit = "1"
    
                -- 写入日志
                local logIO = io.open(hostsLog, "a")
                local nowTime = os.date("%Y-%m-%d %H:%M:%S")
                local logMain = string.format("%s - %s - %s\n", client or '-', Domain or '-', nowTime or '-')
                logIO:write(logMain)
                logIO:close()
    
                -- 重启 dnsmasq 
                local redns = os.execute("/etc/init.d/dnsmasq restart")
               
            end
        end
    
        io.write("Content-type: text/html\nPragma: no-cache\n\n")
        io.write(ifedit)
    end
    
    return edithosts
    
    

    ##发送请求

    */5 * * * * /usr/bin/postroom.sh
    

    br-wan 是网口

    /www/oldip 文件要预先创建

    1.room 和 2.room 是自定义的域名

    #!/bin/bash
    interface=br-wan
    NEW_IP=$(ip a show dev $interface |grep -oP "inet [0-9]+.[0-9]+.[0-9]+.[0-9]+" | sed 's/inet //g')
    NOWTIME=`date`
    IP_FILE='/www/oldip'
    CURRENT_IP=`cat /www/oldip`
    LOG_FILE="/www/post.log"
    if [ ${NEW_IP} == ${CURRENT_IP} ] || [ ! ${NEW_IP} ]; then
        echo ""
    else
        THIS_LOG=`curl -X POST --data 'd=1.room' 2.room:7080/cgi-bin/edithosts`
        echo ${NOWTIME} >> ${LOG_FILE}
        if [ ${THIS_LOG} ]; then
        	echo "TR069 OK" ${NEW_IP} >> ${LOG_FILE}
    		echo "" >> ${LOG_FILE}
    		echo ${NEW_IP} > ${IP_FILE}
    	else
    		echo "Error " ${THIS_LOG} >> ${LOG_FILE}
        fi
    fi
    

    该如何完善这个功能呢?

    Supplement 1  ·  Oct 12, 2020

    知道为什么有个路由的本地 hosts 文件无效了,因为我勾选了 DHCP/DNS 中的忽略解析文件,这个不能勾! DHCP/DNS

    4 replies    2020-10-21 16:29:08 +08:00
    LGA1150
        1
    LGA1150  
       Oct 9, 2020   ❤️ 1
    接口上线时有 hotplug 事件触发,无需定时轮询
    hotplug 脚本在 /etc/hotplug.d/iface/
    再通过 SSH 远程执行命令(需要密钥验证)

    至于 DNS 缓存问题,路由器上是否有其他 DNS 代理?如 Adguard home,pdnsd

    不过要是两个路由器同时重启了,就保持不了了
    最好还是有一个固定 IP 的服务器,用 frp 或 SSH 穿透
    Kaiyuan
        2
    Kaiyuan  
    OP
       Oct 9, 2020
    @LGA1150 #1 这种功能不是在外网的环境使用,是纯内部环境下非固定 IP 的情况。
    有外网就直接 DDNS 方便多了。
    fkmc
        3
    fkmc  
       Oct 21, 2020
    @Kaiyuan #2 内网 ip 也可以 ddns 可以获取内网 ip
    Kaiyuan
        4
    Kaiyuan  
    OP
       Oct 21, 2020 via iPhone
    @wccc 是完全纯内网,完全无外网的情况。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2831 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 15:45 · PVG 23:45 · LAX 08:45 · JFK 11:45
    ♥ Do have faith in what you're doing.