讲师中心 微信公众号
AI工具推荐 视频效率加速

服务器检控代码并实现给手机发送短信

老敏君_6181

老敏君_6181

发布时间:2016-05-25 17:10:13

|

2618人浏览过

|

来源于php中文网

原创

服务器检控代码并实现给手机发送短信           

一个白痴类型服务器检控代码,写的非常粗糙,放到这里备忘把。用服务器B检控服务器A,服务器B ping 服务器A,查看返回结果,如果有异常发送给139邮箱,在139邮箱上设置--手机通知--邮件到达通知--查短信,这样只要服务器有问题手机就能收到短信; 
弊端&不足:139邮箱收到邮件后向手机发送短信会有一定延时;必须保证服务器B稳定。

<?php
 /**
    * 服务器检控
    *   -- 如果ping不同或者丢包情况给139邮箱发送邮件,139邮箱设置手机接收邮件项。
    * @package: 
    * @Files: pingServer.php
    * @Author: 林涛
    * @Date: 2012-8-27 上午11:37:15
    * @Copyright: http://www.iamlintao.com
    * @todo:
    *
    * -----Remarks--------  
    *   
    * --------------------
    */
  
    // 这里是你要执行的代码
         
    exec("ping -c 4 这里写被监控服务器的IP",$rArr);       // 执行Ping
    $content = array_reverse($rArr);    // 翻转数组
         
    $msg_1 = $content[0];   // rtt min/avg/max/mdev = 3.743/3.905/4.314/0.248 ms
    $msg_2 = $content[1];   // 4 packets transmitted, 4 received, 0% packet loss, time 3003ms   
    $findme   = 'received, 0% packet loss'; // 查找字符串
     
    $pos = strpos($msg_2, $findme);
     
    if ($pos === false) {   // 不存在
         
        $smtpserver="smtp.163.com";
        $port =25;                  //smtp服务器的端口,一般是 25
        $smtpuser = ""; //您登录smtp服务器的用户名
        $smtppwd = "";      //您登录smtp服务器的密码
        $mailtype = "TXT";          //邮件的类型,可选值是 TXT 或 HTML ,TXT 表示是纯文本的邮件,
        HTML 表示是 html格式的邮件
        $sender = "";   //发件人,一般要与您登录smtp服务器的用户名($smtpuser)相同,
        否则可能会因为smtp服务器的设置导致发送失败
        $smtp = new smtp($smtpserver,$port,true,$smtpuser,$smtppwd,$sender);
        $smtp->debug = true;             //是否开启调试,只在测试程序时使用,正式使用时请将此行注释
        $to = "";   //收件人,建议填写139邮箱
        $dtime = date("Y-m-d H:i:s");
             
        $subject = "Abnormal:$dtime $msg_2 ";   // 标题
        $body = "Abnormal:
        $dtime
        $msg_1
                     
        ";  // 正文
             
        $subject = iconv("UTF-8", "GB2312", $subject);  // 转码
        $body = iconv("UTF-8", "GB2312", $body);        // 转码
             
        $send=$smtp->sendmail($to,$sender,$subject,$body,$mailtype); // 发送
             
        if($send==1){
            //              echo "邮件发送成功";
        }else{
            //              echo "邮件发送失败<br/>";           
            }
                 
                 
    }else{  // 存在
         
    }
         
     
 /////////////////
  
 /**
  * 发送邮件类
  * @author Administrator
  *
  */
 class smtp{
     
    var $smtp_port;
    var $time_out;
    var $host_name;
    var $log_file;
    var $relay_host;
    var $debug;
    var $auth;
    var $user;
    var $pass;
    var $sock;
     
    function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass){
         
        $this->debug = true;
        $this->smtp_port = $smtp_port;
        $this->relay_host = $relay_host;
        $this->time_out = 30;
        $this->auth = $auth;
        $this->user = $user;
        $this->pass = $pass;
        $this->host_name = "localhost";
        $this->log_file ="";
        $this->sock = FALSE;
    }
     
     function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", 
     $additional_headers = ""){
         
            $mail_from = $this->get_address($this->strip_comment($from));
            $body = ereg_replace("(^|(
))(\.)", "\1.\3", $body);
            $header .= "MIME-Version:1.0
";
            if($mailtype=="HTML")
            {
                $header .= "Content-Type:text/html
";
            }
         $header .= "To: ".$to."
";
         if ($cc != "")
         {
            $header .= "Cc: ".$cc."
";
         }
         $header .= "From: $from<".$from.">
";
         $header .= "Subject: ".$subject."
";
         $header .= $additional_headers;
         $header .= "Date: ".date("r")."
";
         $header .= "X-Mailer:By Redhat (PHP/".phpversion().")
";
         list($msec, $sec) = explode(" ", microtime());
         $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">
";
         $TO = explode(",", $this->strip_comment($to));
     
         if ($cc != "") {
            $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));
         }
         if ($bcc != "") {
            $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));
         }
     
         $sent = TRUE;
         foreach ($TO as $rcpt_to) {
            $rcpt_to = $this->get_address($rcpt_to);
            if (!$this->smtp_sockopen($rcpt_to)) {
                $this->log_write("Error: Cannot send email to ".$rcpt_to."
");
                $sent = FALSE;
                continue;
            }
            if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
                $this->log_write("E-mail has been sent to <".$rcpt_to.">
");
            } else {
                $this->log_write("Error: Cannot send email to <".$rcpt_to.">
");
                $sent = FALSE;
            }
            //fclose($this->sock);
            $this->log_write("Disconnected from remote host
");
         }
         //echo "<br>";
         //echo $header;
         return $sent;
     }
 
    /* Private Functions */
 
     function smtp_send($helo, $from, $to, $header, $body = ""){
             
            if (!$this->smtp_putcmd("HELO", $helo)) {
                return $this->smtp_error("sending HELO command");
            }
            #auth
            if($this->auth){
                if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {
                return $this->smtp_error("sending HELO command");
            }
     
            if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
            return $this->smtp_error("sending HELO command");
        }
        }
        #
        if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {
        return $this->smtp_error("sending MAIL FROM command");
        }
     
        if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {
        return $this->smtp_error("sending RCPT TO command");
        }
     
        if (!$this->smtp_putcmd("DATA")) {
        return $this->smtp_error("sending DATA command");
        }
     
        if (!$this->smtp_message($header, $body)) {
        return $this->smtp_error("sending message");
        }
     
        if (!$this->smtp_eom()) {
        return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");
        }
     
        if (!$this->smtp_putcmd("QUIT")) {
            return $this->smtp_error("sending QUIT command");
        }
     
        return TRUE;
     }
 
     function smtp_sockopen($address){
         
        if ($this->relay_host == "") {
            return $this->smtp_sockopen_mx($address);
        } else {
            return $this->smtp_sockopen_relay();
        }
     }
 
     function smtp_sockopen_relay(){
         
        $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."
");
        $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);
        if (!($this->sock && $this->smtp_ok())) {
            $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."
");
            $this->log_write("Error: ".$errstr." (".$errno.")
");
            return FALSE;
        }
        $this->log_write("Connected to relay host ".$this->relay_host."
");
        return TRUE;;
     }
 
     function smtp_sockopen_mx($address){
         
        $domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
        if (!@getmxrr($domain, $MXHOSTS)) {
            $this->log_write("Error: Cannot resolve MX "".$domain.""
");
            return FALSE;
        }
        foreach ($MXHOSTS as $host) {
            $this->log_write("Trying to ".$host.":".$this->smtp_port."
");
            $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);
            if (!($this->sock && $this->smtp_ok())) {
                $this->log_write("Warning: Cannot connect to mx host ".$host."
");
                $this->log_write("Error: ".$errstr." (".$errno.")
");
                continue;
            }
            $this->log_write("Connected to mx host ".$host."
");
            return TRUE;
        }
        $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")
");
        return FALSE;
     }
 
     function smtp_message($header, $body){
         
        fputs($this->sock, $header."
".$body);
        $this->smtp_debug("> ".str_replace("
", "
"."> ", $header."
> ".$body."
> "));
     
        return TRUE;
     }
 
     function smtp_eom(){
         
        fputs($this->sock, "
.
");
        $this->smtp_debug(". [EOM]
");
     
        return $this->smtp_ok();
     }
 
     function smtp_ok(){
         
        $response = str_replace("
", "", fgets($this->sock, 512));
        $this->smtp_debug($response."
");
     
        if (!ereg("^[23]", $response)) {
            fputs($this->sock, "QUIT
");
            fgets($this->sock, 512);
            $this->log_write("Error: Remote host returned "".$response.""
");
            return FALSE;
        }
        return TRUE;
     }
 
     function smtp_putcmd($cmd, $arg = ""){
         
        if ($arg != "") {
            if($cmd=="") $cmd = $arg;
            else $cmd = $cmd." ".$arg;
        }
     
        fputs($this->sock, $cmd."
");
        $this->smtp_debug("> ".$cmd."
");
     
        return $this->smtp_ok();
     }
 
     function smtp_error($string){
         
        $this->log_write("Error: Error occurred while ".$string.".
");
        return FALSE;
     }
 
     function log_write($message){
         
        $this->smtp_debug($message);
     
        if ($this->log_file == "") {
            return TRUE;
        }
     
        $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;
        if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
            $this->smtp_debug("Warning: Cannot open log file "".$this->log_file.""
");
            return FALSE;
        }
        flock($fp, LOCK_EX);
        fputs($fp, $message);
        fclose($fp);
     
        return TRUE;
     }
 
     function strip_comment($address){
         
        $comment = "\([^()]*\)";
        while (ereg($comment, $address)) {
            $address = ereg_replace($comment, "", $address);
        }
     
        return $address;
     }
 
     function get_address($address){
         
        $address = ereg_replace("([ 	
])+", "", $address);
        $address = ereg_replace("^.*<(.+)>.*$", "\1", $address);
     
        return $address;
     }
 
     function smtp_debug($message){
         
        if ($this->debug) {
            //echo $message."<br>";
        }
     }
 
     function get_attach_type($image_tag) { //
     
        $filedata = array();
     
        $img_file_con=fopen($image_tag,"r");
        unset($image_data);
        while ($tem_buffer=AddSlashes(fread($img_file_con,filesize($image_tag))))
            $image_data.=$tem_buffer;
        fclose($img_file_con);
     
        $filedata['context'] = $image_data;
        $filedata['filename']= basename($image_tag);
        $extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
        switch($extension){
            case ".gif":
                $filedata['type'] = "image/gif";
                break;
            case ".gz":
                $filedata['type'] = "application/x-gzip";
                break;
            case ".htm":
                $filedata['type'] = "text/html";
                break;
            case ".html":
                $filedata['type'] = "text/html";
                break;
            case ".jpg":
                $filedata['type'] = "image/jpeg";
                break;
            case ".tar":
                $filedata['type'] = "application/x-tar";
                break;
            case ".txt":
                $filedata['type'] = "text/plain";
                break;
            case ".zip":
                $filedata['type'] = "application/zip";
                break;
            default:
                $filedata['type'] = "application/octet-stream";
                break;
        }
     
     
        return $filedata;
     }
 }

HTML5主机托管服务公司网站模板
HTML5主机托管服务公司网站模板

HTML5主机托管服务公司网站模板是一款从事主机托管服务公司宣传网站模板下载。提示:本模板调用到谷歌字体库,可能会出现页面打开比较缓慢。

下载

       

 以上就是服务器检控代码并实现给手机发送短信的内容,更多相关内容请关注PHP中文网(www.php.cn)!

热门AI工具

更多
Atoms
Atoms Hot

Atoms是一款AI智能体工具,第一支自动构建真实业务的 AI 团队。

豆包大模型

豆包大模型是一款由字节跳动推出的企业级大语言模型服务平台。

DeepSeek

DeepSeek是一款面向对话、写作、编程和推理场景的AI大模型工具。

讯飞智作

讯飞智作是一款AI视频创作工具,AI文本配音工具,数字人课程、营销视频制作。

WorkBuddy

一款AI办公效率工具,主要用于腾讯云推出的AI原生桌面智能体工作台,适合需要提升相关任务效率的用户。

蛙蛙写作

一款AI论文写作工具,主要用于超级AI智能写作助手,适合需要提升相关任务效率的用户。

Lovart
Lovart Hot

一款面向视觉设计创作的AI设计平台,可通过智能体和画布工作流辅助制作海报、Logo、网页、PPT及其他视觉内容。

火山引擎

火山引擎是一款面向企业的云计算与AI服务平台。

PixTV
PixTV Hot

PixTV是一款面向AIGC内容创作的AI视频生成工具。

相关专题

更多
LLVM自定义Pass怎么写
LLVM自定义Pass怎么写

本专题聚焦LLVM自定义Pass开发,整理Pass类结构、run()方法、PreservedAnalyses、CMake构建、插件注册、-load-pass-plugin加载和测试用例编写流程。

0

2026.09.30

LLVM RISC-V参数配置教程
LLVM RISC-V参数配置教程

本专题介绍LLVM对RISC-V基础ISA和扩展的支持方式,涵盖RV32、RV64、标准扩展、实验性扩展、厂商扩展、-menable-experimental-extensions和版本差异。

0

2026.09.30

LLVM IR中间表示入门指南
LLVM IR中间表示入门指南

本专题整理LLVM IR的核心概念,包括中间表示作用、模块结构、函数、基本块、SSA形式、类型系统和常见语法,帮助新手理解LLVM编译流程中的关键层。

0

2026.09.30

PDF转图片方法
PDF转图片方法

需要把 PDF 页面用于上传、预览、分享或图片归档时,PDF 转图片方法专题整理 JPG/PNG 格式选择、逐页导出、清晰度设置、批量下载和结果检查等流程,帮助用户稳定完成 PDF 图片化处理。

0

2026.09.30

PixTV AI视频生成与无限画布创作
PixTV AI视频生成与无限画布创作

PixTV专题整理AI视频与视觉内容创作相关功能使用教程,涵盖AI生图、视频生成、无限画布、多模型创作、素材管理、声音音乐及视频剪辑等功能,帮助用户快速掌握PixTV从创意到成片的完整制作方法。

0

2026.09.29

Buffalo框架数据库开发全教程
Buffalo框架数据库开发全教程

本专题围绕Buffalo框架数据库开发,讲解database.yml多环境配置、soda与fizz迁移生成回滚、模型结构体标签、增删改查与条件查询、一对多与多对多关联、数据校验、回调钩子、事务处理及原生SQL执行能力。

220

2026.09.23

Buffalo框架路由与请求处理实操指南
Buffalo框架路由与请求处理实操指南

本专题讲解Buffalo框架路由与请求处理机制,涵盖路由注册与分组、资源路由、Handler编写规范、Context上下文方法、参数绑定、中间件编写挂载、Session与Cookie读写、Flash消息及错误页面定制方法。

120

2026.09.23

Buffalo框架零基础入门教程
Buffalo框架零基础入门教程

本专题整理Buffalo框架入门内容,涵盖Go环境准备、buffalo CLI安装、新项目生成、目录结构说明、dev热加载启动、数据库连接配置与常见报错排查,帮助新手按约定优于配置的思路跑通第一个Buffalo框架应用。

100

2026.09.23

Conan创建软件包配方指南
Conan创建软件包配方指南

本专题介绍通过conanfile.py创建软件包的方法,讲解包名、版本、依赖和构建设置等基础信息,以及source、build、package、package_info等常用方法的作用及编写思路。

60

2026.09.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Iris框架MVC快速入门指南
Iris框架MVC快速入门指南

共0课时 | 0人学习

phpMyAdmin 配置文档
phpMyAdmin 配置文档

共0课时 | 0人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn