监控页面中关键词,有则推送消息

一个检测页面中关键词的脚本,比如检测vps是否有货,然后通过pushbullet推送到设备上。
之前也写过类似的脚本,不过那个对消息推送的时间没有做设置,也就是循环一次 如果匹配到关键字就推送消息,这样的话如果推送app有条数限制的话,可能很快用完。

<?php

define("Token", "********************************");	//pushbullet Tokens
$extime = 0;
$url = "https://xxxx.com/xxxx.xxx";	//地址
$curl_sleep = 10;	//curl多久执行一次
$delay = 3600;  //多久发一次消息
$findstr = "xxxxxxx";	//网页中的关键词
$i = 1;

function curl_get($url)
{
        $UGA="AdsBot-Google v3 ( http://www.google.com/adsbot.html)";
        $ch = curl_init();
        $headers = array();

        $headers[] = "Accept: */*";
        $headers[] = "Cache-Control: max-age=0";
        $headers[] = "Connection: keep-alive";
        $headers[] = "Keep-Alive: 300";
        $headers[] = "Cookie: caches=";
        $headers[] = "Accept-Charset: utf-8;ISO-8859-1;iso-8859-2;q=0.7,*;q=0.7";
        $headers[] = "Accept-Language: en-us,en;q=0.5";
        $headers[] = "Pragma: "; // browsers keep this blank.
        curl_setopt($ch, CURLOPT_USERAGENT, $UGA);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT,40);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $datas=curl_exec($ch);

        curl_close($ch);
        return $datas;
}


function pushmsg($msg)
{
        $curl = curl_init('https://api.pushbullet.com/v2/pushes');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ['Access-Token: '.Token]);
        curl_setopt($curl, CURLOPT_POSTFIELDS, ["type" => "link", "title" => "-=-提醒-=-", "body" => "xeovo-azure上货!", "url" => $msg]);
        $response = curl_exec($curl);
        global $extime;
        $extime = date("YmdHis");
        echo "send a message:".$extime."...\n";
//      print_r($response);
}


while(TRUE)
{
        $stime = microtime(true);
        $xxxx = curl_get($url);
        $etime = microtime(true);
        $total = $etime - $stime;
        echo "excute time:".$total."...num=".$i.".----extime=".$extime."\n";
        if(stristr($xxxx,$findstr))
        {
                $tmptme = date("YmdHis") - $extime;
                echo "tmptime=".$tmptme."\n";
                if($extime ==0 || $tmptme > $delay )
                {
                        pushmsg($url);
                }

        }
        $i++;
        sleep($curl_sleep);
        $stime = 0;
}



?>

保存为/xxxx/xxxx.php,第一次最好用screen -S xxxx
然后,php /xxxx/xxxx.php > /xxxx/log.txt &
之后,在rc.local 写入 php /xxxx/xxxx.php > /xxxx/log.txt &
下次重启即可自动启动
以前写的检测Kimsufi是否有货的脚本

上一篇
下一篇