Last active
October 16, 2018 16:51
-
-
Save AbdouSeck/cb50fbbb2c99b4d9cd3e62305b3c6716 to your computer and use it in GitHub Desktop.
Attempt to answer https://stackoverflow.com/q/52838014/3135417
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 python3 | |
""" | |
Script to test or run commands on given servers. | |
./this_script.py test # To test all servers | |
./this_script.py run --id 127.0.0.1 --command "echo hello world" | |
""" | |
from argparse import ArgumentParser, RawDescriptionHelpFormatter as RDHF | |
def test_servers(servers): | |
""" | |
Given a list of servers, let's test them! | |
""" | |
for server in servers: | |
print('Just tested server {s}'.format(s=server)) | |
def do_actual_work(server_id, command): | |
""" | |
Given a server ID and a command, let's run the command on that server! | |
""" | |
print('Connected to server {s}'.format(s=server_id)) | |
print('Ran command {c} successfully'.format(c=command)) | |
if __name__ == '__main__': | |
parser = ArgumentParser(description=__doc__, formatter_class=RDHF) | |
subs = parser.add_subparsers() | |
subs.required = True | |
subs.dest = 'run or test' | |
test_parser = subs.add_parser('test', help='Test all servers') | |
test_parser.set_defaults(func=test_servers) | |
run_parser = subs.add_parser('run', help='Run a command on the given server') | |
run_parser.add_argument('-i', '--id', | |
help='The ID of the server to connect to and run commands', | |
required=True) | |
run_parser.add_argument('-c', '--command', | |
help='The command to run', | |
required=True) | |
run_parser.set_defaults(func=do_actual_work) | |
args = parser.parse_args() | |
if args.func.__name__ == 'test_servers': | |
all_servers = ['127.0.0.1', '127.0.0.2'] | |
test_servers(all_servers) | |
else: | |
do_actual_work(args.id, args.command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment