Batch/concurrent processing using python

Source: Internet
Author: User

I have been studying python recently and feel very good, especially for Batch/concurrent processing. It is very convenient to implement and much more convenient than shell. I just learned python for a month and keep a record here. The following is a piece of code. More suggestions/comments ~ (Qq group exchange: 24967504)

Requirement: View/compare the status of online servers in batches, such as uptime, df-h, and MD5sum files. This command is intended to be run through parameter passing and password interactive input (three times without exit ). Further improvements ~~

#-*-Coding: UTF-8 -*-
#! /Usr/bin/python

Import paramiko
Import threading
Def ssh2 (ip, username, passwd, cmd ):
Try:
Ssh = paramiko. SSHClient ()
Ssh. set_missing_host_key_policy (paramiko. AutoAddPolicy ())
Ssh. connect (ip, 22, username, passwd, timeout = 5)
For m in cmd:
Stdin, stdout, stderr = ssh.exe c_command (m)
Stdin. write ("Y ")
Out = stdout. readlines ()
For o in out:
Print o,
Print '[OK] % s' % (ip ),
Print '============================================== ========================================='
Ssh. close ()
Except t:
Print '[Error] % s' % (ip ),
Print '============================================== ========================================='
Def main ():
Cmd = ['uptime'] # Run the command
Username = "root"
Passwd = "password"
Threads = [4]
F = file('list.txt ') # ip address list
While True:
Ip = f. readline ()
If len (ip) = 0:
Break
A = threading. Thread (target = ssh2, args = (ip, username, passwd, cmd ))
A. start ()
F. close ()

If _ name _ = '_ main __':
Main ()

Execution result:

[Root @ bw-vm-soft ~] # Python ssh2.py
13:31:28 up 514 days, 1 user, load average: 10.27, 9.44, 9.03
[OK] 192.168.13.116
========================================================== ======================================
13:31:28 up 514 days, 1 user, load average: 5.99, 6.05, 6.46
[OK] 192.168.13.117
========================================================== ======================================

For the previous Code, the following problems have been improved:
1. Remove the plaintext password and enter the password through interaction
2. Execute related commands by passing parameters to increase flexibility
3. Exception Handling
4. For other issues to be found, contact the QQ group: 24967504

Code:

#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
Import sys
Import paramiko
Import threading
Import getpass
Def main ():
Try:
Cmd = sys. argv [1:]
Username = "root"
Passwd = getpass. getpass ('Please input password :')
Threads = [4]
F = file('list.txt ')
While True:
Ip = f. readline ()
If len (ip) = 0:
Break
A = threading. Thread (target = ssh2, args = (ip, username, passwd, cmd ))
A. start ()
F. close ()
Except t:
Pass

Def ssh2 (ip, username, passwd, cmd ):
Try:
Ssh = paramiko. SSHClient ()
Ssh. set_missing_host_key_policy (paramiko. AutoAddPolicy ())
Ssh. connect (ip, 22, username, passwd, timeout = 5)
For m in cmd:
Stdin, stdout, stderr = ssh.exe c_command (m)
Stdin. write ("Y ")
Out = stdout. readlines ()
For o in out:
Print o,
Print '[OK] % s' % (ip ),
Print '============================================== ========================================='
Ssh. close ()
Except t:
Print '[Error] % s' % (ip ),
Print '============================================== ========================================='

If _ name _ = '_ main __':
Main ()

Execution result:
[Root @ bw-vm-soft test] # python ssh2.py 'md5sum/usr/local/webserver/nginx/conf/nginx. conf'
Please input password:
33066988953224e936028b341f4e1337/usr/local/webserver/nginx/conf/nginx. conf
[OK] 192.168.13.116
========================================================== ======================================
33066988953224e936028b341f4e1337/usr/local/webserver/nginx/conf/nginx. conf
[OK] 192.168.13.117
========================================================== ======================================

If the password is incorrect
[Error] 192.168.13.116
========================================================== ======================================
[Error] 192.168.13.117

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.