小豆浆 shell+python综合实例(上篇) www.xdoujiang.com

  • A+
所属分类:python实战
  1. 小豆浆 shell+python综合实例(下篇) www.xdoujiang.com
  2. 需求将下面这个日志里取出关键字并导出到各个相关的文本里并压缩好后发送邮件
  3. dianhun-game-trace.log.2017-12-03
  4. 以下是关键字
  5. ====> Dianhun: type=MENU1, userId=
  6. ====> Dianhun: type=MENU2, userId=
  7. ====> Dianhun: type=MENU3, userId=
  8. ====> Dianhun: type=SGY1, userId=
  9. ====> Dianhun: type=MBXX1, userId=
  10. ====> Dianhun: type=MBXX2, userId=
  11. ====> Dianhun: type=MBXX3, userId=
  12. ====> Dianhun: type=MBXX4, userId=
  13. ====> Dianhun: type=MBXX5, userId=
  14. ====> Dianhun: type=MBXX6, userId=
  15. ====> Dianhun: type=MBXX7, userId=
  16. ====> Dianhun: type=MBXX8, userId=
  17. ====> Dianhun: type=MBXX9, userId=
  18. ====> Dianhun: type=MBXX10, userId=
  19. ====> Dianhun: type=MBXX11, userId=
  20. ====> Dianhun: type=MBXX12, userId=
  21. ====> Dianhun: type=MBXX13, userId=
  22. ====> Dianhun: type=MBXX14, userId=
  23. ====> Dianhun: type=MBXX15, userId=
  24. ====> Dianhun: type=MBXX16, userId=
  25. ====> Dianhun: type=MBXX17, userId=
  26. ====> Dianhun: type=MBXX18, userId=
  27. ====> Dianhun: type=MBXX19, userId=
  28. ====> Dianhun: type=MBXX20, userId=
  29. ====> Dianhun: type=MBXX21, userId=
  30. 相关代码
  31. 1、关键字
  32. cat dianhun.conf
  33. ====> Dianhun: type=MENU1, userId=
  34. ====> Dianhun: type=MENU2, userId=
  35. ====> Dianhun: type=MENU3, userId=
  36. ====> Dianhun: type=SGY1, userId=
  37. ====> Dianhun: type=MBXX1, userId=
  38. ====> Dianhun: type=MBXX2, userId=
  39. ====> Dianhun: type=MBXX3, userId=
  40. ====> Dianhun: type=MBXX4, userId=
  41. ====> Dianhun: type=MBXX5, userId=
  42. ====> Dianhun: type=MBXX6, userId=
  43. ====> Dianhun: type=MBXX7, userId=
  44. ====> Dianhun: type=MBXX8, userId=
  45. ====> Dianhun: type=MBXX9, userId=
  46. ====> Dianhun: type=MBXX10, userId=
  47. ====> Dianhun: type=MBXX11, userId=
  48. ====> Dianhun: type=MBXX12, userId=
  49. ====> Dianhun: type=MBXX13, userId=
  50. ====> Dianhun: type=MBXX14, userId=
  51. ====> Dianhun: type=MBXX15, userId=
  52. ====> Dianhun: type=MBXX16, userId=
  53. ====> Dianhun: type=MBXX17, userId=
  54. ====> Dianhun: type=MBXX18, userId=
  55. ====> Dianhun: type=MBXX19, userId=
  56. ====> Dianhun: type=MBXX20, userId=
  57. ====> Dianhun: type=MBXX21, userId=
  58. 2、shell脚本
  59. cat dianhun.sh
  60. #!/bin/bash
  61. #--------------------------------------------------
  62. #Author:gongzheng
  63. #Email:85646830@163.com
  64. #FileName:dianhun.sh
  65. #Function: 
  66. #Version:1.0 
  67. #Created:2017-12-04
  68. #--------------------------------------------------
  69. LANG=en_US.UTF-8
  70. LC_ALL=en_US.UTF-8
  71. PATH=$PATH:/usr/local/bin:/usr/bin:/usr/sbin
  72. if [ $# -lt 1 ];then
  73.     echo -e "Usage: $0 dianhun.conf\n"
  74.     exit 0
  75. else
  76.     config=$1
  77.     if [ ! -f $config ];then
  78.         echo -e "Usage: $0 dianhun.conf\n"
  79.         echo -e "Filename: $1 can't open\n"
  80.         exit 0
  81.     fi
  82. fi
  83. Lastdate=`date +%Y-%m-%d --date='1 day ago'`
  84. Localip=`/sbin/ifconfig eth0|grep 'inet'|grep  -v '127.0.0.1'|head -1|awk '{print $2}'|cut -d":" -f2`
  85. Rizhidir="/home/fulihui/tomcat8089/logs/fulihui-wechatapp"
  86. Linenum=`cat $1|wc -l`
  87. cd ${Rizhidir}/MBXX && rm -f * && cd /home/fulihui/jenkinsripts
  88. for ((k=1;k<=${Linenum};k++))
  89. do
  90.     guanjianzi=$(cat $1|sed -n "$k"p)
  91.     echo "${guanjianzi}"
  92.     cat ${Rizhidir}/dianhun-game-trace.log.${Lastdate}|grep "${guanjianzi}" >> /${Rizhidir}/MBXX/${k}.txt
  93. done
  94. python dianhunyasuo.py
  95. python dianhunmailsend.py /tmp/nginx1.tar.gz "$0 $(Localip)"
  96. 3、压缩
  97. cat dianhunyasuo.py
  98. #!/usr/bin/python
  99. # -*- coding: utf-8 -*-
  100. #--------------------------------------------------
  101. #Author:gongzheng
  102. #Email:85646830@163.com
  103. #FileName:dianhunyasuo.py
  104. #Function: 
  105. #Version:1.0 
  106. #Created:2017-12-04
  107. #--------------------------------------------------
  108. import tarfile
  109. import os
  110. import time
  111. start = time.time()
  112. tmp_tar_dir = "/tmp"
  113. fileformat = ".tar.gz"
  114. tmp_dir = "/home/fulihui/tomcat8089/logs/fulihui-wechatapp/MBXX"
  115. proj = "nginx2"
  116. #filename = time.strftime("%Y%m%d")
  117. #os.path.join拼接tmp_tar_dir和file_name的变量
  118. #t = tarfile.open(os.path.join(tmp_tar_dir,filename+proj+fileformat),"w:gz")
  119. t = tarfile.open(os.path.join(tmp_tar_dir,proj+fileformat),"w:gz")
  120. #os提供的walk方法很强大,能够把给定的目录下的所有目录和文件遍历出来.
  121. #将os.walk在元素中提取的值,分别放到root(根目录),dir(目录名),files(文件名)中.
  122. for root,dir,files in os.walk(tmp_dir):
  123. #print root,dir,files
  124. #os.path.relpath(path[, start])从start开始计算相对路径
  125.     root1 = os.path.relpath(root,start=tmp_dir)
  126. #print root1
  127.     for file in files:
  128. #使用os.path.join拼接地址
  129.         fullpath = os.path.join(root,file)
  130.         t.add(fullpath,arcname=os.path.join(root1,file))
  131. t.close()
  132. print time.time()-start
  133. 4、发送邮件
  134. cat dianhunmailsend.py
  135. #!/usr/bin/python
  136. # -*- coding: utf-8 -*-
  137. #--------------------------------------------------
  138. #Author:gongzheng
  139. #Email:85646830@163.com
  140. #FileName:dianhunmailsend.py
  141. #Function: 
  142. #Version:1.0 
  143. #Created:2017-12-03
  144. #--------------------------------------------------
  145. import smtplib
  146. import email.MIMEMultipart
  147. import email.MIMEText
  148. import email.MIMEBase
  149. import os.path
  150. From = "aaa@11111.com"
  151. To = "bbb@11111.com"
  152. file_name = "/tmp/nginx2.tar.gz"
  153. smtpserver = "smtp.exmail.qq.com"
  154. username = "aaa@11111.com"
  155. password = "111111"
  156. s = smtplib.SMTP_SSL()
  157. s.set_debuglevel(1)
  158. s.connect(smtpserver)
  159. s.ehlo()
  160. s.login(username,password)
  161. # 构造MIMEMultipart对象做为根容器  
  162. main_msg = email.MIMEMultipart.MIMEMultipart()
  163. # 构造MIMEText对象做为邮件显示内容并附加到根容器  
  164. text_msg = email.MIMEText.MIMEText("172.16.0.100 duanhunrizhi")
  165. main_msg.attach(text_msg)
  166. # 构造MIMEBase对象做为文件附件内容并附加到根容器
  167. contype = 'application/octet-stream'
  168. maintype, subtype = contype.split('/', 1)
  169. ## 读入文件内容并格式化
  170. data = open(file_name,'rb')
  171. file_msg = email.MIMEBase.MIMEBase(maintype, subtype)
  172. file_msg.set_payload(data.read( ))
  173. data.close( )
  174. email.Encoders.encode_base64(file_msg)
  175. ## 设置附件头  
  176. basename = os.path.basename(file_name)
  177. file_msg.add_header('Content-Disposition',
  178.  'attachment', filename = basename)
  179. main_msg.attach(file_msg)
  180. # 设置根容器属性  
  181. main_msg['From'] = From
  182. main_msg['To'] = To
  183. main_msg['Subject'] = "172.16.0.100 dianhunrizhi"
  184. main_msg['Date'] = email.Utils.formatdate( )
  185. # 得到格式化后的完整文本  
  186. fullText = main_msg.as_string( )
  187. # 用smtp发送邮件  
  188. try:
  189.     s.sendmail(From,To,fullText)
  190. finally:
  191.     s.quit()
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: