Skip to content

Instantly share code, notes, and snippets.

@afq984
Created March 16, 2017 22:34
Show Gist options
  • Save afq984/a6f3f1d83ccd72385125cd867464faf6 to your computer and use it in GitHub Desktop.
Save afq984/a6f3f1d83ccd72385125cd867464faf6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import sys
import re
import subprocess
import itertools
_find_unsafe = re.compile(r'[^\w@%+=:,./-]').search
NODES = ['poseidon{:02d}'.format(i) for i in range(1, 8)]
def shlex_quote(s): # new in python 3.3
"""Return a shell-escaped version of the string *s*."""
if not s:
return "''"
if _find_unsafe(s) is None:
return s
# use single quotes, and put single quotes into double quotes
# the string $'b is then quoted as '$'"'"'b'
return "'" + s.replace("'", "'\"'\"'") + "'"
sshcmdarg = shlex_quote(' '.join(shlex_quote(arg) for arg in sys.argv[1:]))
template = '''echo {{hostname}}: {sshcmdarg}
echo {delim}
ssh {{hostname}} -- {sshcmdarg}
read -p "{delim}\n{{hostname}}: command exited with code $?\n"'''.format(
sshcmdarg=sshcmdarg,
delim='=' * 79
)
def make_command(hostname):
return template.format(hostname=hostname)
subprocess.check_call(['tmux', 'new-session', '-d', '-s', 'anc', '-n', NODES[0], make_command(NODES[0])])
for hostname in NODES[1:]:
subprocess.check_call(['tmux', 'new-window', '-n', hostname, make_command(hostname)])
subprocess.check_call(['tmux', 'attach-session', '-d', '-t', 'anc'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment