Shell脚本实践(一)

2021-11-15 17:54:02 浏览数 (1)

使用Shell脚本发送带有附件的邮件

代码语言:javascript复制
fromAdd="xx@xx.com"   #发送者
tolist="xx@xx.com"  #收件人
cclist="xx@xx.com"  #抄送cc
subject="xxxx"  #邮件主题
attach="/tmp/xx.log"   # 附件的绝对路径
(  
echo "From: $fromAdd"  
echo "To: $tolist"  
echo "Cc: $cclist"  
echo "Subject: $subject"  
echo "MIME-Version: 1.0"  
echo 'Content-Type: multipart/mixed; boundary="GvXjxJ pjyke8COw"'  
echo  
echo "--GvXjxJ pjyke8COw"  
echo "Content-Type: text/html; charset=US-ASCII"  
echo "Content-Disposition: inline"  
echo  
echo "<h1>See Details from attachment</h1>"   # 此处可以写html,引用变量
echo  
echo "--GvXjxJ pjyke8COw"  
echo "Content-Type: text/plain; charset=US-ASCII;name=$attach"  
echo "Content-Disposition: attachment;filename=$attach"  
echo  
echo  
echo "--GvXjxJ pjyke8COw"  
) | `which sendmail` -t

自动获取服务器的ip

代码语言:javascript复制
if [ `ifconfig | grep -c bond` -ne 0 ]; then
   iface=bond
else
   iface=eth
   if [ `ifconfig | grep -c eth1` -eq 0 ]; then
       local_flag=1
   else
       local_flag=0
   fi
fi
vm_private_ip=`ifconfig ${iface}0 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1`if [ $local_flag -eq 1 ]; then
   vm_public_ip=${vm_private_ip}else
   vm_public_ip=`ifconfig ${iface}1 | grep "inet addr" | cut -d ":" -f 2 | cut -d " " -f 1`
fi

0 人点赞