广告
返回顶部
首页 > 资讯 > 服务器 >脚本实现SSH登录邮件报警
  • 873
分享到

脚本实现SSH登录邮件报警

脚本邮件SSH 2022-06-04 21:06:48 873人浏览 独家记忆
摘要

登录保护是一个非常重要的环节,下面通过图文并茂的方式给大家详细讲解下: Http://www.gimoo.net/t/1512/ 前两天@cyy 给我发了一个图 然后我就想到USHQ的ssh登录app通知

登录保护是一个非常重要的环节,下面通过图文并茂的方式给大家详细讲解下:

Http://www.gimoo.net/t/1512/
前两天@cyy 给我发了一个图

WEBroot/GCms/lib/api/Open/Article.PHP on line 161http://www.gimoo.net/t/1512/
img/2015/08/19/141255_55d41e671cdb5.png" alt="查看图片" />

然后我就想到USHQ的ssh登录app通知功能,然后就像如果把这个部署到自用的服务器就好了。至少多一层安全系数。

首先要感谢@Legion 帮忙搞定了几个错误以及搞定了Geo2IP的JSON转换。 (P.S.此人为自动化运维大神级人物,现任职于德国一数据统计企业。)

当然,我和他相比我就是战五渣了...大家一定要多向 @Legion 学习啊~~

说下需要做的准备:

sendmail或者Postfixhttp://www.gimoo.net/t/1512/
phphttp://www.gimoo.net/t/1512/
bashhttp://www.gimoo.net/t/1512/
Centos/Debian/ubuntuhttp://www.gimoo.net/t/1512/
若你的生产环境中没有php sendmail Postfix等组件,请移步:

@Legion: linux之使用shell脚本实现ssh登录报警

参考文件

首先是报警脚本文件

Shell


#!/bin/sh
#########################################################################
# File Name: Login-alert.sh
# Author: Jason
# Email: master#deamwork.com
# Created Time: Tue Jul 21 2015 21:23:16 PM CST 
#########################################################################

#require jq
#wget http://stedolan.GitHub.io/jq/download/linux64/jq -O /usr/local/bin/jq
#chmod +x /usr/local/bin/jq

#if error, please # following one
#PATH=/usr/local/Nginx/sbin:/usr/local/php/bin:/usr/local/Mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#Geo2IP by Legion(http://www.dwhd.org/)
eval `curl -s "http://ip.taobao.com/service/getIpInfo.php?ip=${SSH_CLIENT%% *}" | jq . | awk -F':|[ ]+|"' '{if($3~/^(country|area|region|city|isp)$/){print $3"="$7}}'`

#html mail content
cat >> mail-no-base64.html <<EOF
#请自行准备邮件模板,以下为可能用到的变量
#输出主机名 `hostname`
#输出登录端口 ${SSH_CLIENT##* }
#输出登录来源IP ${SSH_CLIENT%% *}
#输出IP地址归属地 {country}_${area}_${region}_${city}_${isp}
#输出登录时间 `date`
EOF

#Base64 Encoding
base64 mail-no-base64.html > mail-base64.html

#使用Sendmail
#sendmail -t >/dev/null 2>&1 <<EOF
#to:example@example.com
#from:Example<example@example.com>
#subject:[`hostname`]服务器登录告警

#`cat mail-no-base64.html`

#EOF

#使用postfix
#cat >> mail.php <<EOF
#<?php
#$to = "example@example.com";
#$subject = "[`hostname`]服务器登录告警";
#$message = "`cat mail-base64.html`";
#$headers = "MIME-Version: 1.0" . "rn";
#$headers .= "Content-Type: text/html; charset="utf-8"" . "rn";
#$headers .= "Content-Transfer-Encoding: base64" . "rn";
#$headers .= 'From: Example<example@example.com>' . "rn";
#$send = mail($to,$subject,$message,$headers);
#if($send){echo 'Mail Send Successful.';}else{echo 'Failed.';}
#?>
#EOF

#使用 SMTP (require smtp-class.php)
cat >> mail.php <<EOF
<?php
require("smtp-class.php"); 
$smtpserver = "smtp.example.com";
$smtpserverport = 25;
$smtpusermail = "example@example.com";
$smtpemailto = "example@example.com";
$smtpuser = "example";
$smtppass = "passWord";
$mailsubject = "[`hostname`]服务器登录告警";
$mailbody = "`cat mail-base64.html`";
$mailtype = "HTML";
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
$smtp->debug = TRUE;
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype); 
?>
EOF
php mail.php
yes y | rm mail-no-base64.html mail-base64.html mail.php

然后是如何触发这个脚本:

Shell

echo "screen -fa -d -m -S WL /etc/Login-alert.sh" >> /etc/profile
http://www.gimoo.net/t/1512/
用这种方法, 新开终端或者复制终端都会触发报警

如果使用smtp方式,请保存以下文件为smtp-class.php

PHP


<?php
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 = FALSE;
    $this->smtp_port = $smtp_port;
    $this->relay_host = $relay_host;
    $this->time_out = 30; //is used in fsockopen() 
    $this->auth = $auth;//auth
    $this->user = $user;
    $this->pass = $pass;
    $this->host_name = "localhost"; //is used in HELO command 
    $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("(^|(rn))(.)", "1.3", $body);
    $header .= "MIME-Version:1.0rn";
    if($mailtype=="HTML")
    {
      $header .= "Content-Type: text/html; charset="utf-8"" . "rn";
      $header .= "Content-Transfer-Encoding: base64" . "rn";
    }
    $header .= "To: ".$to."rn";
    if ($cc != "") 
    {
      $header .= "Cc: ".$cc."rn";
    }
    $header .= "From: $from<".$from.">rn";
    $header .= "Subject: ".$subject."rn";
    $header .= $additional_headers;
    $header .= "Date: ".date("r")."rn";
    $header .= "X-Mailer:By TianhaiTech (PHP/".phpversion().")rn";
    list($msec, $sec) = explode(" ", microtime());
    $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn";
    $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."n");
        $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.">n");
      } 
      else 
      {
        $this->log_write("Error: Cannot send email to <".$rcpt_to.">n");
        $sent = FALSE;
      }
      fclose($this->sock);
      $this->log_write("Disconnected from remote hostn");
    }
    return $sent;
  }

  
  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."n");
    $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."n");
      $this->log_write("Error: ".$errstr." (".$errno.")n");
      return FALSE;
    }
    $this->log_write("Connected to relay host ".$this->relay_host."n");
    return TRUE;;
  }

  function smtp_sockopen_mx($address)
  {
    $domain = ereg_replace("^.+@([^@]+)$", "1", $address);
    if (!@getmxrr($domain, $MXHOSTS)) 
    {
      $this->log_write("Error: Cannot resolve MX "".$domain.""n");
      return FALSE;
    }
    foreach ($MXHOSTS as $host) 
    {
      $this->log_write("Trying to ".$host.":".$this->smtp_port."n");
      $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."n");
        $this->log_write("Error: ".$errstr." (".$errno.")n");
        continue;
      }
      $this->log_write("Connected to mx host ".$host."n");
      return TRUE;
    }
    $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")n");
    return FALSE;
  }

  function smtp_message($header, $body)
  {
    fputs($this->sock, $header."rn".$body);
    $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
    return TRUE;
  }

  function smtp_eom()
  {
    fputs($this->sock, "rn.rn");
    $this->smtp_debug(". [EOM]n");
    return $this->smtp_ok();
  }

  function smtp_ok()
  {
    $response = str_replace("rn", "", fgets($this->sock, 512));
    $this->smtp_debug($response."n");
    if (!ereg("^[23]", $response)) 
    {
      fputs($this->sock, "QUITrn");
      fgets($this->sock, 512);
      $this->log_write("Error: Remote host returned "".$response.""n");
      return FALSE;
    }
    return TRUE;
  }

  function smtp_putcmd($cmd, $arg = "")
  {
    if ($arg != "") 
    {
      if($cmd=="") 
      {
        $cmd = $arg;
      }
      else
      {
        $cmd = $cmd." ".$arg;
      }
    }
    fputs($this->sock, $cmd."rn");
    $this->smtp_debug("> ".$cmd."n");
    return $this->smtp_ok();
  }

  function smtp_error($string)
  {
    $this->log_write("Error: Error occurred while ".$string.".n");
    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.""n");
      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("([ trn])+", "", $address);
    $address = ereg_replace("^.*<(.+)>.*$", "1", $address);
    return $address;
  }

  function smtp_debug($message)
  {
    if ($this->debug) 
    {
      echo $message;
    }
  }

}

?>http://www.gimoo.net/t/1512/
实现效果:

查看图片

有需要的朋友可以参考下,希望大家能够喜欢。

--结束END--

本文标题: 脚本实现SSH登录邮件报警

本文链接: https://www.lsjlt.com/news/18540.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 脚本实现SSH登录邮件报警
    登录保护是一个非常重要的环节,下面通过图文并茂的方式给大家详细讲解下: http://www.gimoo.net/t/1512/ 前两天@cyy 给我发了一个图 然后我就想到USHQ的ssh登录app通知...
    99+
    2022-06-04
    脚本 邮件 SSH
  • 如何实现脚本SSH登录邮件报警
    本篇内容主要讲解“如何实现脚本SSH登录邮件报警”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何实现脚本SSH登录邮件报警”吧!登录保护是一个非常重要的环节,下面通过图文并茂的方式给大家详细讲...
    99+
    2023-06-09
  • zabbix邮件报警python脚本
    下面是两个不同的python脚本,只是连接邮件服务器的方法不同,其他方法还是相同的#!/usr/bin/env python #-*- coding: UTF-8 -*- import smtplib import sys from em...
    99+
    2023-01-31
    脚本 邮件 zabbix
  • 怎么用shell脚本实现ssh登录
    要使用shell脚本实现SSH登录,你可以使用SSH命令行工具,并将其包含在shell脚本中。以下是一个简单的示例:```bash#...
    99+
    2023-10-12
    shell ssh
  • 怎么使用shell脚本实现ssh登录
    使用shell脚本实现SSH登录可以通过以下步骤实现:1. 创建一个新的shell脚本文件,比如`login.sh`。2. 在脚本文...
    99+
    2023-09-22
    shell ssh
  • zabbix实现Python邮件报警
    zabbix是个非常强大的监控工具,可以监控linux和windows的服务器数据,也可以通过自定义key来扩展默认的监控项,但是自带的邮件报警提供的信息却不太友善。本文想通过自定脚本的方式,实现在报警邮件的同时发送对应的图像和url连接。...
    99+
    2023-01-31
    邮件 zabbix Python
  • Shell脚本实现非法IP登陆自动报警
    服务器的安全稳定是每个运维都希望达到的目标,毕竟网站一旦流量大了,访问高了,就会有一些无聊人来攻击,帮忙检测漏洞是好,但纯ddos的性质就很恶劣了.说远了,这篇文章只是检测有非法ip登录到服务器上就自动给运...
    99+
    2022-06-04
    脚本 Shell IP
  • shell脚本实现ssh自动登录功能分享
    文件名:ssh_auto_login #!/usr/bin/expect### ssh模拟登陆器## @author zhiyuan <hzyhouzhiyuan艾特gmail.com>##i...
    99+
    2022-06-04
    脚本 自动登录 功能
  • shell脚本怎么实现ssh自动登录功能
    本篇内容介绍了“shell脚本怎么实现ssh自动登录功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!文件名:ssh_auto_login代...
    99+
    2023-06-09
  • 如何实现用Shell脚本监控服务器在线状态和邮件报警
    本篇内容主要讲解“如何实现用Shell脚本监控服务器在线状态和邮件报警”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何实现用Shell脚本监控服务器在线状态和邮件报警”吧!对于服务器来说在线率...
    99+
    2023-06-09
  • 如何用Shell脚本实现非法IP登陆自动报警
    这篇文章主要讲解了“如何用Shell脚本实现非法IP登陆自动报警”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何用Shell脚本实现非法IP登陆自动报警”吧!代码如下:#!/bin/bas...
    99+
    2023-06-09
  • CentOS7.8是如何通过QQ邮箱实现邮件报警
    CentOS7.8是如何通过QQ邮箱实现邮件报警,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。为了实现服务得高可用性,可以使用电子邮件通知服务,当出现问题时能够实现邮件报警从而...
    99+
    2023-06-28
  • Shell脚本监控服务器在线状态和邮件报警的方法
    对于服务器来说在线率很重要,出现问题要能及时解决,但系统管理员不能一直守在电脑旁边,通过脚本监控网站出现问题及时通过mail通知管理员,如果是139邮箱还可免费手机短信通知。注:通过系统直接发送mail容易...
    99+
    2022-06-04
    在线 脚本 状态
  • Shell脚本实现删除邮件队列
    最近遇到了一个麻烦事:机器上邮件队列里老是被塞满,但是又不知道是什么地方发邮件写入的,十分蛋疼,没办法,只有临时处理一下了,写了个删除邮件的脚本rm_mqueue.sh。 #/bin/bash ###...
    99+
    2022-06-04
    队列 脚本 邮件
  • linux使用QQ实现网络邮件报警功能
    环境: CentOS7目的: 考虑到实现服务的高可用性。使用电子邮件通知服务,可以快速的通知维护人员。提高服务的可靠性,而通过 smtp.qq.com 实现脚本邮件报警 一、设置并取得 smtp.qq.com 的 使用 ...
    99+
    2022-06-04
    linux qq 邮件报警 linux 邮件报警
  • 用shell+sendmail实现服务器监控报警小脚本
    这种email报警脚本遍地都是,很多用的sendmail、postfix,感觉有些大材小用了;也有些用perl的NET::SMTP和Authen::SASL模块发信的,不过我perl用的不好,老发出些莫名其...
    99+
    2022-06-04
    脚本 服务器 shell
  • SpringBoot应用监控带邮件警报的实现示例
    目录1.actor(client)2.admin(server)3.测试1.actor(client) 1.1 pom.xml <xml version="1.0" encod...
    99+
    2022-11-13
  • Python脚本实现自动登录校园网
    Python自动化脚本登录校园网 所需工具:python编译环境(博主使用的pycharm作演示,其实在cmd也可以操作!) selenium自动化脚本 .bat批处理文件 第一步...
    99+
    2022-11-12
  • Linux expect实现自动登录脚本实例代码
    expect expect可以让我们实现自动登录远程机器,并且可以实现自动远程执行命令。当然若是使用不带密码的密钥验证同样可以实现自动登录和自动远程执行命令。但当不能使用密钥验证的时候,我们就没有办法了。所以,这时候只要...
    99+
    2022-06-04
    linux expect 脚本 expect实现自动登录 expect 自动登录
  • Linux下如何实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本
    这篇文章主要介绍了Linux下如何实现SSH免密码登录和实现秘钥的管理、分发、部署SHELL脚本,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。环境:ssh server: 1...
    99+
    2023-06-09
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作