鹿先生丶

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、检查是否安装crontab

apt-get install cron

 

 

 说明以及安装过了

一般cron检索的crontab文件。这个文件在默认情况下:/etc/crontab

执行文件在 /etc/init,d/cron

重启:  /etc/init.d/cron    restart

 

 

2、检查安装python

 apt-get instll python

 

获取ip以及发送邮箱的功能

#!/bin/bash
import os
# smtplib 用于邮件的发信动作
import smtplib
# email 用于构建邮件内容
from email.mime.text import MIMEText
#获取ipv6地址
ip6s = [ip.split('/')[0] for ip in os.popen("ip addr | grep 'inet6 '|awk '{print $2}'").readlines()]
#获取ipv4地址
ip4s = [ip.split('/')[0] for ip in os.popen("ip addr | grep 'inet '|awk '{print $2}'").readlines()]

# 发信方的信息:发信邮箱,QQ 邮箱授权码
from_addr = '发信方的信息'
password = 'QQ 邮箱授权码'

# 收信方邮箱
to_addr = '收信方邮箱'

# 发信服务器
smtp_server = 'smtp.qq.com'
msg_text   = 'ipv4地址集:   '+str(ip4s)+'\n\n'+'ipv6地址集:     '+str(ip6s)
# 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码
msg = MIMEText(msg_text,'plain','utf-8')


server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server,465)

server.login(from_addr, password)

server.sendmail(from_addr, to_addr, msg.as_string())
# 关闭服务器
server.quit()

 

linux crontab记时事件:

 

 

 python在文件里的执行目录一定需要写为  完全路径

 

 

最后的验证

 

posted on 2022-05-02 00:16  鹿先生丶  阅读(349)  评论(0编辑  收藏  举报