この記事は、主に実用的な自動化された運用とメンテナンスの Python スクリプトの共有を紹介します。これは、必要な友人と共有して参照することができます
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 と電子メール署名を送信しますが、送信者の固定値は自分で変更する必要があります。
関連する推奨事項:
Linux で Python スクリプトのタスクをセルフスタートしてスケジュールする方法の詳細な例 IIS の CGI モードで Python スクリプトを実行するチュートリアルの例を共有します
以上が実践的な自動運用保守 Pythonスクリプト共有の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonは、データサイエンス、Web開発、自動化タスクに適していますが、Cはシステムプログラミング、ゲーム開発、組み込みシステムに適しています。 Pythonは、そのシンプルさと強力なエコシステムで知られていますが、Cは高性能および基礎となる制御機能で知られています。

2時間以内にPythonの基本的なプログラミングの概念とスキルを学ぶことができます。 1.変数とデータ型、2。マスターコントロールフロー(条件付きステートメントとループ)、3。機能の定義と使用を理解する4。

Pythonは、Web開発、データサイエンス、機械学習、自動化、スクリプトの分野で広く使用されています。 1)Web開発では、DjangoおよびFlask Frameworksが開発プロセスを簡素化します。 2)データサイエンスと機械学習の分野では、Numpy、Pandas、Scikit-Learn、Tensorflowライブラリが強力なサポートを提供します。 3)自動化とスクリプトの観点から、Pythonは自動テストやシステム管理などのタスクに適しています。

2時間以内にPythonの基本を学ぶことができます。 1。変数とデータ型を学習します。2。ステートメントやループの場合などのマスター制御構造、3。関数の定義と使用を理解します。これらは、簡単なPythonプログラムの作成を開始するのに役立ちます。

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法

Python 3.6のピクルスファイルのロードレポートエラー:modulenotFounderror:nomodulenamed ...

風光明媚なスポットコメント分析におけるJieba Wordセグメンテーションの問題を解決する方法は?風光明媚なスポットコメントと分析を行っているとき、私たちはしばしばJieba Wordセグメンテーションツールを使用してテキストを処理します...


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

メモ帳++7.3.1
使いやすく無料のコードエディター

Dreamweaver Mac版
ビジュアル Web 開発ツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン
