Files
servicemanager/doforall
T
zolliker 33b556ccca add doforall script
execute commands or copy files to different machines in one go
2026-06-02 11:04:39 +02:00

57 lines
1.4 KiB
Python
Executable File

#!/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))