diff options
| -rw-r--r-- | PKGBUILD | 8 | ||||
| -rw-r--r-- | src/python-sshmgr/setup.py | 16 | ||||
| -rw-r--r-- | src/python-sshmgr/sshmgr/__init__.py | 0 | ||||
| -rwxr-xr-x | src/python-sshmgr/sshmgr/manager.py | 100 | ||||
| -rw-r--r-- | src/python-sshmgr/sshmgr/utils/__init__.py | 0 | ||||
| -rw-r--r-- | src/python-sshmgr/sshmgr/utils/getch.py | 40 |
6 files changed, 160 insertions, 4 deletions
@@ -6,16 +6,16 @@ # See http://wiki.archlinux.org/index.php/Python_Package_Guidelines for more # information on Python packaging. -# Maintainer: Your Name <jan-sebastian.tuomi@aalto.fi> +# Maintainer: Jan Tuomi <jan-sebastian.tuomi@aalto.fi> pkgname=python-sshmgr pkgver=0.1 -pkgrel=1 +pkgrel=2 pkgdesc="Minimal ssh connection manager" arch=('x86_64' 'i386') url="" license=('GPL') groups=() -depends=('python' 'screen') +depends=('python3' 'screen' 'openssh') makedepends=() provides=() conflicts=() @@ -27,7 +27,7 @@ source=() md5sums=() package() { - cd "$srcdir/$pkgname-$pkgver" + cd "$srcdir/$pkgname" python setup.py install --root="$pkgdir/" --optimize=1 } diff --git a/src/python-sshmgr/setup.py b/src/python-sshmgr/setup.py new file mode 100644 index 0000000..2889e9b --- /dev/null +++ b/src/python-sshmgr/setup.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +from setuptools import setup + +setup(name='sshmgr', + version='0.1', + description='', + author='Jan Tuomi', + author_email='jan-sebastian.tuomi@aalto.fi', + url='', + packages=['sshmgr', 'sshmgr.utils'], + entry_points= {'console_scripts': [ + 'sshmgr = sshmgr.manager:start', + ], + }, +) diff --git a/src/python-sshmgr/sshmgr/__init__.py b/src/python-sshmgr/sshmgr/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/python-sshmgr/sshmgr/__init__.py diff --git a/src/python-sshmgr/sshmgr/manager.py b/src/python-sshmgr/sshmgr/manager.py new file mode 100755 index 0000000..a55aa07 --- /dev/null +++ b/src/python-sshmgr/sshmgr/manager.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +from os import system +from sshmgr.utils.getch import getch +import os.path + +CONN_DB_PATH = os.path.join(os.path.expanduser("~"), ".sshmgr-db") + +class Manager: + def __init__(self): + pass + + def load_conns(self): + conns = [] + try: + with open(CONN_DB_PATH) as f: + conns = [l.strip() for l in f.readlines()] + except FileNotFoundError: + with open(CONN_DB_PATH, 'w') as f: + conns = [] + except: + print("Could not load connections from file. ({:s})".format(CONN_DB_PATH)) + getch() + + return conns + + def create_new(self, conns): + name = input("Name the new connection: ") + conns.append(name) + + self.save_conns(conns) + + def save_conns(self, conns): + with open(CONN_DB_PATH, 'w') as f: + for conn in conns: + f.write("{:s}\n".format(conn)) + + def get_connection(self, conns, c): + try: + num = int(c) + except ValueError: + print("Selection needs to be a number!") + return False + + if num < 1 or num > len(conns): + print("Selection not in range!") + return False + + selection = conns[num - 1] + return selection + + def connect(self, connection): + system("screen ssh {}".format(connection)) + + def delete_conn(self, conns): + c = input("Which connection should be removed? ") + deleted = self.get_connection(conns, c) + if not deleted: + print("Nothing was deleted.") + getch() + return + + conns = [conn for conn in conns if conn != deleted] + self.save_conns(conns) + + def run(self): + running = True + while running: + conns = self.load_conns() + system("clear") + + print("Select connection: ") + for i,conn in enumerate(conns): + print("({:d}) {:s}".format(i + 1, conn)) + print("(n) New connection") + print("(d) Delete connection") + print("(q) Quit") + + c = getch() + + if (c == "q"): + return + elif (c == "d"): + self.delete_conn(conns) + elif (c == "n"): + self.create_new(conns) + else: + connection = self.get_connection(conns, c) + if not connection: + print("Select a valid connection.") + getch() + else: + self.connect(connection) + return + +def start(): + s = Manager() + s.run() + +if __name__ == "__main__": + start() diff --git a/src/python-sshmgr/sshmgr/utils/__init__.py b/src/python-sshmgr/sshmgr/utils/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/python-sshmgr/sshmgr/utils/__init__.py diff --git a/src/python-sshmgr/sshmgr/utils/getch.py b/src/python-sshmgr/sshmgr/utils/getch.py new file mode 100644 index 0000000..3ee4b25 --- /dev/null +++ b/src/python-sshmgr/sshmgr/utils/getch.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +class _Getch: + """Gets a single character from standard input. Does not echo to the +screen.""" + def __init__(self): + try: + self.impl = _GetchWindows() + except ImportError: + self.impl = _GetchUnix() + + def __call__(self): return self.impl() + + +class _GetchUnix: + def __init__(self): + import tty, sys + + def __call__(self): + import sys, tty, termios + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(sys.stdin.fileno()) + ch = sys.stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + return ch + + +class _GetchWindows: + def __init__(self): + import msvcrt + + def __call__(self): + import msvcrt + return msvcrt.getch() + + +getch = _Getch() |
