python批量运行cmd_批量执行任务的python脚本

how to run the script or commands  on multipl hosts simultaneously

the utility stems from  my previous experiences listed below ,Let's suppose that you are asked to get information that who is logged on the remote hosts and find out disk usages and much more ?what would you do ?if you have multiple servers with similar or identical configurations (such as nodes in a cluster), it's difficult when you need to make configuration modifications from the command line,and how to execute the exact same command on a large number of systems

originally,logging every host and running specified command and then obtain the information ? It's boring and time-consuming ,but it might occur in your routines .

Time to change now ! let's discard old approaches and experience my magical utility now

1)first ,Puppet is an open source configuration management tool. It is written in Ruby and released under the GPL/Apache,it consists of a custom declarative language to describe system configuration. Puppet seems to handle more complex tasks, at the possible expense of more complex configuration,so it's hard to make full use of for beginners,Familiarity and experience with ruby script is required before you are capable of using it .Besides ,puppet mostly focused on configuration management ,it's an disadvantage in comparison with user defined script .

2) using my magical script

Now we are going to see the following tools which does the required job we are looking for:

1) pexpect

here is the brief introduct about pexpect:

pexpect is a program that talks to other interactive programs according to a script. It is useful for running any program which requires interaction between the program and the user. In general, Pexpect is useful for running any program which requires interaction between  the  program  and  the user.

For example:

set/change pwd via script

ftp/ssh authentication

2)other modules

The scrpit includes 2 functions ,the 1st one named ssh_handle(),which will automation interact with remote hosts and respon as if a human were actually typing , the regular expression provided in advance in the script will capture the output generated by expect child process , When output matches a given pattern ,expect will send the corrent answer and command which running on this host.

the reset function will read the ip address and password which stored in the hostlist ,the ip address contains the remote hosts you plan to have the command executed on,this function calls ssh_handle() , run command or scripts at the same time on every hosts ,gather the output and save in the /tmp or any other place you specified before .

This is just small script but you are only limited by your own imagination

specific/obtain/fairly/pretty/verfaction/stem from

to monitor verify database network is up ,most db opens a tcp port on the network interface to provide access for application or manual query

but they might communicate through a method called IPC

database "health" is critical. Typically, IT organisations have dedicated staff, DBA's, to look after them. To relieve them of routine tasks and improve on issue escalation and resolution, automated database monitoring with a tool like Nagios is a must.

It is a good idea to create a dedicated account for monitoring and to restrict it's rights within the database.

Not to mention extremly hard if Nagios runs on a UNIX/Linux platform but the target DB is Microsoft's SQL server. It is much easier if we use JDBC

#!/usr/bin/env python

#-*- coding:UTF-8 -*-

import pexpect,sys,time

import signal,datetime

import multiprocessing

from optparse import OptionParser

class Notcmd: pass

class Mssh(multiprocessing.Process):

def __init__(self,ip,user,passwd,cmd,port,debug):

multiprocessing.Process.__init__(self)

self.ip=ip

self.user=user

self.passwd=passwd

self.cmd=cmd

self.port=port

self.debug=debug

def run(self):

try:

if not cmd: raise Notcmd

now=time.strftime("%Y-%m-%d %H:%M:%S")

print "start time: %s ----- %s " % (now , self.ip)

ssh = pexpect.spawn('ssh %s@%s %s' % (self.user, self.ip, self.port),timeout=120)

if debug == None:

#%(time.strftime("%Y%m%d%H")) #need to import time module

mlog='/tmp/mssh/' + self.ip

f = open(mlog,"a+")

f.write('\n################ %s start at: %s #############\n'%(self.ip,now))

ssh.logfile_read = f

#ssh.logfile_send = f

else :

f=open('/tmp/tmp.log',"a+b")

ssh.logfile_read=sys.stdout

i = ssh.expect(['(?i)password','continue connecting (yes/no)?','[$#]','No route to host','pexpect.TIMEOUT'])

if i == 0:

ssh.sendline(self.passwd)

elif i == 1:

ssh.sendline('yes')

ssh.expect('password')

ssh.sendline(self.passwd)

elif i == 2:

ssh.sendline()

#pass

elif i == 3:

print 'couldn\'t connect to host ',ssh.before

else :

print 'ssh to host timeout ,please check network and pasword '

#ssh.expect('(?i)terminal type\?')

#ssh.sendline('vt100')

if user == 'root' :

pass

else :

ssh.expect('$')

#ssh.logfile_read = None

ssh.sendline('sudo su -')

i = ssh.expect(['[pP]assword','#'])

if i == 0:

ssh.sendline(sudopwd)

else :

ssh.sendline()

ssh.expect('#')

ssh.sendline(cmd)

ssh.expect('#')

ssh.sendline()

f.write('\n#################### %s task finished ##################\n'%self.ip)

except pexpect.EOF:

ssh.close()

except Notcmd:

print "Mssh Error -- %s server xxnot command" % self.ip

sys.exit(1)

except pexpect.TIMEOUT:

ssh.close()

print "Mssh Error -- ssh to %s server connect timeout" % self.ip

sys.exit(1)

except Exception,e :

ssh.close()

print " connect error,",str(e)

sys.exit(1)

else:

ssh.close()

finally:

f.close()

def Argument(Print="No"):

parser = OptionParser(usage="%prog -f hostlist -u user -c \"cmd\" versrion 1.1",version="%prog xiaofu V1.1")

parser.add_option("-f", "--file",dest="File",action="store",help="host list ,which stores ip and password")

parser.add_option("-u", "--user",action="store", dest="User",help="username,root or other users")

parser.add_option("--port",action="store", dest="Port",help="sshd's port ,default:22")

parser.add_option("-d", "--debug",action="store_true", dest="debug",help="Output debug messages")

parser.add_option("-c", "--cmd",action="store", dest="Cmd",

help="commadn to be exected,don't forget to type \"\", e.g \"ifconfig\"\n ")

(options, args) = parser.parse_args()

ArgvDict = vars(options)

if Print == "Yes" : parser.print_help()

return ArgvDict

def signal_handler(signal, frame):

print "Kill All Process"

sys.exit(0)

def Main():

hostlist=[]

start = datetime.datetime.now()

ArgvDict = Argument()

File = ArgvDict["File"]

global user

user = ArgvDict["User"]

global cmd

cmd = ArgvDict["Cmd"]

global debug

debug = ArgvDict["debug"]

global port

port = ArgvDict["Port"]

signal.signal(signal.SIGINT, signal_handler)

if port :

port = "-p "+port

else :

port = ""

file = open(File,"r")

try:

while True:

line = file.readline()

if len(line) == 0:break

i = line.strip()

if i[0] == "#" : continue

host= i.split()

hostlist.append(host)

for i in hostlist:

ip=i[0]

passwd=i[1]

if len(i) == 3 :

global sudopwd

sudopwd=i[2]

p=Mssh(ip,user,passwd,cmd,port,debug)

x=[]

x.append(p)

p.start()

for t in x:

t.join()

end = datetime.datetime.now()

print '\n---------------------------------------------'

print "All tasks finished! time consuming :",end - start,"\n"

except Exception,e:

print "\nError ,reason is : ",str(e),"\n"

finally:

file.close()

if __name__ == "__main__":

if len(sys.argv[1:]) >= 3:

Main()

else:

Argument(Print="Yes")

sys.exit(1)

目的是替代puppet,Cfengine等批量管理的软件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值