• Linux Shell Script 如何取得外部IP, 排程及傳回值
  • 推文至Facebook  
  • Linux 取得外部 IP 地址

    以下會使用 curl 擷取顯示 IP 地址的網頁, 如果沒有安裝, 可以用 yum apt-get 安裝:

    RHEL / CentOS / Fedora:

    # yum install curl


    Debian / Ubuntu / Mint:

    # apt-get install curl


    安裝完成後, 就可以透過 curl 取得外部 IP 地址, 以下指令都可以取得對外 IP, 可以在指令模式執行, 或者放到 Shell Script 也可以:
     

    # curl ident.me

    # curl ifconfig.me

    # curl tnx.nl/ip

    # curl icanhazip.com

    # curl ipecho.net/plain

    # curl ip.appspot.com

    # curl whatsmyip.akamai.com


    1. 排程
    crontab -e   #編輯該使用者的 crontab 指令


    2. Cron 語法介紹



     

    依序是 分鐘,   小時,   日期,   月份,    星期,   command

    參數為 0-59,   0-23,   1-31,  1-21,   0-6,   需要執行的command

     

    星期參數為 0 代表星期日

    *】:星號,代表任何時刻都接受的意思

    ,】:逗號,代表分隔時段。例如:30 9,17 * * * command,代表早上 9 點半和下午五點半都執行 command

    -】:減號,代表一段時間範圍。例如:15 9-12 * * * command,代表從 9 點到 12 點的每個 15 分都執行 command

    /n】:斜線,n 代表數字,表示每個 n 單位間隔。例如:*/5 * * * * command,代表每隔 5 分鐘執行一次 command

     

    範例:
    vi /root/callback.sh

    curl "http://abc.com/xxx.php?weburl=14D&webip=$(curl ident.me)"


    傳到xxx.php檔案

    參數:
    weburl=14D #別名
    webip=$(curl ident.me) #外部連線IP

    參考文章
    https://jqnets.com/blog/ubuntu-% ... %E6%95%99%E5%AD%B8/
    https://www.ltsplus.com/linux/linux-get-external-ip-address

  • 標籤:Linux, Shell, Script, 排程,
NetYea 客服 NetYea 客服
NetYea 客服

Linux Shell Script 如何取得外部IP, 排程及傳回值 -