分类 linux 下的文章

首先,crontab要执行的任务要有输出日志,表明任务执行过。

然后,另起一个crontab任务,去监控上面的这个日志,比较日志的更新时间,判断任务是否执行,从而提醒。

下面是我在实际生产环境中使用的一个脚本示例:

timestamp() {
  date +"%s"
}
url="http://报警url"

last_mofidy_timestamps="$(stat -c %Y /home/example/example.log)"
echo $last_mofidy_timestamps

current_timestamps="$(timestamp)"
echo $current_timestamps

if (($current_timestamps - $last_mofidy_timestamps > 3600)); then
    curl $url
fi

ufw 全称 Uncomplicated Firewall ,从名称来看,不那么复杂的防火墙?。

提到linux的防火墙管理,就不得不提iptables,但是说实话,这个命令我实在记不住,用得少,同时参数多,比较复杂。
ufw就是用来解决这个问题的,他算是iptables的一个前端(frontend)。

## 安装ufw
在Debian系的电脑上直接执行以下命令:

   $ sudo apt-get update
   $ sudo apt-get install ufw

启用ufw

    $ sudo ufw enable

禁用ufw

    $ sudo ufw disable

查看默认规则

    $ grep 'DEFAULT_' /etc/default/ufw

Outputs:

DEFAULT_INPUT_POLICY="DROP"
DEFAULT_OUTPUT_POLICY="ACCEPT"
DEFAULT_FORWARD_POLICY="DROP"
DEFAULT_APPLICATION_POLICY="SKIP"

设置默认值

    ## 禁止所有传入,允许所有传出
    $ sudo ufw default allow outgoing
    $ sudo ufw default deny incoming

查看ufw的状态

    $ sudo ufw status
    $ sudo ufw status numbered

打开端口

    ## 打开12345端口,允许tcp协议和udp协议
    $ sudo ufw allow 12345

    ## 打开12345端口,允许tcp协议
    $ sudo ufw allow 12345/tcp

    ## 打开12345端口,允许udp协议
    $ sudo ufw allow 12345/udp

    ## 指定方向,in即传入,out即传出
    ## 如果不指定方向,默认是传入
    $ sudo ufw allow in 12345/tcp
    $ sudo ufw allow out 12345/tcp

关闭端口

    ## 禁止80端口
    $ sudo deny 80/tcp

打开或关闭连续端口

    ## 打开3000到5000之间的所有端口
    $ sudo ufw allow 3000:5000/tcp
    $ sudo ufw allow 3000:5000/udp

对ip做限制

    ## 允许指定ip访问指定端口
    $ sudo ufw allow from 1.2.3.4 to any port 443
    $ sudo ufw allow from 1.2.3.4 to any port 80
    ## 允许子网
    $ sudo ufw allow from 1.2.3.4/23 to any port 443
    $ sudo ufw allow from 1.2.3.4/23 to any port 80

    ## 禁止某ip的所有连接
    $ sudo ufw deny from 1.2.3.4
    ## 禁止子网
    $ sudo ufw deny from 1.2.3.4/24
    ## 禁止ip使用tcp协议访问某个端口
    $ sudo ufw deny from 1.2.3.4 to any port 22 proto tcp

删除操作

    $ sudo delete deny 80/tcp

重置

    $ sudo ufw reset   

重新加载

    $ sudo ufw reload

查看日志

    $ sudo tail -f /var/log/ufw.log