Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
Made this script suitable for PyPI
Browse files Browse the repository at this point in the history
  • Loading branch information
dmotte committed Jun 19, 2022
1 parent 8b140f0 commit 8cce584
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 21 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
name: release

# This workflow requires a PYPI_API_TOKEN secret to be defined in the GitHub
# repository settings.

on:
release:
types: [created]

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set package version in setup.cfg
run: sed -i "s/^version = 0$/version = ${GITHUB_REF#refs/tags/v}/" setup.cfg

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.x

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python -m build
python -m twine upload dist/*
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/sshkp/__pycache__/

/build/
/dist/

/*.egg-info/
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# sshkp

[![GitHub release workflow](https://img.shields.io/github/workflow/status/dmotte/sshkp/release?logo=github&label=release&style=flat-square)](https://github.com/dmotte/sshkp/actions)
[![PyPI](https://img.shields.io/pypi/v/sshkp?logo=python&style=flat-square)](https://pypi.org/project/sshkp/)

:snake: Script to execute an **SSH command** using a password from a **KeePass database**.

This project uses the [pykeepass](https://pypi.org/project/pykeepass/) library and the `sshpass` command line utility.

Note that, for this to work, the title of your KeePass database entry should be written in a form that can be used with the `ssh` command, e.g. `user@hostname`.

## Installation

This utility is available as a Python package on **PyPI**:

```bash
pip3 install sshkp
```

## Usage

```bash
Expand All @@ -17,25 +28,20 @@ sshkp user@hostname .print # just prints the SSH password

If you don't set the `KP_PASSWORD` environment variable before calling the script, the password will be asked at runtime.

See `sshkp --help` for more information.

It is advised to install the script in a directory under your `$PATH` (see [below](#installation)).
:information_source: For more details on how to use this command, you can also refer to the help message (`sshkp --help`).

## Installation
## Development

To install _sshkp_ you need to execute the following commands as root:
If you want to contribute to this project, the first thing you have to do is to **clone this repository** on your local machine:

```bash
apt update && apt install sshpass python3-pip
pip3 install -r requirements.txt

curl -Lo "/usr/local/bin/sshkp" \
https://github.com/dmotte/sshkp/releases/latest/download/sshkp
chmod +x "/usr/local/bin/sshkp"
git clone https://github.com/dmotte/sshkp.git
```

:information_source: For **user installation** (no root needed, will only work for current user) we recommend `~/.local/bin` instead of `/usr/local/bin`. If it's not in your `$PATH`, you can add the following to your `.bashrc` or `.zshrc`:
Then you can install the package in **editable** mode:

```bash
export PATH="~/.local/bin:$PATH"
pip3 install -e . --user
```

This will just link the package to the original location, basically meaning any changes to the original package would reflect directly in your environment ([source](https://stackoverflow.com/a/35064498)).
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

42 changes: 42 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[metadata]
name = sshkp
url = https://github.com/dmotte/sshkp
author = dmotte
license = MIT

description = Script to execute an SSH command using a password from a KeePass database
long_description =
[![GitHub release workflow](https://img.shields.io/github/workflow/status/dmotte/sshkp/release?logo=github&label=release&style=flat-square)](https://github.com/dmotte/sshkp/actions)
[![PyPI](https://img.shields.io/pypi/v/sshkp?logo=python&style=flat-square)](https://pypi.org/project/sshkp/)

Script to execute an **SSH command** using a password from a **KeePass database**.

Please refer to the [GitHub repository](https://github.com/dmotte/sshkp) for more information.
long_description_content_type = text/markdown

keywords = ssh database script password environment-variables keepass sshpass pykeepass sshkp kdbx command cli
classifiers =
Intended Audience :: End Users/Desktop
Environment :: Console
Natural Language :: English
License :: OSI Approved :: MIT License
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.9
Topic :: Terminals
Topic :: Utilities

# This version number will be replaced with the current git tag by the
# release.yml workflow
version = 0

[options]
install_requires =
pykeepass >= 4.0.1
python_requires = >=3.9.1
packages = sshkp

[options.entry_points]
console_scripts =
sshkp = sshkp.cli:main
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python3

from setuptools import setup

if __name__ == '__main__':
setup()
5 changes: 5 additions & 0 deletions sshkp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env python3

# To make all the functions defined in the cli module accessible by importing
# the root module
from .cli import *
8 changes: 8 additions & 0 deletions sshkp/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python3

import sys

from .cli import main

if __name__ == '__main__':
sys.exit(main())
14 changes: 7 additions & 7 deletions sshkp → sshkp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import argparse
import getpass
import os
import sys

from pykeepass import PyKeePass


def main():
def main(argv=None):
if argv is None:
argv = sys.argv

parser = argparse.ArgumentParser(description='''
Executes an SSH command with a password from a KeePass database.
This script supports two environment variables: KP_FILENAME (mandatory)
Expand All @@ -21,7 +25,7 @@ def main():
then it just prints the password without executing
anything else''')

args = vars(parser.parse_args())
args = vars(parser.parse_args(argv[1:]))

############################################################################

Expand Down Expand Up @@ -49,15 +53,11 @@ def main():

if len(command) >= 1 and command[0] == '.print':
print(entry.password)
return
return 0

os.environ['SSHPASS'] = entry.password

os.execv(
'/usr/bin/sshpass',
['sshpass', '-e', 'ssh', entryname] + command,
)


if __name__ == '__main__':
main()

0 comments on commit 8cce584

Please sign in to comment.