sh 仅使用Bash内置函数在网页更新时发送IFTTT通知

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 仅使用Bash内置函数在网页更新时发送IFTTT通知相关的知识,希望对你有一定的参考价值。

#!/bin/bash

declare -r CHECK_HOST=www.mtgox.com
declare -r CHECK_RESOURCE=/

declare -r IFTTT_EVENT=MtGOX
declare -r IFTTT_KEY=<CHANGE ME>
declare -r IFTTT_HOST=maker.ifttt.com
declare -r IFTT_RESOURCE=/trigger/${IFTTT_EVENT}/with/key/${IFTTT_KEY}
declare -r IFTT_REQUEST="HEAD ${IFTT_RESOURCE} HTTP/1.1\r\nHost: ${IFTTT_HOST}\r\nConnection: Close\r\n\r\n"

declare -r DEBUG=off
declare -r -i SLEEP=300

declare LAST_MODIFIED

echo_debug() {
    [[ "$DEBUG" == "on" ]] && echo -e "$@"
}

get_Last-Modified() {
    local -r REQUEST="HEAD ${CHECK_RESOURCE} HTTP/1.1\r\nHost: ${CHECK_HOST}\r\nConnection: Close\r\n\r\n"
    LAST_MODIFIED=""

    printf "%(%F %T)T Getting Last-Modified date\n"
    echo_debug "\nRequest:\n$REQUEST"
    exec {RESOURCE}<>"/dev/tcp/${CHECK_HOST}/80"
    echo -e $REQUEST >&$RESOURCE
    read -u $RESOURCE RESPONSE
    echo_debug "\nResponse:\n${RESPONSE}"

    # check for HTTP OK status
    if [[ $RESPONSE =~ "200 OK" ]] ; then
        while read -u $RESOURCE -a RESPONSE ; do
            echo_debug "${RESPONSE[@]}"
            [[ ${RESPONSE[0]} =~ "Last-Modified:" ]] && LAST_MODIFIED=${RESPONSE[@]:1}
        done
    fi
    exec {RESOURCE}>&-

    if [[ -z "$LAST_MODIFIED" ]] ; then
        printf "%(%F %T)T Webpage does not seem to have a Last-Modified date\n"
    else
        printf "%(%F %T)T Got Last-Modified date ${LAST_MODIFIED}\n"
    fi
}

check_If-Modified() {
    local -r REQUEST="HEAD ${CHECK_RESOURCE} HTTP/1.1\r\nHost: ${CHECK_HOST}\r\nIf-Modified-Since: ${LAST_MODIFIED}\r\nConnection: Close\r\n\r\n"
    local -i RET=1

    printf "%(%F %T)T Checking If-Modified-Since $LAST_MODIFIED\n"
    echo_debug "\nRequest:\n$REQUEST"
    exec {RESOURCE}<>"/dev/tcp/${CHECK_HOST}/80"
    echo -e $REQUEST >&$RESOURCE
    read -u $RESOURCE RESPONSE
    exec {RESOURCE}>&-

    echo_debug "\nResponse:\n${RESPONSE}"

    # check for HTTP Not Modified or OK status
    if [[ $RESPONSE =~ "304 Not Modified" ]] ; then
        printf "%(%F %T)T Webpage has not been modified since $LAST_MODIFIED\n"
    elif [[ $RESPONSE =~ "200 OK" ]] ; then
        printf "%(%F %T)T Webpage has been modified since $LAST_MODIFIED\n"
        LAST_MODIFIED=""
        RET=0
    fi

    return $RET
}

while true ; do
    get_Last-Modified
    while [[ ! -z "$LAST_MODIFIED" ]] ; do
        read -t $SLEEP -N 0
        if check_If-Modified ; then
            printf "%(%F %T)T Triggering IFTTT notification"
            echo_debug "\nRequest:\n${IFTT_REQUEST}"
            exec {RESOURCE}<>"/dev/tcp/${IFTTT_HOST}\/80"
            echo -e $IFTT_REQUEST >&$RESOURCE
            read -u $RESOURCE RESPONSE
            exec {RESOURCE}>&-
            echo_debug "\nResponse:\n${RESPONSE}"
        fi
    done
    read -t $SLEEP -N 0
done

exit 0

以上是关于sh 仅使用Bash内置函数在网页更新时发送IFTTT通知的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习总结(70)——Bash 脚本中常用的内置变量汇总

学习过程遇到问题的解决方法

sh 仅使用wget仅下载google驱动器文件wget在bash命令行下载谷歌网盘(狗哥网盘)上的文件

shell 脚本中常用的内置变量

bash 中的访问修饰符

sh 一个非常基本的BASH脚本,当一个新的在线上传租约符合我的期望时,它会自动通过电子邮件发送给我。在2015年真正的estat