登录  /  注册

实用自动化运维Python脚本分享

不言
发布: 2018-06-04 11:13:43
原创
3520人浏览过

这篇文章主要介绍了关于实用自动化运维python脚本分享,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

并行发送sh命令

pbsh.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import paramiko
import sys
import threading
#Copy local file to remote server.
def sshclient_scp(hostname, port, username, password, local_path, remote_path):
 t = paramiko.Transport((hostname, port))
 t.connect(username=username, password=password) # 登录远程服务器
 sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议
 sftp.put(local_path, remote_path)
 t.close()
def sshclient_scp_get(hostname, port, username, password, remote_path, local_path):
 t = paramiko.Transport((hostname, port))
 t.connect(username=username, password=password) # 登录远程服务器
 sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议
 sftp.get(remote_path, local_path)
 t.close()
def sshclient_execmd(hostname, port, username, password, execmd):
 paramiko.util.log_to_file("paramiko.log")
 s = paramiko.SSHClient()
 s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 s.connect(hostname=hostname, port=port, username=username, password=password)
 stdin, stdout, stderr = s.exec_command(execmd)
 stdin.write("Y") # Generally speaking, the first connection, need a simple interaction.
 line=stdout.read()
 s.close()
 print (hostname+":")
 print line
try:
 file_name = sys.argv[1]
 cmd= sys.argv[2]
except IndexError:
 print 'Wrong params!'
 print 'Usage :'
 print '  batch.py "$OS_LIST_FILE" "$BATCH_EXECUTE_CMD"'
 print 'cat oslist.txt:'
 print '192.168.0.1,22,oracle,passwd1'
 print '192.168.0.2,22,oracle,passwd1'
 print '192.168.0.3,24,oracle,passwd1'
 print 'Format is :'
 print 'IPADDR,SSHPORT,USERNAME,PASSWORD'
 print 'Examples of usage:'
 print './batch.py "/root/workspace/oslist.txt" "df -h"'
 sys.exit()
#file_name = sys.argv[1]
#cmd= sys.argv[2]
#maintenance_osinfo
with open(file_name) as file_object:
 for line in file_object:
  splits_str = line.rstrip().split(',')
  a=threading.Thread(target=sshclient_execmd,args=(splits_str[0],int(splits_str[1]),splits_str[2],splits_str[3],cmd))
  a.start()
  #print sshclient_execmd(splits_str[0],int(splits_str[1]),splits_str[2],splits_str[3],cmd)
#  print sshclient_scp(splits_str[0], int(splits_str[1]), splits_str[2], splits_str[3], file_name, splits_str[4]+file_name)
登录后复制

python发送邮件

sendmail.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
import sys
#from email.mime.application import MIMEApplication
#import os.path
def sendmail(f_from, f_to, f_cclist, alert_info, f_subject):
 From = f_from
 To = f_to
 #file_name = f_file_name
 server = smtplib.SMTP("smtp.xxxx.com.cn")
 server.login("xxxx","xxxx")
 #构造MIMEMultipart对象做为根容器
 main_msg = email.MIMEMultipart.MIMEMultipart()
 text_msg = email.MIMEText.MIMEText("您好。<br><br><br><br>"
          + alert_info.title() +
          "<br>任凤军 <br>"
          "xx技术股份有限公司 <br>"
          "手机: xx<br>"
          "座机:xxx<br>"
          "邮箱:xxxx@xx.com<br>"
          "地址:xxxx<br>"
          "邮编:130011<br>"
          "===================================<br>"
          "",&#39;HTML&#39;,&#39;utf-8&#39;)
 main_msg.attach(text_msg)
 #xlsxpart = MIMEApplication(open(file_name, &#39;rb&#39;).read())
 #xlsxpart.add_header(&#39;Content-Disposition&#39;, &#39;attachment&#39;, filename=f_subject+".docx")
 #main_msg.attach(xlsxpart)
 # 设置根容器属性
 main_msg[&#39;From&#39;] = From
 main_msg[&#39;To&#39;] = To
 main_msg[&#39;Cc&#39;] = ",".join(f_cclist)
 main_msg[&#39;Subject&#39;] = f_subject
 main_msg[&#39;Date&#39;] = email.Utils.formatdate()
 #f_cclist为完整的需要接收邮件的列表,原本只存放抄送列表,这里需要添加上收件人
 f_cclist.append(To)
 # 得到格式化后的完整文本
 fullText = main_msg.as_string()
 # 用smtp发送邮件
 try:
  server.sendmail(From, f_cclist, fullText)
 finally:
  server.quit()
if __name__ == "__main__":
 #sys.setdefaultencoding(&#39;utf-8&#39;)
 message= [
 &#39;Usage:&#39;,
 &#39;  sendmail.py "topic" "mail body text" "mail to"&#39;,
 &#39;Examples of usage:&#39;,
 &#39;     sendmail.py "topic" "hello world" "14638852@qq.com"&#39;,
 ]
 try:
  topic = str(sys.argv[1]).encode("utf-8")
  alert = str(sys.argv[2]).encode("utf-8")
  mailto = str(sys.argv[3]).encode("utf-8")
 except IndexError:
  for line in message:
   print line+&#39;\n&#39;
  sys.exit()
 cclist=[]
 #clist =[]
 sendmail("xxxx@xxx",mailto,cclist,alert, topic)
备注:
sendmail("xxxx@gmail.com",mailto,cclist,alert, topic)
发件人,收件人,抄送列表,正文内容,邮件标题
Usage:
  sendmail.py "topic" "mail body text" "mail to"
Examples of usage:
     sendmail.py "topic" "hello world" "14638852@qq.com"
./sendmail.py "topic" "hello world" "14638852@qq.com"
登录后复制

smtp以及邮件的签名,还有发件人为定值,需要自己修改。

相关推荐:

Python脚本在Linux下如何自启动与定时任务的实例详解

分享在IIS上用CGI方式运行Python脚本的实例教程

以上就是实用自动化运维Python脚本分享的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号