Skip to content

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>.log
 1 #!/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()
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[:-
1]+" server started"
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.

Rétroliens

Pas de rétroliens

Commentaires

Afficher les commentaires en Vue non groupée | Vue groupée

Pas de commentaires

Ajouter un commentaire

Marquer un texte en gras: *mot*, souligner un texte: _mot_.
Les smilies standard comme :-) et ;-) sont convertis en images.
Les adresses Email ne sont pas affichées, et sont seulement utilisées pour la communication.

Pour éviter le spam par des robits automatisés (spambots), merci d'entrer les caractères que vous voyez dans l'image ci-dessous dans le champ de fomulaire prévu à cet effet. Assurez-vous que votre navigateur gère et accepte les cookies, sinon votre commentaire ne pourra pas être enregistré.
CAPTCHA

Form options
tweetbackcheck