安装sendmail
1sudo apt install sendemail
开机自动启动服务
在文件夹:/etc/systemd/system/
下创建文件:auto_sendip.service,并填写下列内容
1[Unit]2Description=Send ip with email when start machine3After=network-online.target4Wants=network-online.target5
6[Service]7Environment="LIBVA_DRIVER_NAME=iHD" # 可以去掉,本脚本是从todesk的自启动脚本移植而来8ExecStart=/home/user/send_ip.sh # 绝对地址9Restart=on-failure10RestartSec=5s11User=user # 想要运行本服务的用户12
13[Install]14WantedBy=multi-user.target
将自启动脚本软链接到系统,并开启开机自启。
1sudo ln -sf /home/user/auto_sendip.service /usr/lib/systemd/system/auto_sendip.service2systemctl enable --now auto_sendip.service
send_ip.sh脚本
使用139邮箱作为发送邮箱,开启IMAP/SMTP功能,允许sendemail命令行登陆邮箱并发送邮件。下列的参数-xp后跟的字符串即为客户端授权码。
send_mail为发送邮件的139邮箱,accept_mail_1和accept_mail_2为接受信息的邮箱。
1#! /bin/bash2curl -s -o /dev/null 'http://10.3.8.211/login' --data 'user=202211xxxx&pass=xxxx' # 登陆校园网的用户和密码3sleep 2s4echo begin send email5ifconfig | grep inet | sendemail -f send_mail -t accept_mail_1 -u ip_of_P920_1 -s smtp.139.com -xu send_mail -xp 授权码 -o tls=no6sleep 2s7ifconfig | grep inet | sendemail -f send_mail -t accept_mail_2 -u ip_of_P920_1 -s smtp.139.com -xu send_mail -xp 授权码 -o tls=no