Lyndra's Blog

服务器开机自动发送ip邮件

2024-01-19
系统维护 ServerUbuntu
2分钟
265字
温馨提示:本文最后更新于 2025-03-11 ,部分信息可能因时间推移而不再适用,欢迎反馈。

安装sendmail

1
sudo apt install sendemail

开机自动启动服务

  在文件夹:/etc/systemd/system/​ 下创建文件:auto_sendip.service,并填写下列内容

Terminal window
1
[Unit]
2
Description=Send ip with email when start machine
3
After=network-online.target
4
Wants=network-online.target
5
6
[Service]
7
Environment="LIBVA_DRIVER_NAME=iHD" # 可以去掉,本脚本是从todesk的自启动脚本移植而来
8
ExecStart=/home/user/send_ip.sh # 绝对地址
9
Restart=on-failure
10
RestartSec=5s
11
User=user # 想要运行本服务的用户
12
13
[Install]
14
WantedBy=multi-user.target

  将自启动脚本软链接到系统,并开启开机自启。

Terminal window
1
sudo ln -sf /home/user/auto_sendip.service /usr/lib/systemd/system/auto_sendip.service
2
systemctl enable --now auto_sendip.service

send_ip.sh脚本

  使用139邮箱作为发送邮箱,开启IMAP/SMTP功能,允许sendemail命令行登陆邮箱并发送邮件。下列的参数-xp后跟的字符串即为客户端授权码。

  send_mail为发送邮件的139邮箱,accept_mail_1和accept_mail_2为接受信息的邮箱。

send_host_ip_to_edu_mail.sh
1
#! /bin/bash
2
curl -s -o /dev/null 'http://10.3.8.211/login' --data 'user=202211xxxx&pass=xxxx' # 登陆校园网的用户和密码
3
sleep 2s
4
echo begin send email
5
ifconfig | 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=no
6
sleep 2s
7
ifconfig | 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

  ‍

本文标题:服务器开机自动发送ip邮件
文章作者:Lyndra
发布时间:2024-01-19
总访问量
总访客数人次
Copyright 2025
站点地图