想通過Linux監控系統狀況,並郵件自動報警,可以使用腳本處理,通過自帶的mailx工具來發出警告 郵件
安裝mailx
root@pts/1 # yum -y install mailx
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.163.com
* extras: mirrors.yun-idc.com
* updates: mirrors.yun-idc.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 4.0 MB 00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package mailx.x86_64 0:12.4-8.el6_6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===============================================================================================================================================
Package Arch Version Repository Size
===============================================================================================================================================
Installing:
mailx x86_64 12.4-8.el6_6 base 235 k
Transaction Summary
===============================================================================================================================================
Install 1 Package(s)
Total download size: 235 k
Installed size: 452 k
Downloading Packages:
mailx-12.4-8.el6_6.x86_64.rpm | 235 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : mailx-12.4-8.el6_6.x86_64 1/1
Verifying : mailx-12.4-8.el6_6.x86_64 1/1
Installed:
mailx.x86_64 0:12.4-8.el6_6
Complete!
確認安裝結果(版本必須是12.4以上,不然發郵件需要sendmail服務)
root@pts/1 # rpm -qa|grep mailx
mailx-12.4-8.el6_6.x86_64
root@pts/1 # mail -V
12.4 7/29/08
配置外部郵箱(發件人)
root@pts/0 # vim /etc/mail.rc
set [email protected] smtp=smtp.139.com
set [email protected] smtp-auth-password=******
set smtp-auth=login
說明:
from: 對方收到郵件時顯示的發件人
smtp: 指定第三方發送郵件的smtp服務器地址
smtp-auth: SMTP的認證方式。默認是LOGIN,也可改爲CRAM-MD5或PLAIN方式
smtp-auth-user: 第三方發郵件的用戶名
smtp-auth-password: 用戶名對應密碼
mail命令
root@pts/2 # mail --help
mail: illegal option -- -
Usage: mail -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users
注:部分系統參數稍有差異,最好看幫助
1) 無郵件正文
mail -s "主題" 收件地址
mail -s "測試" [email protected]
2) 有郵件正文
mail -s "主題" 收件地址< 文件(郵件正文.txt)
mail -s "郵件主題" [email protected] < /data/findyou.txt
echo "郵件正文" | mail -s 郵件主題 收件地址
echo "郵件正文內容" | mail -s "郵件主題" [email protected]
cat 郵件正文.txt | mail -s 郵件主題 收件地址
cat /data/findyou.txt | mail -s "郵件主題" [email protected]
3) 帶附件
mail -s "主題" 收件地址 -a 附件 < 文件(郵件正文.txt)
mail -s "郵件主題" [email protected] -a /data/findyou.tar.gz < /data/findyou.txt