1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        樹(shù)莓派4B動(dòng)態(tài)解析公網(wǎng)IP

        共 4531字,需瀏覽 10分鐘

         ·

        2021-10-19 06:50

        ????????因?yàn)橐婚_(kāi)始采用的是Frp內(nèi)網(wǎng)穿透技術(shù),走的阿里云服務(wù)器的通道,所以帶寬最大只支持1M,導(dǎo)致我本地服務(wù)器的流媒體服務(wù)訪問(wèn)人數(shù)一多就會(huì)引起觀影卡頓,所以只能更換為DDNS技術(shù)來(lái)動(dòng)態(tài)解析本地的公網(wǎng)IP,共試了兩種方法,第一種方法我本地測(cè)試有點(diǎn)問(wèn)題,后續(xù)換為了第二種方法,在此記錄。

        一、自定義Python腳本 + 阿里云SDK 實(shí)現(xiàn)DDNS

        1. 安裝庫(kù)

          pip install aliyun-python-sdk-core
          pip install aliyun-python-sdk-alidns
          pip install pyyaml
        2. 新建目錄

          cd /
          cd urs
          mkdir ddns
        3. 新建Py腳本

          sudo nano aliddns.py
          import os
          import re
          import time
          import json
          import platform
          import requests
          from settings import * # 所有需要修改的數(shù)據(jù)
          from aliyunsdkcore.client import AcsClient
          from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest

          def LocalIPv6Address():
          ? ?# 判斷當(dāng)前操作系統(tǒng),使用命令
          ? ?if(platform.system() == 'Windows'):
          ? ? ? ?cmd = 'ipconfig'
          ? ?elif(platform.system() == 'Linux'):
          ? ? ? ?cmd = 'ifconfig'
          ? ?else:
          ? ? ? ?print("操作系統(tǒng)不兼容")
          ? ? ? ?exit()
          ? ?v6_pattern = re.compile(r"(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})")
          ? ?r = os.popen(cmd)
          ? ?text = r.read()
          ? ?r.close()
          ? ?v6_address = v6_pattern.findall(text)
          ? ?return v6_address[0][0]


          def AliDNSUpdate(v6_address):
          ? ?client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')

          ? ?request = AddDomainRecordRequest()
          ? ?request.set_accept_format('json')

          ? ?request.set_Value(v6_address)
          ? ?request.set_Type("AAAA")
          ? ?request.set_RR(RR)
          ? ?request.set_DomainName(DomainName)

          ? ?try:
          ? ? ? ?response = client.do_action_with_exception(request)
          ? ? ? ?response = str(response, encoding='utf-8')
          ? ?except:
          ? ? ? ?response = '{"ERROR":"error"}'
          ? ?return response

          # 如果更新失敗,返回False,否則返回True
          def JudgeResponse(r):
          ? ?data = json.loads(r)
          ? ?if "ERROR" in data:
          ? ? ? ?return False
          ? ?return True


          def WriteLog(msg, address):
          ? ?data = json.loads(msg)
          ? ?now_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
          ? ?with open(LOGFILE, 'w+', encoding='utf-8') as f:
          ? ? ? ?f.write(now_time+'\t'+address+'\n')
          ? ? ? ?for key in data:
          ? ? ? ? ? ?value = data[key]
          ? ? ? ? ? ?line = '\t' + str(key) + '\t:\t' + str(value) + '\n'
          ? ? ? ? ? ?f.write(line)
          ? ? ? ?f.close()


          if __name__ == '__main__':
          ? ?pre_address = ''
          ? ?while True:
          ? ? ? ?v6_address = LocalIPv6Address()
          ? ? ? ?if v6_address != pre_address:
          ? ? ? ? ? ?r = AliDNSUpdate(v6_address)
          ? ? ? ? ? ?WriteLog(r, v6_address)
          ? ? ? ? ? ?if not JudgeResponse(r):
          ? ? ? ? ? ? ? ?break
          ? ? ? ? ? ?pre_address = v6_address
          ? ? ? ?time.sleep(60)
        4. 運(yùn)行腳本

          python3 aliddns.py


        二、開(kāi)源工具Features實(shí)現(xiàn)DDNS

        網(wǎng)址:https://ddns.newfuture.cc/

        Git: https://github.com/NewFuture/DDNS/issues?page=2&q=is%3Aissue+is%3Aopen

        1. 安裝工具

          sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple ddns
        2. 新建目錄

          cd /
          cd urs
          mkdir ddns
        3. 運(yùn)行

          ddns
        • 此時(shí)會(huì)默認(rèn)在此目錄下生成 config.json 文件

        • 若想指定目錄生成需帶參執(zhí)行 例:ddns -c path/to/config.json

        修改配置文件

        {
        "$schema": "https://ddns.newfuture.cc/schema/v2.8.json",
        "debug": false,
        "dns": "alidns",
        "id": "阿里云ID",
        "index4": "public",
        "index6": "default",
        "ipv4": [
        "域名"
        ],
        "ipv6": [
        "域名"
        ],
        "proxy": null,
        "token": "阿里云Secret"
        "ttl": null
        }

        執(zhí)行

        ddns
        • 此時(shí)將會(huì)獲取本地公網(wǎng)IP,然后解析域名


        三、設(shè)置cron定時(shí)任務(wù)

        1. 查看任務(wù)列表

          crontab -l
        2. 編輯/添加 任務(wù)

          crontab -e
        3. 查看定時(shí)服務(wù)狀態(tài)

          service cron status
        4. 啟動(dòng)定時(shí)服務(wù)

          service cron start
        5. 重啟定時(shí)服務(wù)

          service cron restart

          // or

          sudo /etc/init.d/cron restart
        6. 重載定時(shí)配置

          service cron reload
        7. 終止定時(shí)服務(wù)

          service cron stop
        8. 定時(shí)任務(wù)規(guī)則

          # 0 和 7 都代表星期日
          0 - Sun Sunday
          1 - Mon Monday
          2 - Tue Tuesday
          3 - Wed Wednesday
          4 - Thu Thursday
          5 - Fri Friday
          6 - Sat Saturday
          7 - Sun Sunday

          # 總體格式
          ┌────────── minute (0 - 59)
          │ ┌──────── hour (0 - 23)
          │ │ ┌────── day of month (1 - 31)
          │ │ │ ┌──── month (1 - 12)
          │ │ │ │ ┌── day of week (0 - 6 => Sunday - Saturday, or
          │ │ │ │ │ 1 - 7 => Monday - Sunday)
          ↓ ↓ ↓ ↓ ↓
          * * * * * command to be executed

          # 舉例
          30 21 * * * /usr/local/etc/rc.d/lighttpd restart
          #每晚的21:30重啟apache

          45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
          #每月1、10、22日的4 : 45重啟apache

          10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
          #每周六、周日的1 : 10重啟apache

          0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
          #在每天18 : 00至23 : 00之間每隔30分鐘重啟apache

          0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
          #每星期六的11 : 00 pm重啟apache

          * */1 * * * /usr/local/etc/rc.d/lighttpd restart
          #每一小時(shí)重啟apache

          * 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
          #晚上11點(diǎn)到早上7點(diǎn)之間,每隔一小時(shí)重啟apache

          0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
          #每月的4號(hào)與每周一到周三的11點(diǎn)重啟apache

          0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
          #一月一號(hào)的4點(diǎn)重啟apache
        9. 為ddns添加每分鐘定時(shí)任務(wù)

          */1 * * * * cd /usr/ddns && /home/pi/.local/bin/ddns
          */1 * * * * echo "ddns解析"
        • 注意:在cron中使用命令必須用全路徑,查看全路徑方法 which ddns

        錯(cuò)誤排查

        • 若是提示返回信息中顯示 info (No MTA installed, discarding output) ,可安裝 sudo apt-get install postfix

        • 查看郵件命令 cat /var/mail/pi

        • 修改命令后記得重啟cron服務(wù)

        • 若是日志出現(xiàn) Skipping @reboot jobs -- not system startup,將 /var/run/crond.reboot 刪除,然后重啟cron服務(wù)


        瀏覽 129
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            好屌色网站 | 91人妻人人澡人人爽人人精品 | 操黄片| 亚洲 欧美 日韩 偷 妻 乱 | 欧美在线不卡视频 | 9l视频自拍9l视频自拍九色学生 | 日本三级韩国三级久久爱 | 性猛交xxxxx乱大交孕妇 | 青娱乐在线精品 | 惩罚哭喊抽打花蒂疼痛蹂躏漫画人 |