Amazing-Python-Scripts
40 строк · 1.1 Кб
1import argparse2import os3
4SSH_TEMPLATE = """5HOST {name}
6HostName {hostname}
7User {user}
8Port {port}
9"""
10
11
12def args_to_obj(args):13return SSH_TEMPLATE.format(**args.__dict__)14
15
16def add_to_conf(conf, obj):17conf = os.path.expanduser(conf)18with open(conf, 'a') as f:19f.write(obj)20
21
22def main():23parser = argparse.ArgumentParser(24prog="Adds ssh hosts to the ssh config file.")25parser.add_argument('name', help="Name of the Host to add to the config.")26parser.add_argument('hostname', help="Hostname/IP address of the host.")27parser.add_argument('--user', default='root',28help="The user to connect with. Defaults to root.")29parser.add_argument('--port', default=22, type=int,30help="The port to connect to. Defaults to 22.")31parser.add_argument('--conf', default='~/.ssh/config',32help="The path to the ssh config file. Defaults to ~/.ssh/config.")33
34args = parser.parse_args()35obj = args_to_obj(args)36add_to_conf(args.conf, obj)37
38
39if __name__ == '__main__':40main()41