欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

實用自動化運維Python腳本分享

 更新時間:2018年06月04日 10:58:56   作者:renfengjun  
今天小編就為大家分享一篇實用自動化運維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) # 登錄遠程服務器
 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) # 登錄遠程服務器
 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")
 #構造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發(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ā)件人,收件人,抄送列表,正文內容,郵件標題
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ā)件人為定值,需要自己修改。

以上這篇實用自動化運維Python腳本分享就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 對Django中的權限和分組管理實例講解

    對Django中的權限和分組管理實例講解

    今天小編就為大家分享一篇對Django中的權限和分組管理實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 解決springboot yml配置 logging.level 報錯問題

    解決springboot yml配置 logging.level 報錯問題

    今天小編就為大家分享一篇解決springboot yml配置 logging.level 報錯問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python實現(xiàn)封裝得到virustotal掃描結果

    python實現(xiàn)封裝得到virustotal掃描結果

    這篇文章主要介紹了python實現(xiàn)封裝得到virustotal掃描結果的方法,是比較實用的技巧,可將掃描結果寫入數(shù)據(jù)庫,需要的朋友可以參考下
    2014-10-10
  • Python保留指定位數(shù)小數(shù)的5種方法總結

    Python保留指定位數(shù)小數(shù)的5種方法總結

    很多小伙伴在學習python的時候可能會遇到對數(shù)據(jù)進行格式化輸出的需求,其中最常見的需求為保留幾位小數(shù),這篇文章主要給大家介紹了關于Python保留指定位數(shù)小數(shù)的5種方法,需要的朋友可以參考下
    2023-08-08
  • python讀取并寫入mat文件的方法

    python讀取并寫入mat文件的方法

    這篇文章主要介紹了python讀取并寫入mat文件的方法,文中給大家提到了python讀取matlab寫的mat文件問題以及解決辦法 ,需要的朋友可以參考下
    2019-07-07
  • Python和Go語言的區(qū)別總結

    Python和Go語言的區(qū)別總結

    在本篇文章里小編給大家分享了關于Python和Go語言的區(qū)別相關知識點,需要的朋友們學習下。
    2019-02-02
  • Python多線程如何同時處理多個文件

    Python多線程如何同時處理多個文件

    這篇文章主要介紹了Python多線程如何同時處理多個文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python 利用jinja2模板生成html代碼實例

    python 利用jinja2模板生成html代碼實例

    這篇文章主要介紹了python 利用jinja2模板生成html代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • Django中提示消息messages的設置方式

    Django中提示消息messages的設置方式

    今天小編就為大家分享一篇Django中提示消息messages的設置方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python使用爬蟲猜密碼

    Python使用爬蟲猜密碼

    我們可以通過python 來實現(xiàn)這樣一個簡單的爬蟲猜密碼功能。下面就看看如何使用python來實現(xiàn)這樣一個功能,對python爬蟲猜密碼相關知識感興趣的朋友參考下吧
    2016-02-02

最新評論