Code Monkey home page Code Monkey logo

local-ssh-config's Introduction

local-ssh-config

Imports: isort Code style: black pre-commit ci

Ian Cleary (iancleary)

Description

Welcome! This is a CLI to generate/update SSH config files for your local virtual machines quickly.

Problem

Windows doesn't maintain a static IP Address of Hyper-V Virtual Machines across reboots. When using multipass with a Hyper-V backend, this applies as well for multipass.

This leads to ssh configuration, which is by ip address, to be stale every reboot.

Solution

This script updates my ssh config file for me

  • get IP address from PowerShell for hyper-v by name according to source ('hyper-v' directly, or 'multipass list')
  • write template config files to the ~/.ssh/config.d/ directory according to your ~/.config/local-ssh-config/settings.json file.

This assumes you have:

Hyper-V Manager IP Address

Hyper-V Manager Networking Tab

I currently use Ubuntu Servers, if you do too, please install several apt packages in the Virtual Machine (so that Hyper-V can report the IP Address)

🚨🚨 Hyper-V will not report the ip address until you do the above 🚨🚨

Multipass or Virtual Box may report the IP address of an Ubuntu Guest. I'm not currently sure if it's a Hyper-V limitation or a Windows limitation.

As this tool only currently supports Hyper-V, please consider this a warning of the required step.

Quickstart

❯ pipx install local-ssh-config --user
❯ local-ssh-config --help

That will output the following:

Usage: local_ssh_config [OPTIONS]

  Creates an `~/.ssh/config.d/` directory, 
  checks to see if you include all files in that directory,
  and then creates config files for each virtual machine specified
  in your `~/.config/vm-ip-ssh-config/settings.json` file.

  See https://github.com/iancleary/local-ssh-config/ for more information.

Arguments:
  None

Options:
  -f, --file TEXT       The JSON file containing the virtual machine
                        configuration  [default: C:\Users\username\.config\vm-
                        ip-ssh-config\settings.json]
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it or
                        customize the installation.
  --help                Show this message and exit.

Example Usage

The first and only argument is the name of the component to create.

$ local-ssh-config
{'host': 'test.local', 'hostname': '0.0.0.0', 'user': 'test', 'identity_file': '~/.ssh/example_id_ed25519'}
{'host': 'ubuntu.local', 'hostname': {'source': 'hyper-v', 'physical_address': '00-15-5d-95-fb-09'}, 'user': 'icleary', 'identity_file': '~/.ssh/github_id_rsa_ed25519'}
Hyper-V: Powershell (arp -a): Interface command executed successfully!
-------------------------
{'host': 'dev1.multipass.local', 'hostname': {'source': 'multipass', 'name': 'dev1'}, 'user': 'ubuntu'}
Multipass-V: Powershell (multipass list): Interface command executed successfully!
-------------------------

✨ Creating ~/.ssh/config.d/ files
✅ C:\Users\icleary\.ssh\config.d\test.local
✅ C:\Users\icleary\.ssh\config.d\ubuntu.local
✅ C:\Users\icleary\.ssh\config.d\dev1.multipass.local
SSH config updated! 🚀 ✨!

Thank you for using local-ssh-config.

The path printed is the absolute path to the updated config files.

This uses a directory ~/.ssh/config.d/ to allow for a single file per Host, to allow cleaner version tracking within a dotfile manager. See Include config.d/* in your ~/.ssh/config for the include syntax WINDOWS_MULTIPASS_DEFAULT_ID_RSA = "%PROGRAMDATA%/Multipass/data/ssh-keys/id_rsa"

Configuration

Configuration can be done through 2 different ways:

  • Creating a global settings.json in your home directory (~/.config/local-ssh-config/settings.json).
  • Creating a local .local-ssh-config-config.json in your project's root directory and including the path to that file with the --f or -f optionanl command-line argument.

The optional command line value takes precendence global settings file being the default.

API Reference

File

Controls the settings.json to load. Defaults to ~/.config/local-ssh-config/settings.json

Usage:

Command line: --file <value> or -f <value>

JSON config:

Example with single host, as dictionary

{
    "host": "ubuntu.local",
    "hostname": {
        "source": "hyper-v",
        "physical_address": "00-15-5d-95-fb-09"
    },
    "user": "icleary",
    "identity_file": "~/.ssh/github_id_rsa_ed25519"
}

Example with single host, as list:

[
  {
      "host": "ubuntu.local",
      "hostname": {
          "source": "hyper-v",
          "physical_address": "00-15-5d-95-fb-09"
      },
      "user": "icleary",
      "identity_file": "~/.ssh/github_id_rsa_ed25519"
  }
]

A single dictionary is converted to a list of hosts before looping through the files, so either structure is valid (your preference).

Example with multiple hosts:

[
    {
        "host": "test.local",
        "hostname": "0.0.0.0",
        "user": "test",
        "identity_file": "~/.ssh/example_id_ed25519"
    },
    {
        "host": "ubuntu.local",
        "hostname": {
            "source": "hyper-v",
            "physical_address": "00-15-5d-95-fb-09"
        },
        "user": "icleary",
        "identity_file": "~/.ssh/github_id_rsa_ed25519"
    },
    {
        "host": "dev1.multipass.local",
        "hostname": {
            "source": "multipass",
            "name": "dev1"
        },
        "user": "ubuntu"
    }
]

Further information

I will likely evolve this CLI as I learn more; I'm on my way 😊

Enjoy quickly updating your ssh configurations 🚀!

Contributing

I created this CLI for my opinionated uses and may not accept changes. That said, I made this to solve a problem, and if you have the same problem, I hope it helps you! 😊

See CONTRIBUTING.md.

local-ssh-config's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

local-ssh-config's Issues

Hyper-V Util should use the last IP address for each physical address

Description

  • System info
    • Operating system and version: Windows 11
  • Steps to reproduce:
    • Open Powershell
    • Run arp -a
PS C:\Users\iancleary> arp -a

Interface: 10.0.0.189 --- 0x11
  Internet Address      Physical Address      Type
  10.0.0.1              70-4f-b8-a3-fd-7f     dynamic
  10.0.0.80             78-28-ca-ea-b4-d6     dynamic
  10.0.0.186            48-a6-b8-36-4e-d2     dynamic
  10.0.0.255            ff-ff-ff-ff-ff-ff     static
  224.0.0.22            01-00-5e-00-00-16     static
  224.0.0.251           01-00-5e-00-00-fb     static
  224.0.0.252           01-00-5e-00-00-fc     static
  239.255.255.250       01-00-5e-7f-ff-fa     static
  255.255.255.255       ff-ff-ff-ff-ff-ff     static

Interface: 172.27.80.1 --- 0x16
  Internet Address      Physical Address      Type
  172.27.82.223         00-15-5d-01-37-00     static
  172.27.94.195         00-15-5d-01-37-00     static
  172.27.95.255         ff-ff-ff-ff-ff-ff     static
  224.0.0.2             01-00-5e-00-00-02     static
  224.0.0.22            01-00-5e-00-00-16     static
  224.0.0.251           01-00-5e-00-00-fb     static
  239.255.255.250       01-00-5e-7f-ff-fa     static
  255.255.255.255       ff-ff-ff-ff-ff-ff     static

Interface: 100.100.210.19 --- 0x2b
  Internet Address      Physical Address      Type
  224.0.0.22                                  static
  224.0.0.251                                 static
  239.255.255.250                             static

Suggestions

Application should get the last row matching the physical address...theory, but the later row matches the IP reported in Hyper-V manager.

Remove break from loop that detects row containing physical address.
https://github.com/iancleary/vm-ssh-config/blob/91badaa8d977138807325b131a7c8f36939c1cf9/vmsshconfig/utils/hyper_v.py#L38

Related

Relates to organization/repo#number

Error if settings file doesn't exist

Description

  • System info
    • Operating system and version: Windows 11
  • Steps to reproduce:
    • pipx install local-ssh-config==0.4.0 --force
    • Next step
iancleary@WINDOWS-TOWER MINGW64 ~
$ pipx install local-ssh-config==0.4.0 --force
Installing to existing venv 'local-ssh-config'
⚠️  Overwriting file C:\Users\iancleary\.local\bin\local-ssh-config.exe with C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\Scripts\local-ssh-config.exe
⚠️  Overwriting file C:\Users\iancleary\.local\bin\local_ssh_config.exe with C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\Scripts\local_ssh_config.exe
⚠️  Overwriting file C:\Users\iancleary\.local\bin\lsc.exe with C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\Scripts\lsc.exe
  installed package local-ssh-config 0.4.0, installed using Python 3.10.5
  These apps are now globally available
    - local-ssh-config.exe
    - local_ssh_config.exe
    - lsc.exe
done! ✨ 🌟 ✨

iancleary@WINDOWS-TOWER MINGW64 ~
$ lsc
✨ Starting local-ssh-config ✨

Traceback (most recent call last):

  File "C:\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,

  File "C:\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)

  File "C:\users\iancleary\.local\bin\lsc.exe\__main__.py", line 7, in <module>
    sys.exit(main())

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\local_ssh_config\__main__.py", line 24, in main
    cli.app(prog_name=__app_name__)

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\typer\main.py", line 289, in __call__

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\typer\main.py", line 280, in __call__

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\click\core.py", line 1130, in __call__

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\click\core.py", line 1055, in main

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\click\core.py", line 1404, in invoke

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\click\core.py", line 760, in invoke

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\typer\main.py", line 607, in wrapper

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\local_ssh_config\cli.py", line 49, in main
    (NEW_CONFIG_FILE_CREATED, config) = _load_config(config_file=Path(file))

  File "C:\Users\iancleary\.local\pipx\venvs\local-ssh-config\lib\site-packages\local_ssh_config\_config.py", line 21, in _load_config
    with open(config_file, "w") as f:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\iancleary\\.config\\local-ssh-config\\settings.json'

Suggestions

Related

Relates to #1

Default id_rsa path on Windows 11 may have changed

Description

"/c/ProgramData/Multipass/data/ssh-keys/id_rsa" vs "/c/Windows/ProgramData/Multipass/data/ssh-keys/id_rsa"

  • System info
    • Operating system and version: Windows 11
  • Steps to reproduce:
    • Step
    • Next step

Suggestions

Windows 11 config file

{
	"host": "dev.multipass.local",
        "hostname": {
            "source": "multipass",
            "name": "dev"
        },
        "user": "ubuntu",
        "identity_file": "/c/ProgramData/Multipass/data/ssh-keys/id_rsa"
}

Windows 10 config file

{
	"host": "dev.multipass.local",
        "hostname": {
            "source": "multipass",
            "name": "dev"
        },
        "user": "ubuntu",
        "identity_file": "/c/Windows/ProgramData/Multipass/data/ssh-keys/id_rsa"
}

WINDOWS_MULTIPASS_DEFAULT_ID_RSA = (
"C:/Windows/ProgramData/Multipass/data/ssh-keys/id_rsa"
)

Related

Relates to organization/repo#number

Create ssh config file without .j2 extension

Description

_create_file_from_template(directory=SSH_DIR, template_name="config.j2")
creates a ~/.ssh/config.j2 file instead of a ~/.ssh/config file,

  • System info
    • Operating system and version: Windows 11
  • Steps to reproduce:
    • backup and move your ~/.ssh/config file so that no file by that path and name exists
    • run lsc

Suggestions

_create_file_from_template(directory=SSH_DIR, template_name="config.j2")
should create a ~/.ssh/config file.

Related

Relates to organization/repo#number

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.