From c81ed00cf5c957c395a94237c2b0487c7a3bcd91 Mon Sep 17 00:00:00 2001 From: Sergei Eremenko Date: Mon, 8 Aug 2016 17:39:44 +0300 Subject: [PATCH] Rewrite to Python --- debian/control | 8 +++--- unetlab-x-integration | 65 ++++++++++++++++++++++++++++--------------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/debian/control b/debian/control index a4d5e41..e9555ec 100644 --- a/debian/control +++ b/debian/control @@ -1,12 +1,12 @@ Package: unetlab-x-integration Priority: extra Section: net -Installed-Size: 755 +Installed-Size: 1396 Maintainer: Sergei Eremenko Architecture: all -Version: 0.1.2 -Depends: telnet, wireshark, ssh-askpass-gnome | ssh-askpass, xterm | x-terminal-emulator -Recommends: vinagre, docker-engine | docker.io +Version: 0.2.0 +Depends: python, ssh-askpass-gnome | ssh-askpass, telnet, vinagre, wireshark, xterm | x-terminal-emulator +Recommends: docker-engine | docker.io Homepage: http://smartfinn.github.io/unetlab-x-integration/ Description: a tool which helps UNetLab to integrate with Ubuntu/Debian This package provides the following protocols: diff --git a/unetlab-x-integration b/unetlab-x-integration index d3c16c5..fa05200 100755 --- a/unetlab-x-integration +++ b/unetlab-x-integration @@ -1,23 +1,42 @@ -#!/bin/bash - -URI=(`echo $1 | \ - sed -r 's%^(.*)://([a-Z0-9\\-\\.]+):?([0-9]+)?/?(.*)$%x\1 x\2 x\3 x\4%'`) - -URI_SCHEME=${URI[0]#x} -URI_HOST=${URI[1]#x} -URI_PORT=${URI[2]#x} -URI_PATH=${URI[3]#x} - -case "$URI_SCHEME" in - telnet) - x-terminal-emulator -e "telnet $URI_HOST $URI_PORT" - ;; - capture) - ssh -l root $URI_HOST tcpdump -s 0 -U -n -i $URI_PATH -w - | \ - wireshark -k -i - - ;; - docker) - x-terminal-emulator -e "docker -H $URI_HOST:$URI_PORT attach \ - ${URI_PATH%%\?*}" - ;; -esac +#!/usr/bin/env python +# See https://github.com/SmartFinn/unetlab-x-integration/ + +from __future__ import print_function +from subprocess import Popen + +import sys + + +def main(argv): + try: + from urllib.parse import urlparse + except ImportError: + from urlparse import urlparse + + u = urlparse(argv) + + d = { + 'host': u.hostname, + 'port': u.port, + 'path': u.path.lstrip('/'), + } + + if u.scheme == 'capture': + Popen('ssh -l root {host} tcpdump -s 0 -U -n -i {path} -w - | \ + wireshark -k -i -'.format(**d), shell=True) + elif u.scheme == 'docker': + Popen('x-terminal-emulator -e \ + "docker -H {host}:{port} attach {path}"'.format(**d), shell=True) + elif u.scheme == 'telnet': + Popen('x-terminal-emulator -e "telnet {host} {port}"'.format(**d), + shell=True) + else: + return 'URL "{}" is currently not supported.'.format(u.geturl()) + + +if __name__ == '__main__': + try: + sys.exit(main(sys.argv[1])) + except IndexError: + print('usage:', __file__, '', file=sys.stderr) + sys.exit(1)