Last active
December 9, 2019 08:26
-
-
Save dgromov/350c6d80f65ba2bedf63ac168bcd788f to your computer and use it in GitHub Desktop.
mssh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Summary: | |
# This script will ssh to multiple boxes by splitting an iterm2 or tmux window evenly | |
# and then logging into all boxes listed. | |
# | |
# Will create a new iterm2 window if you are in iterm2 or a new tmux window if it detects | |
# that you are already in a tmux session | |
# | |
# Install: | |
# copy this script into your $PATH somewhere (Name it mssh) | |
# chmod +x [SOMEPATH]/mssh | |
# profit | |
# | |
# Example usage: | |
# mssh box1 box2 box3 box4 | |
# mssh box{1..4} -- Same as above | |
# | |
# Todo: TMUX integration is a little wonky. If the ssh command fails, the pane executing it will just close. | |
import argparse | |
import subprocess | |
import os | |
def iterm2(addrs): | |
def get_write_tells(): | |
cmd = "" | |
for i, box in enumerate(addrs, 1): | |
cmd += "tell item {} of sessions in current tab of current window to write text \"ssh {}\" \n".format(i, box) | |
return cmd | |
cmd = """osascript<<END | |
tell application "iTerm" | |
activate | |
create window with default profile | |
set paneCount to %s | |
set c to round ((paneCount) ^ 0.5) rounding up | |
set r to round ((paneCount) / c) rounding up | |
set cur to r | |
set newSession to (current session of current window) | |
set theRows to {newSession} | |
repeat r - 1 times | |
tell newSession | |
set newSession to (split horizontally with default profile) | |
set theRows to theRows & {newSession} | |
end tell | |
end repeat | |
repeat with theRow in theRows | |
repeat c - 1 times | |
if cur is equal to (paneCount) then | |
exit repeat | |
end if | |
tell theRow | |
split vertically with default profile | |
end tell | |
set cur to cur + 1 | |
end repeat | |
end repeat | |
%s | |
end tell | |
return | |
END""" % (len(addrs), get_write_tells()) | |
subprocess.call([cmd], shell=True) | |
def tmux(boxes): | |
tmux_cmds = [] | |
new_window = "tmux new-window -n mssh -P \"ssh {}\"".format(boxes[0]) | |
window_name = subprocess.check_output(new_window, shell=True).strip() | |
for box in boxes[1:]: | |
tmux_cmds.append("tmux split-window -h -t {} \"ssh {}\"".format(window_name, box)) | |
tmux_cmds.append("tmux select-layout -t {} tiled > /dev/null".format(window_name)) | |
subprocess.call('\n'.join(tmux_cmds), shell=True) | |
def main(): | |
parser = argparse.ArgumentParser(description='split session and ssh to all boxes') | |
parser.add_argument('boxes', nargs='+', help='list of instances to connect to') | |
args = parser.parse_args() | |
if os.environ.get('TMUX'): | |
tmux(args.boxes) | |
else: | |
iterm2(args.boxes) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment