python 批量执行脚本(服务端和客户端建立TCP连接)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 批量执行脚本(服务端和客户端建立TCP连接)相关的知识,希望对你有一定的参考价值。
有很多开源的批量部署的工具可以使用,比如puppet, ansible , saltstack , chef 。
但这些工具都有自己的一套语法规则,你得按照他的语法来编写程序,然后批量执行。
那有没有方法可以直接写bash 或者Python脚本,然后批量执行呢?
方法当然是有的,需要自己写程序来传输脚本,拷贝文件,再复杂点,可以自定义脚本式同步还是异步执行,自定义要执行的主机组,等等,需要根据自己的业务需要来想。
这里只是抛砖引玉,我采用建立socket(TCP)连接来批量执行脚本。
服务端脚本,cat server.py
#!/usr/bin/env python
# This script is used to as server , which could transfer script to agents , copy files to agents
import os
import socket
import threading
HostList = {
‘10.1.214.10‘:10001,
‘10.1.214.105‘:10001,
‘10.1.214.106‘:10001
}
class LoadScript(object):
def __init__(self):
pass
def get_input(self):
input_str = raw_input(‘Please input the script(Absolute Path ) :‘)
return input_str
def check_input(self):
while 1:
input_str = self.get_input()
if input_str is None or os.path.isfile(input_str) is False:
print "Wrong Path for script!\n"
else:
print "Your script is %s" % input_str
return input_str
def read_script(self):
script = self.check_input()
try:
f = open(script, ‘r‘)
content = f.readlines()
content_str = ‘‘.join(content)
return content_str
except:
print "Open script %s Error!" % script
sys.exit(1)
class SocketServer(object):
def __init__(self, host, port):
self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.host = host
self.port = port
def connect(self):
self.sock.connect((self.host, self.port))
def send(self, content):
#content = LoadScript().read_script()
print "content:", content
self.sock.send(content)
def receive(self):
result = self.sock.recv(10000)
print result
def close(self):
self.sock.close()
def run(self, content):
self.connect()
self.send(content)
self.receive()
self.close()
content = LoadScript().read_script()
print content
for key in HostList.keys():
server = SocketServer(key, HostList[key])
t = threading.Thread(target = server.run, args = (str(content)))
t.start()
客户端脚本,cat agent.py
#!/usr/bin/env python
# This script is used to as server , which could transfer script to agents , copy files to agents
import socket
import subprocess
import os
class SocketAgent(object):
def __init__(self, port):
self.port = port;
self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
self.sock.bind( ("", port) )
self.sock.listen( 5 )
def listen(self):
pass
def receive(self):
self.clisock, (remote_host, remote_port) = self.sock.accept()
content = self.clisock.recv(10000)
#clisock.close()
return content
def set_file(self, file = "/var/tmp/script"):
return file
def convert_to_script(self):
content = self.receive()
script = self.set_file()
f = open(script, ‘w‘)
f.write("#!/usr/bin/bash\n" + content + "\n")
f.close()
def execute(self):
script = self.set_file()
cmd = ‘bash %s‘ % str(script)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
out, err = p.communicate()
print "output:", out
print "err:", err
return err
def response(self):
if self.execute() is None:
self.clisock.send("Script run successfully")
else:
self.clisock.send("Script run failed")
agent = SocketAgent(10001)
agent.convert_to_script()
agent.execute()
agent.response()
先运行客户端脚本,在执行服务端脚本,在服务端指定要执行的脚本。
考虑到用建立socket连接的方法不是很方便,后续会考虑改成其他协议,比如ftp等。
若大家有什么想法,欢迎随时留言沟通。
本文出自 “Linux运维” 博客,谢绝转载!
以上是关于python 批量执行脚本(服务端和客户端建立TCP连接)的主要内容,如果未能解决你的问题,请参考以下文章