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

作者:renfengjun 时间:2022-05-23 20:26:57 

并行发送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以及邮件的签名,还有发件人为定值,需要自己修改。

来源:https://blog.csdn.net/renfengjun/article/details/78661324

标签:python,自动化,运维
0
投稿

猜你喜欢

  • python发腾讯微博代码分享

    2022-05-27 04:45:00
  • 网页设计软件FrontPage快捷键一览

    2008-02-24 16:55:00
  • 如何利用pyecharts画好看的饼状图

    2021-04-19 09:07:19
  • 不用加载Include文件也能生成选择列表吗?

    2009-10-29 12:30:00
  • python使用pil生成图片验证码的方法

    2022-03-08 18:39:24
  • python中可以声明变量类型吗

    2022-02-13 19:44:05
  • 对python numpy.array插入一行或一列的方法详解

    2022-12-08 15:44:07
  • 如何在网页中制作虚线表格

    2010-10-20 20:07:00
  • Zen Coding: 一种快速编写HTML/CSS代码[译]

    2009-12-16 12:53:00
  • 如何将Python编译成C语言

    2022-01-02 10:49:31
  • 三大原因:按照新HTML标准设计网页好处

    2008-11-12 11:23:00
  • 语义、标准和样式

    2008-06-05 12:52:00
  • 基于JavaScript实现单选框下拉菜单添加文件效果

    2023-07-15 08:46:33
  • Python 的可变和不可变对象详情

    2021-11-24 22:28:33
  • 在Python中使用M2Crypto模块实现AES加密的教程

    2022-09-29 17:43:59
  • ADO组件之分页程序详解

    2008-10-09 12:28:00
  • ASP开发中有用的函数(function)集合(3)

    2008-10-14 17:20:00
  • pytorch实现CNN卷积神经网络

    2023-07-04 20:23:06
  • Yii2框架实现登陆添加验证码功能示例

    2023-11-21 11:36:32
  • ASP图片分页代码 (通用)

    2009-06-22 12:57:00
  • asp之家 网络编程 m.aspxhome.com