實(shí)用自動(dòng)化運(yùn)維Python腳本分享
并行發(fā)送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) # 登錄遠(yuǎn)程服務(wù)器
sftp = paramiko.SFTPClient.from_transport(t) # sftp傳輸協(xié)議
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) # 登錄遠(yuǎn)程服務(wù)器
sftp = paramiko.SFTPClient.from_transport(t) # sftp傳輸協(xié)議
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發(fā)送郵件
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")
#構(gòu)造MIMEMultipart對(duì)象做為根容器
main_msg = email.MIMEMultipart.MIMEMultipart()
text_msg = email.MIMEText.MIMEText("您好。<br><br><br><br>"
+ alert_info.title() +
"<br>任鳳軍 <br>"
"xx技術(shù)股份有限公司 <br>"
"手機(jī): xx<br>"
"座機(jī):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)
# 設(shè)置根容器屬性
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發(fā)送郵件
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)
發(fā)件人,收件人,抄送列表,正文內(nèi)容,郵件標(biāo)題
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以及郵件的簽名,還有發(fā)件人為定值,需要自己修改。
以上這篇實(shí)用自動(dòng)化運(yùn)維Python腳本分享就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 10個(gè)Python自動(dòng)化辦公的腳本分享
- Python自動(dòng)化辦公之文件整理腳本分享
- 五個(gè)方便好用的Python自動(dòng)化辦公腳本的實(shí)現(xiàn)
- python http接口自動(dòng)化腳本詳解
- 6個(gè)實(shí)用的Python自動(dòng)化腳本詳解
- python實(shí)現(xiàn)自動(dòng)化腳本編寫(xiě)
- 十個(gè)簡(jiǎn)單使用的Python自動(dòng)化腳本分享
- 教你在Excel中調(diào)用Python腳本實(shí)現(xiàn)數(shù)據(jù)自動(dòng)化處理的方法
- 八個(gè)超級(jí)好用的Python自動(dòng)化腳本(小結(jié))
- Python自動(dòng)化辦公中的應(yīng)用說(shuō)明和腳本示例
相關(guān)文章
一文帶你精通Python中exec函數(shù)的高級(jí)技巧
在?Python?中,exec?是一個(gè)內(nèi)置函數(shù),允許在運(yùn)行時(shí)動(dòng)態(tài)執(zhí)行?Python?代碼,本文將詳細(xì)介紹?Python?exec?函數(shù)的高級(jí)用法,包括動(dòng)態(tài)代碼生成、執(zhí)行外部文件等內(nèi)容,希望對(duì)大家有所幫助2023-11-11
Python自動(dòng)檢測(cè)requests所獲得html文檔的編碼
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Python自動(dòng)檢測(cè)requests實(shí)現(xiàn)獲得html文檔的編碼,文中的示例代碼講解詳細(xì),感興趣的可以了解下2024-11-11
python3 pandas 讀取MySQL數(shù)據(jù)和插入的實(shí)例
下面小編就為大家分享一篇python3 pandas 讀取MySQL數(shù)據(jù)和插入的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Python3交互式shell ipython3安裝及使用詳解
這篇文章主要介紹了Python3交互式shell ipython3安裝及使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Python實(shí)現(xiàn)檢測(cè)文件的MD5值來(lái)查找重復(fù)文件案例
這篇文章主要介紹了Python實(shí)現(xiàn)檢測(cè)文件的MD5值來(lái)查找重復(fù)文件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
解決python中使用PYQT時(shí)中文亂碼問(wèn)題
今天小編就為大家分享一篇解決python中使用PYQT時(shí)中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
python開(kāi)發(fā)之for循環(huán)操作實(shí)例詳解
這篇文章主要介紹了python開(kāi)發(fā)之for循環(huán)操作,以實(shí)例形式較為詳細(xì)的分析了Python中for循環(huán)的具體使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11

