nmap+diff 自動化腳本

nmap+diff實現自動化掃描,併發送郵件centos

NMap,英文是Network Mapper,最先是Linux下的網絡掃描和嗅探工具包。軟件簡介nmap是一個網絡鏈接端掃描軟件,用來掃描網上電腦開放的網絡鏈接端。bash

 CentOS安裝nmap端口查看工具網絡

1、安裝nmap併發

yum install nmap    #輸入y安裝app

安裝完後咱們能夠在隨意一個地方建一個*.sh文件,腳本內容以下:tcp

[root@ centos]#cat /tmp/a.sh
#!/bin/bash
#author:zrh
NETWORK="172.25.0.0/24"    ##要檢測網段的ip
mail_user="8475369abcqq.com"
time=$(date -d "today" +%Y-%m-%d)
hostname=$(hostname)
send_mail () {
   echo "$1" | mail -s "Port survival detection in $hostname - $time" $mail_user
  
}
send_mail2 () {
   echo "$1" | mail -s "Port already change in $hostname - $time" $mail_user
}
nmap_port1 () { 
      cd /nmap
      if [ $? -eq 0 ]; then 
          cd /nmap
          nmap -sS $NETWORK > nmap1.txt
          send_mail "`cat nmap1.txt`"
          echo  "`cat nmap1.txt`"
      else
          mkdir /nmap
          nmap -sS $NETWORK > nmap1.txt
          send_mail "`cat nmap1.txt`"
          echo  "`cat nmap1.txt`"
     fi
}
nmap_port2()  {
        cd /nmap 
        nmap -sS $NETWORK > nmap2.txt
        diff nmap1.txt nmap2.txt > nmap3.txt
        cat  nmap3.txt  | grep  "/tcp"
            if [ $? -eq 0 ];then
                 diff nmap1.txt nmap2.txt > nmap3.txt
                 a=$(cat nmap1.txt | grep "tcp" |  wc -l)
                 b=$(cat nmap2.txt | grep "tcp" |  wc -l) 
                    if [ $a -gt $b ]; then
                        reduce=$(cat nmap3.txt | grep "tcp" | cut -c "3-10" | cut -d/ -f1)
                        send_mail2  "The $hostname reduces  port with $reduce port `cat nmap3.txt` "  
                    else
                        if [ $a -lt $b ];then
                             increase=$(cat nmap3.txt | grep "tcp" | cut -c "3-10" | cut -d/ -f1)  
                             send_mail2  "The $hostname increases port with $increase ports `cat nmap3.txt` "
                             break
                        fi  
                    fi                      
            else        
                 send_mail "The port is unchanged"
    fi
}
$1
crontab () {
  cat /etc/crontab | grep "bash $0 nmap_port2"  
  if [ $? -eq 0 ]; then
     echo "The port test has been written to the time task, and 00:00 will perform the task"
  else
     echo "0 0 * * *  bash $0 nmap_port2" >> /etc/crontab
  fi
}
line(){
 echo "Please echo \"test\""  
 echo "If you check your script file please input \"nmap_port/nmap_port2\" " 
 }
line
test () {
nmap_port1
crontab
}
$1

以上的腳本咱們就能夠隨時檢查主機所在的網段端口的變化狀況。ide

相關文章
相關標籤/搜索