• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically generate ssh config files for your cloud servers

generate-ssh-configs

Description

generate-ssh-configs reads cloud providers API and generates ssh config files for you. This is especially useful when dealing with tens or hundreds of servers.

The program writes to stdout. Using shell redirection we can write persistent config files and include them using the ssh Include directive.

Examples

Prerequisites

Install generate-ssh-configs

go get github.com/danihodovic/generate-ssh-configs

Ensure your ssh config includes all the config files in the ssh directory.

cat ~/.ssh/.config
# ...at the bottom of the file...
Include ~/.ssh/config-*

Ensure your AWS credentials have been configured if using AWS

See https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html

Ensure $DIGITAL_OCEAN_TOKEN is set if using DigitalOcean

See https://www.digitalocean.com/docs/api/create-personal-access-token/

Generate ssh configs for all AWS instances

Uses the current AWS region (AWS_DEFAULT_REGION) and generates all configs using the EC2 API.

generate-ssh-configs aws --prefix myservers --user myuser > ~/.ssh/config-myservers

AWS multi-region, multi-environment setup

Using multiple regions, environments and jumphosts for each region and environment. This works if all of your environments are contained in a single AWS account and separated by VPC and tags.

# Generate configs for dev,test,prod in eu-west-1
AWS_DEFAULT_REGION=eu-west-1 generate-ssh-configs aws \
    --prefix myorg-dev-eu-west-1 \
    --filters 'Name=tag:Environment,Values=dev'
    --jumphost jumphost --user dani \
    > ~/.ssh/config-myorg-dev-eu-west-1

AWS_DEFAULT_REGION=eu-west-1 generate-ssh-configs aws \
    --prefix myorg-prod-eu-west-1 \
    --filters 'Name=tag:Environment,Values=prod' \
    --jumphost jumphost \
    --user dani  \
    > ~/.ssh/config-myorg-prod-eu-west-1


# Generate configs for dev,test,prod in ap-south 1
AWS_DEFAULT_REGION=ap-south-1 generate-ssh-configs aws \
    --prefix myorg-dev-ap-south-1 \
    --filters 'Name=tag:Environment,Values=dev' \
    --jumphost jumphost \
    --user dani \
    > ~/.ssh/config-myorg-dev-ap-south-1

AWS_DEFAULT_REGION=ap-south-1 generate-ssh-configs aws \
    --prefix myorg-prod-ap-south-1 \
    --filters 'Name=tag:Environment,Values=prod' \
    --jumphost jumphost \
    --user dani  \
    > ~/.ssh/config-myorg-prod-ap-south-1

Usage with FZF

SSH configs work beautifully with FZF since the servers are essentially a list. Using some bash magic we can quickly to select the server we want to ssh to.

Here is an example of using fzf and zsh to quickly select a server. Pressing Ctrl+s in a terminal launches fzf-ssh. Place the script in your ~/.zshrc

stty stop undef
function fzf-ssh {
  all_matches=$(grep -P -r "Host\s+\w+" ~/.ssh/ | grep -v '\*')
  only_host_parts=$(echo "$all_matches" | awk '{print $NF}')
  selection=$(echo "$only_host_parts" | fzf)
  echo $selection

  if [ ! -z $selection ]; then
    BUFFER="ssh $selection"
    zle accept-line
  fi
  zle reset-prompt
}
zle     -N     fzf-ssh
bindkey "^s" fzf-ssh

Features

  • AWS
    • Uses name tags to identify instances.
    • Works with jumphosts or bastion hosts.
    • Uses the public IP if
      • the instance is in a public subnet
      • the security group allows ingress port 22 from the public internet
      • the security group allows ingress port 22 from subnet provided via --subnet flag
    • Otherwise it uses the private IP and routes through the jumphost if one is configured.
  • DigitalOcean

More Repositories

1

celery-exporter

A Prometheus exporter for Celery metrics
Jsonnet
386
star
2

django-allauth-ui

Nice looking templates for django-allauth
HTML
118
star
3

mjml-server

MJML wrapped in Express for use over HTTP
JavaScript
39
star
4

django_admin_chart_js

An example repo showing how to add Chart.js to Django admin
Python
37
star
5

django-postgres-fulltext-search

How to optimize Postgres full text search in Django
Python
20
star
6

borg-exporter

Prometheus exporter for Borg backups.
Python
16
star
7

testing-cloudflare-workers

Examples on how to test Cloudflare workers with Mocha.js and Chai.js
JavaScript
15
star
8

vim-ansible-vault

Manage vault secrets in vim
Python
11
star
9

.dotfiles

My dotfiles
Shell
9
star
10

docker-sopcast

Sopcast containerized
Shell
8
star
11

drone-promote

A Drone plugin which triggers promotions (deployments).
Python
5
star
12

django-disable-cache-headers

Middleware that disables caching headers during development in Django.
Python
4
star
13

docker-ansible

Dockerfile
4
star
14

ansible-role-borgbackup

Installs borgbackup and borgmatic and configures automated backups.
Python
4
star
15

steeef

Zsh steeef theme as a standalone repository
4
star
16

django.wtf

Django Package Index
Python
4
star
17

ansible-samson

Ansible modules for Samson
Python
3
star
18

scdl-docker

3
star
19

django-toolshed

Python
3
star
20

ansible-role-django

Python
3
star
21

ansible-role-openproject

An opinionated role that installs OpenProject as a single Docker container.
3
star
22

vim-snippets

Vim Snippet
2
star
23

ansible-role-redis

Python
2
star
24

cheats

2
star
25

superrequests

Python
2
star
26

dht

Python
2
star
27

nodejs-require.vim

Go to the file for the require() statement under the cursor.
Python
2
star
28

docker-s3rver

Dockerfile
2
star
29

ansible-role-consul

An Ansible role that deploys a consul cluster as containers.
Python
2
star
30

ansible-role-openresty

A role to deploy Openresty in a Docker container with useful plugins and libraries
Python
2
star
31

pgcli-docker

Pgcli dockerized
2
star
32

cookiecutter-django-app

Python
2
star
33

borgmatic-binary

Python
2
star
34

ansible-role-jobber

An Ansible role that installs Jobber on debian based architecture. https://dshearer.github.io/jobber
2
star
35

ansible-role-drone

Python
2
star
36

ansible-role-monitoring

Python
2
star
37

docker-vegeta

1
star
38

docker-gitlab-runner

Dockerfile
1
star
39

hn-comment-parser

Go
1
star
40

go-websocket-example

Go
1
star
41

dockerfiles

Dockerfile
1
star
42

action-ansible

Python
1
star
43

seo_friendly_urls_django

Example app with SEO optimized urls in Django
Python
1
star
44

DIT165

C
1
star
45

erl-chat-otp

Erlang
1
star
46

ansible-role-gitlab

Ruby
1
star
47

samson-docker

Dockerfile
1
star