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

yipeiwu_com5年前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>"
     "",'HTML','utf-8')
 main_msg.attach(text_msg)
 #xlsxpart = MIMEApplication(open(file_name, 'rb').read())
 #xlsxpart.add_header('Content-Disposition', 'attachment', filename=f_subject+".docx")
 #main_msg.attach(xlsxpart)
 # 设置根容器属性
 main_msg['From'] = From
 main_msg['To'] = To
 main_msg['Cc'] = ",".join(f_cclist)
 main_msg['Subject'] = f_subject
 main_msg['Date'] = 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('utf-8')
 message= [
 'Usage:',
 ' sendmail.py "topic" "mail body text" "mail to"',
 'Examples of usage:',
 '   sendmail.py "topic" "hello world" "14638852@qq.com"',
 ]
 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+'\n'
 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脚本分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django框架组成结构、基本概念与文件功能分析

本文实例讲述了Django框架组成结构、基本概念与文件功能。分享给大家供大家参考,具体如下: django遵循MVC架构: 管理工具(management):一套内置的创建站点、迁移数据...

Python基于property实现类的特性操作示例

本文实例讲述了Python基于property实现类的特性操作。分享给大家供大家参考,具体如下: Python中的特性是一个函数,但是在使用的形式上看起来更像是一个属性。针对一个对象来说...

python判断设备是否联网的方法

本文实例为大家分享了python判断设备是否联网的具体代码,供大家参考,具体内容如下 直接上代码,就是用判断socket能不连上的方法来判断。 #!/usr/bin/env pyth...

详解Python基础random模块随机数的生成

随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等。Python内置的random模块提供了生成随机数的方法,使用这些方法时...

Python对象体系深入分析

Python对象体系深入分析

本文较为详细的分析了了Python的对象体系。分享给大家供大家参考。具体如下: Guido用C语言创造了Python,在Python的世界中一切皆为对象. 一.C视角中的Python对象...