From 33b556cccaacde82aff46480fd4ffe50bdb5e322 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Tue, 2 Jun 2026 11:04:39 +0200 Subject: [PATCH] add doforall script execute commands or copy files to different machines in one go --- doforall | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 doforall diff --git a/doforall b/doforall new file mode 100755 index 0000000..0aa93d2 --- /dev/null +++ b/doforall @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +import sys +from subprocess import Popen, PIPE + +replaces = 'dmc sans hrpt amor focus tasp camea boa eiger zebra sans-llb' +replace_list = replaces.split() + +if len(sys.argv) < 2: + print(""" +Usage examples: + + > ./doforall - dmc hrpt - ssh linse@ALL cd sea ";" git pull + +Do the git pull in the sea directory of linse@dmc and linse@hrpt + + > ./doforall scp -r myfolder nicos@ALL:/home/software/blabla + +Copy myfolder to the directory /home/software/blabla asking +for a list of hosts. The default will be a list of instrument +computers. +""") + sys.exit(0) + + +def replace(cmd, by): + return [v.replace('ALL', by) for v in cmd] + + +def do(cmd): + out = Popen(cmd, stdout=PIPE).communicate(timeout=5)[0] + return out.decode() + +if sys.argv[1] == '-': + try: + idx = sys.argv[2:].index('-') + except ValueError: + print('missing "-" indiciating the end of replace list') + sys.exit(1) + replace_list = sys.argv[2:2+idx] + cmd = sys.argv[2:idx+1:] +else: + cmd = sys.argv[1:] + print('replace list:') + print(replaces) + print(' ') + print('the first command will be:') + print(f"> {' '.join(replace(cmd, replace_list[0]))}\n") + repl = input('press return to confirm above replace list or enter a new one: ').split() + if repl: + replace_list = repl + +for value in replace_list: + this = replace(cmd, value) + print('>', ' '.join(this)) + print(do(this)) +