Execute shell commands in bulk

Source: Internet
Author: User
Tags haproxy

Although the application of automation such as puppet saltstack in the environment is now realized, it is unavoidable to write some simple scripts to execute shell commands in batch.


Python Paramiko module is currently used more comfortable module, the execution of the command is basically no need to convert, directly throw the shell command in execution OK


In a simple example, 10 threads perform SSH or SCP actions at the same time, and timeout time is not set, such as a lengthy non-response that causes script execution problems:


#!/usr/bin/python

# _*_ Coding:utf-8 _*_


Import Paramiko

Import Sys

Import logging

From Multiprocessing.dummy import Pool as ThreadPool

Import re

Import OS


Try

Fun_name = sys.argv[1]

File = open (Sys.argv[2])

Except Exception,e:

Print "" "Use like:

#copy dir or file

shell_exec_map.py SCP ip_file source_file des_file

#do cmd in Host

exec_map.py ssh ip_file ' yum install-y Zabbix '

"""

Exit ()


Logging.basicconfig (level=logging. DEBUG,

format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',

Datefmt= '%a,%d%b%Y%h:%m:%s ',

Filename= ' Exec_map.log ',

Filemode= ' A + ')


#ssh Key File Location

Privatekeyfile = Os.path.expanduser ('/root/.ssh/id_rsa ')

MyKey = Paramiko. Rsakey.from_private_key_file (Privatekeyfile)

#############################

#对ssh key to set the password method

#privatekeyfile = Os.path.expanduser ('/root/.ssh/id_rsa ')

#mykey = Paramiko. Rsakey.from_private_key_file (privatekeyfile,password= ' password ')

#############################


def getcmdinfo (Host_ip,cmd):

Print Host_ip

Try

c = Paramiko. Sshclient ()

C.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())

##################

#密码登录方式

#c. Connect (host_ip,22, ' root ', password)

##################

C.connect (host_ip,22, ' root ', Pkey=mykey)

Stdin,stdout,stderr = C.exec_command (cmd)

info = Stdout.read ()

C.close ()

return info

Except Exception as E:

Logging.error (' getcmdinfo IP%s%s ' ERROR:%s '% (host_ip,cmd,e))


def postfiletoremote (Host_ip,localpath,remotepath):

Print Host_ip

Try

t = Paramiko. Transport ((host_ip,22))

##################

#密码登录方式

#t. Connect (username= ' root ', password= ')

##################

T.connect (username= ' root ', Pkey=mykey)

SFTP = Paramiko. Sftpclient.from_transport (t)

Sftp.put (Localpath,remotepath)

Sftp.close ()

T.close ()

Except Exception,e:

Logging.error (' scp_cmd ip%s soure:%s des:%s error:%s '% (host_ip,localpath,remotepath,e))


#10个并发同时进行

Pool = ThreadPool (10)

Line_list = []

For line in File.readlines ():

Line_list.append (Line.strip ())

if fun_name = = ' SCP ':

Try

Pool.map (Scpfile,line_list)

Logging.debug (' scpfile IP file%s Source%s Dec%s '% (Sys.argv[2],sys.argv[3],sys.argv[4])

Except Exception as E:

Logging.error (' scpfile map error:%s '% e)

elif fun_name = = ' ssh ':

Try

If Pattern_cmd.findall (Sys.argv[3]):

Print "can ' t use RM command!"

Logging.debug (' sshcmd ip file%s cmd%s '% (Sys.argv[2],sys.argv[3]))

Else

Pool.map (Sshcmd,line_list)

Logging.debug (' sshcmd ip file%s cmd%s '% (Sys.argv[2],sys.argv[3]))

Except Exception as E:

Logging.error (' sshcmd map error:%s '% e)

Pool.close ()

Pool.join ()



ansible batch execution script based on Paramiko module, remote execution

1, install ansible software, configure the Epel source, or online download ansible-1.7.2

2, Configuration node, IP (hostname requires the main function of the normal analysis)

Vi/etc/ansible/hosts

[Other]
Zabbix-127023.xxx.com
Haproxy-127021.xxx. com
Controller-127022.xxx. com
Haproxy-127021.xxx.com

3. Perform a ping test ( with the environment variable . ~/.bash_profile)

Ansible-m shell-a '. ~/.bash_profile &&ping-c 2 10.197.128.1 ' other

controller-129022.xxx.com | Success | Rc=0 >>
PING 10.197.128.1 (10.198.127.1) bytes of data.
Bytes from 10.198.127.1:icmp_seq=1 ttl=254 time=0.466 ms
Bytes from 10.198.127.1:icmp_seq=2 ttl=254 time=0.409 ms

---10.198.127.1 ping statistics---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
RTT Min/avg/max/mdev = 0.409/0.437/0.466/0.035 ms

pinsh-127024.xxx.com | FAILED = SSH encountered an unknown error during the connection. We recommend you re-run the command USING-VVVV, which'll enable SSH debugging output to help diagnose the issue


Ansible method There's an inconvenient place. The first time you execute it, you need to confirm it manually. Python scripts do not require manual confirmation

Execute shell commands in bulk

Related Article

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.