SRC : Simultaneous Remote Command
When you manage several server, you can want to run the same command on all the server (for security update for exemple).
I develop a small Python script to send command on a list of server. This script use ssh to run the command on all server. For example, it can manage to run a security update on Debian server with the send of "apt-get update;apt-get -y upgrade" command.
The Script
The Script use thread to send the command to each server simultaneously. The output of command is write to a file named <server_name>.log1 #!/usr/bin/env python
2
3 import os
4 from threading import Thread
5
6
7 class MyThread(Thread):
8 def __init__(self,serverName,command):
9 Thread.__init__(self)
10 self.serverName=serverName
11 self.command=command
12
13 def run(self):
14 cmd_ssh = "ssh root@"+self.serverName+ " '"+self.command+"'"
15 logfile = open(self.serverName+".log",'w')
16 for line in os.popen(cmd_ssh).readlines():
17 logfile.write(line)
18 logfile.close()
19
20 cmdFile = open( "cmd")
21 cmd = cmdFile.readline ()
22 cmdFile.close() 1]+" server started"
23
24 servers = open("servers") )
25 server = servers.readline()
26 while server:
27 thrd = MyThread(server[:-1], cmd[ :-1])
28 thrd.start(
29 print "Thread for "+server[:-
30 server = servers.readline()
31 servers.close()
Use
To use the script, download it here and create the following file :- servers : one line by server name,
- cmd : one line with the command to send (if you want a list of command, separate each command by ; on one line).
The script use ssh, but it cannot send the passphrase to ssh. Also you need to use the authentification with certificat and if you have a passphrase, you must use ssh-agent.
And now run the src.py command and wait the end of each command on each server.
Commentaires
Afficher les commentaires en Vue non groupée | Vue groupée