advanced-ssh-config를 사용하여 .ssh/config를 매우 깔끔하게 만드는 방법

advanced-ssh-config 은 YAML로 .ssh/config 설정을 쓰고 엉망이 되기 쉬운 설정을 깨끗하게 쓸 수 있는 Go 언어로 쓰여진 도구입니다 🖥



주요 기능


  • 정규식
  • gateways: 투명 SSH 연결 체인
  • includes: 파일 포함
  • local command execution: RemoteCommand 반대
  • templates : 호스트와 동일하지만 템플릿 호스트에 직접 연결할 수 없습니다. 상속에 최적.
  • 상속 : 호스트가 호스트 호스트 또는 템플릿에서 상속됨 ​​
  • variable expansion : 환경 변수를 확장 할 수 있습니다
  • smart proxycommand : 기본 tcp 연결 (가능하면 netcat과 socat을 기본 폴백으로 사용)
  • rate limit: 호스트 단위 또는 전역 속도 제한
  • JSON 출력 : JSON에서 출력
  • Graphviz : 호스트 정보를 graphviz로 출력한다

  • 설치



    Go에서 설치


    go get -u github.com/moul/advanced-ssh-config/cmd/assh
    

    macOS 환경


    brew install assh
    

    기타 방법



    htps : // 기주 b. 코 m / 이미 l / 아 d 에서 바이너리 가져오기

    ssh 명령을 alias에서 assh를 사용하도록 설정



    필수는 아니지만 자동으로 .ssh/config 를 생성할 수 있도록 하기 위해 필요.
    alias ssh="assh wrapper ssh"
    

    참고: ssh는 고급 패턴을 이해하지 못합니다. 이 제한을 피하기 위해 assh는 알려진 호스트 목록을 유지하고 .ssh/config를 확장 된 알려진 모든 호스트에서 재생성합니다.

    wrapper가 없으면 .ssh/config는 새 호스트에 처음 연결할 때 오래된 위험이 있으므로 명령을 다시 시작해야합니다. wrapper에서 ssh는 업데이트된 .ssh/config 파일을 항상 사용합니다.

    설정 예


    ~/.ssh/assh.yaml 를 새로 작성하여 설정을 작성합니다.

    ~/.ssh/assh.yaml
    hosts:
      jump.k8s.aws:
        HostName: bastion.ap-northeast-1.elb.amazonaws.com
        User: admin
        IdentityFile: ~/.ssh/jump_k8s.pem
    
      node01.k8s:
        Hostname: 172.10.10.10
        inherits: node.k8s
    
      node02.k8s:
        Hostname: 172.10.10.11
        inherits: node.k8s
    
      node03.k8s:
        Hostname: 172.10.10.12
        inherits: node.k8s
    
    templates:
      node.k8s:
        User: admin
        Gateways: jump.k8s.aws
    

    .ssh/config 생성



    이 단계에서는 .ssh/config를 덮어씁니다. 항상 기존 .ssh/config를 백업합시다!
    assh config build > ~/.ssh/config
    

    복잡한 설정 예


    hosts:
    
      homer:
        # ssh homer ->  ssh 1.2.3.4 -p 2222 -u robert
        Hostname: 1.2.3.4
        User: robert
        Port: 2222
    
      bart:
        # ssh bart ->   ssh 5.6.7.8 -u bart           <- direct access
        #            or ssh 5.6.7.8/homer -u bart     <- using homer as a gateway
        Hostname: 5.6.7.8
        User: bart
        Gateways:
        - direct                   # tries a direct access first
        - homer                    # fallback on homer gateway
    
      maggie:
        # ssh maggie ->   ssh 5.6.7.8 -u maggie       <- direct access
        #              or ssh 5.6.7.8/homer -u maggie   <- using homer as a gateway
        User: maggie
        Inherits: bart             # inherits rules from "bart"
    
      bart-access:
        # ssh bart-access ->  ssh home.simpson.springfield.us -u bart
        Inherits:
        - bart-template
        - simpson-template
    
      lisa-access:
        # ssh lisa-access ->  ssh home.simpson.springfield.us -u lisa
        Inherits:
        - lisa-template
        - simpson-template
    
      marvin:
        # ssh marvin    -> ssh marvin    -p 23
        # ssh sad-robot -> ssh sad-robot -p 23
        # ssh bighead   -> ssh bighead   -p 23
        # aliases inherit everything from marvin, except hostname
        Port: 23
        Aliases:
        - sad-robot
        - bighead
    
      dolphin:
        # ssh dolphin   -> ssh dolphin -p 24
        # ssh ecco      -> ssh dolphin -p 24
        # same as above, but with fixed hostname
        Port: 24
        Hostname: dolphin
        Aliases: ecco
        RateLimit: 10M # 10Mbytes/second rate limiting
    
      schooltemplate:
        User: student
        IdentityFile: ~/.ssh/school-rsa
        ForwardX11: yes
    
      schoolgw:
        # ssh school ->   ssh gw.school.com -l student -o ForwardX11=no -i ~/.ssh/school-rsa
        Hostname: gw.school.com
        ForwardX11: no
        Inherits: schooltemplate
    
      "expanded-host[0-7]*":
        # ssh somehost2042 ->       ssh somehost2042.some.zone
        Hostname: "%h.some.zone"
    
      vm-*.school.com:
        # ssh vm-42.school.com ->   ssh vm-42.school.com/gw.school.com -l student -o ForwardX11=yes -i ~/.ssh/school-rsa
        Gateways: schoolgw
        Inherits: schooltemplate
        # do not automatically create `ControlPath` -> may result in error
        ControlMasterMkdir: true
    
      "*.shortcut1":
        ResolveCommand: /bin/sh -c "echo %h | sed s/.shortcut1/.my-long-domain-name.com/"
    
      "*.shortcut2":
        ResolveCommand: /bin/sh -c "echo $(echo %h | sed s/.shortcut2//).my-other-long-domain-name.com"
    
      "*.scw":
        # ssh toto.scw -> 1. dynamically resolves the IP address
        #                 2. ssh {resolved ip address} -u root -p 22 -o UserKnownHostsFile=null -o StrictHostKeyChecking=no
        # requires github.com/scaleway/scaleway-cli
        ResolveCommand: /bin/sh -c "scw inspect -f {{.PublicAddress.IP}} server:$(echo %h | sed s/.scw//)"
        User: root
        Port: 22
        UserKnownHostsFile: /dev/null
        StrictHostKeyChecking: no
    
      my-env-host:
        User: user-$USER
        Hostname: ${HOSTNAME}${HOSTNAME_SUFFIX}
    
    templates:
      # Templates are similar to Hosts; you can inherit from them
      # but you cannot ssh to a template
      bart-template:
        User: bart
      lisa-template:
        User: lisa
      simpson-template:
        Host: home.simpson.springfield.us
    
    defaults:
      # Defaults are applied to each hosts
      ControlMaster: auto
      ControlPath: ~/tmp/.ssh/cm/%h-%p-%r.sock
      ControlPersist: yes
      Port: 22
      User: bob
      Hooks:
        # Automatically backup ~/.ssh/config
        BeforeConfigWrite:
          - 'exec set -x; cp {{.SSHConfigPath}} {{.SSHConfigPath}}.bkp'
    
        AfterConfigWrite:
          # Concat another `ssh_config` file with the one just generated by `assh`
          - 'exec cat ~/.ssh/my-heroku-generated-config >> {{.SSHConfigPath}}'
    
          # Alert me with a Desktop notification
          - notify "{{.SSHConfigPath}} has been rewritten"
    
        OnConnect:
          # Log internal information to a file
          - 'exec echo {{.}} | jq . >> ~/.ssh/last_connected_host.txt'
    
          # Alert me with a Desktop notification
          - notify New SSH connection to {{.Host.Prototype}} at {{.Stats.ConnectedAt}}
    
          # Write the host prototype to the terminal stderr
          - write New SSH connection to {{.Host.Prototype}}
    
        OnDisconnect:
          # write on terminal and in a Desktop notification some statistics about the finished connection
          - "write  SSH connection to {{.Host.HostName}} closed, {{.Stats.WrittenBytes }} bytes written in {{.Stats.ConnectionDuration}} ({{.Stats.AverageSpeed}}bps)"
          - "notify SSH connection to {{.Host.HostName}} closed, {{.Stats.WrittenBytes }} bytes written in {{.Stats.ConnectionDuration}} ({{.Stats.AverageSpeed}}bps)"
    
    includes:
    - ~/.ssh/assh.d/*.yml
    - /etc/assh.yml
    - $ENV_VAR/blah-blah-*/*.yml
    
    ASSHBinaryPath: ~/bin/assh  # optionally set the path of assh
    

    assh 명령 옵션


    NAME:
       assh - advanced ssh config
    
    USAGE:
       assh [global options] command [command options] [arguments...]
    
    VERSION:
    2.7.0 (HEAD)
    
    AUTHOR(S):
       Manfred Touron <https://github.com/moul/advanced-ssh-config>
    
    COMMANDS:
       ping          Send packets to the SSH server and display statistics
       info          Display system-wide information
       config        Manage ssh and assh configuration
       sockets       Manage control sockets
       help, h       Shows a list of commands or help for one command
    
    GLOBAL OPTIONS:
      --config value, -c value       Location of config file (default: "~/.ssh/assh.yml") [$ASSH_CONFIG]
      --debug, -D                    Enable debug mode [$ASSH_DEBUG]
      --verbose, -V                  Enable verbose mode
      --help, -h                     show help
      --version, -v                  print the version
    

    자세한 문서



    GitHub moul/advanced-ssh-config

    좋은 웹페이지 즐겨찾기