Practical automated Operation Python script sharing

Source: Internet
Author: User
This article mainly introduces the practical automation operation of Python script sharing, has a certain reference value, now share to everyone, have the need for friends can refer to

Send SH command in parallel

pbsh.py

#!/usr/bin/python#-*-coding:utf-8-*-import paramikoimport sysimport threading#copy local file to remote Server.def SSH CLIENT_SCP (hostname, port, username, password, local_path, remote_path): t = Paramiko. Transport ((hostname, port)) T.connect (Username=username, Password=password) # Log on to the remote server SFTP = Paramiko. Sftpclient.from_transport (t) # SFTP Transport Protocol 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) # Log on to the remote server SFTP = Paramiko. Sftpclient.from_transport (t) # SFTP Transport Protocol 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 Linetry:file_name = sys.argv[1] cmd= sys.argv[2]except IndexErro R:print ' wrong params! ' print ' Usage: ' print ' batch.py ' $OS _list_file ' "$BATCH _execute_cmd" ' Print ' cat oslist.txt: ' P Rint ' 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_osinfowith 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 Send mail

sendmail.py#!/usr/bin/python#-*-coding:utf-8-*-import smtplibimport email. Mimemultipartimport email. Mimetextimport email. Mimebaseimport sys#from email.mime.application Import mimeapplication#import os.pathdef 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 ("Hello. <br><br><br><br> "+ alert_info.title () +" <br> Ningfeng <br> "xx          Technology Co., Ltd. <br> "Mobile: xx<br>" "Landline:xxx<br>" "Mailbox:xxxx@xx.com<br>" "Address:xxxx<br>" "Zip:130011<br>" "===================================<br>" "", ' H TML ', ' 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) # Set root container Properties 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为完整的需要接收邮件的列表, originally only to hold the CC list, here need to add on the recipient F_cclist.append (to) # Get formatted full text fulltext = Main_ Msg.as_string () # Send mail with 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 ") al    ert = 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) Remarks: SendMail ("Xxxx@gmai L.com ", Mailto,cclist,alert, topic) sender, Recipient, CC list, body content, message labelTitle 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 and the signature of the message, as well as the sender's fixed value, need to be modified by themselves.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.