Amazing-Python-Scripts

Форк
0
40 строк · 1.1 Кб
1
import argparse
2
import os
3

4
SSH_TEMPLATE = """
5
HOST {name}
6
    HostName {hostname}
7
    User {user}
8
    Port {port}
9
"""
10

11

12
def args_to_obj(args):
13
    return SSH_TEMPLATE.format(**args.__dict__)
14

15

16
def add_to_conf(conf, obj):
17
    conf = os.path.expanduser(conf)
18
    with open(conf, 'a') as f:
19
        f.write(obj)
20

21

22
def main():
23
    parser = argparse.ArgumentParser(
24
        prog="Adds ssh hosts to the ssh config file.")
25
    parser.add_argument('name', help="Name of the Host to add to the config.")
26
    parser.add_argument('hostname', help="Hostname/IP address of the host.")
27
    parser.add_argument('--user', default='root',
28
                        help="The user to connect with. Defaults to root.")
29
    parser.add_argument('--port', default=22, type=int,
30
                        help="The port to connect to. Defaults to 22.")
31
    parser.add_argument('--conf', default='~/.ssh/config',
32
                        help="The path to the ssh config file. Defaults to ~/.ssh/config.")
33

34
    args = parser.parse_args()
35
    obj = args_to_obj(args)
36
    add_to_conf(args.conf, obj)
37

38

39
if __name__ == '__main__':
40
    main()
41

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.