Send SH command in parallel
pbsh.py
#!/usr/bin/python #-*-coding:utf-8-*-import paramiko Import and 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) # 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 (Hostna Me, 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 (Hostnam E, 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 A-conn
Ection, need a simple interaction. Line=stdout.read () S.close () print (hostname+ ":") Print line try:file_name = sys.argv[1] cmd= sys.a
RGV[2] except indexerror:print ' wrong params! ' print ' Usage: ' print ' batch.py ' $OS _list_file ' "$BATCH _execute_cmd" ' Print ' cat oslist.txt: ' print ' 19 2.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/wo Rkspace/oslist.txt "" Df-h "' sys.exit () #file_name = sys.argv[1] #cmd = sys.argv[2] #maintenance_osinfo with open (fi Le_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 (SPL ITS_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 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_CCLI St, 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> "" ", ' 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) # Set root container Properties main_msg[' from '] = from Mai N_msg[' to '] = to main_msg[' Cc ' = ', '. Join (f_cclist) main_msg[' Subject '] = f_subject main_msg[' Date '] = email. Utils.formatdate () #f_cclist为完整的需要接收邮件的列表, which would have only a CC list, where you need to add f_cclist.append (to) # Formatted full text fulltext = Main_msg.as_string () # Send mail with SMTP try:server.sendmail (from, F_cclist, Fulltext) Finally:se Rver.quit () if __name__ = = "__main__": #sys. Setdefaultencoding (' utf-8 ') message= [' Usage: ', ' SENDM ail.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) Note: SendMail ("xxxx @gmail. com ", Mailto,cclist,alert, topic) senders, recipients, cc lists, body contents, Message Headers Usage:sendmail.py" topic "" Mail body text "" Mail t O "Examples of usage:sendmail.py" topic "" Hello World "" 14638852@qq.com "/sendmail.py" topic "" Hel "
Lo World "14638852@qq.com"
SMTP and the signature of the message, as well as the sender fixed value, need to modify their own.