From 2042178cbe8ce0919891cdac7c4d469b1a15ed94 Mon Sep 17 00:00:00 2001 From: fluzzi Date: Sat, 26 Mar 2022 16:30:37 -0300 Subject: [PATCH] update --- README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++++-- conn/__init__.py | 1 - conn/connapp.py | 8 +++-- test.py | 5 +-- 4 files changed, 93 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 96bec90..92a7c7f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,84 @@ -# connpy -Network connection and automation tool +# Conn + +Conn is a ssh and telnet connection manager and automation module + +## Installation + +pip install conn +## Automation module usage +### Standalone module +``` +import conn +router = conn.node("unique name","ip/hostname", user="username", password="password") +router.run("show run") +print(router.output) +``` + +### Using manager configuration +``` +import conn +conf = conn.configfile() +device = conf.getitem("router@office") +router = conn.node("unique name", **device, config=conf) +result = router.run("show ip int brief") +print(result) +router.interact() +``` +## Connection manager usage +``` +usage: conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] + conn {profile,move,mv,copy,cp,list,ls,bulk,config} ... + +positional arguments: + node|folder node[@subfolder][@folder] + Connect to specific node or show all matching nodes + [@subfolder][@folder] + Show all available connections globaly or in specified path +``` + +#### Options: +``` + -h, --help show this help message and exit + --add Add new node[@subfolder][@folder] or [@subfolder]@folder + --del, --rm Delete node[@subfolder][@folder] or [@subfolder]@folder + --mod, --edit Modify node[@subfolder][@folder] + --show Show node[@subfolder][@folder] + --debug, -d Display all conections steps +``` + +#### Commands: +``` + profile Manage profiles + move (mv) Move node + copy (cp) Copy node + list (ls) List profiles, nodes or folders + bulk Add nodes in bulk + config Manage app config +``` + +#### Manage profiles: +``` +usage: conn profile [-h] (--add | --del | --mod | --show) profile + +positional arguments: + profile Name of profile to manage + +options: + -h, --help show this help message and exit + --add Add new profile + --del, --rm Delete profile + --mod, --edit Modify profile + --show Show profile +``` + +#### Examples: +``` + conn profile --add office-user + conn --add @office + conn --add @datacenter@office + conn --add server@datacenter@office + conn --add pc@office + conn --show server@datacenter@office + conn pc@office + conn server +``` diff --git a/conn/__init__.py b/conn/__init__.py index f057f77..f6948a4 100644 --- a/conn/__init__.py +++ b/conn/__init__.py @@ -3,7 +3,6 @@ from .core import node from .configfile import configfile from .connapp import connapp -import __main__ __version__ = "2.0" __all__ = [node, configfile, connapp] diff --git a/conn/connapp.py b/conn/connapp.py index 4ef705e..423b862 100755 --- a/conn/connapp.py +++ b/conn/connapp.py @@ -25,7 +25,7 @@ class connapp: defaultparser = argparse.ArgumentParser(prog = "conn", description = "SSH and Telnet connection manager", formatter_class=argparse.RawTextHelpFormatter) subparsers = defaultparser.add_subparsers(title="Commands") #NODEPARSER - nodeparser = subparsers.add_parser("node", help=self._help("node"),formatter_class=argparse.RawTextHelpFormatter) + nodeparser = subparsers.add_parser("node",usage=self._help("usage"), help=self._help("node"),epilog=self._help("end"), formatter_class=argparse.RawTextHelpFormatter) nodecrud = nodeparser.add_mutually_exclusive_group() nodeparser.add_argument("node", metavar="node|folder", nargs='?', default=None, action=self.store_type, type=self._type_node, help=self._help("node")) nodecrud.add_argument("--add", dest="action", action="store_const", help="Add new node[@subfolder][@folder] or [@subfolder]@folder", const="add", default="connect") @@ -65,7 +65,7 @@ class connapp: configparser.add_argument("--keepalive", dest="idletime", nargs=1, action=self.store_type, help="Set keepalive time in seconds, 0 to disable", type=int, metavar="INT") configparser.set_defaults(func=self._func_others) #Set default subparser and tune arguments - commands = ["node", "-h", "--help", "profile", "mv", "move","copy", "cp", "bulk", "ls", "list", "config"] + commands = ["node", "profile", "mv", "move","copy", "cp", "bulk", "ls", "list", "config"] profilecmds = ["--add", "--del", "--rm", "--mod", "--edit", "--show"] if len(sys.argv) >= 3 and sys.argv[2] == "profile" and sys.argv[1] in profilecmds: sys.argv[2] = sys.argv[1] @@ -623,6 +623,10 @@ class connapp: def _help(self, type): if type == "node": return "node[@subfolder][@folder]\nConnect to specific node or show all matching nodes\n[@subfolder][@folder]\nShow all available connections globaly or in specified path" + if type == "usage": + return "conn [-h] [--add | --del | --mod | --show | --debug] [node|folder]\n conn {profile,move,mv,copy,cp,list,ls,bulk,config} ..." + if type == "end": + return "Commands:\n profile Manage profiles\n move (mv) Move node\n copy (cp) Copy node\n list (ls) List profiles, nodes or folders\n bulk Add nodes in bulk\n config Manage app config" def _getallnodes(self): nodes = [] diff --git a/test.py b/test.py index a411386..0ec9e0d 100755 --- a/test.py +++ b/test.py @@ -18,7 +18,7 @@ conf = conn.configfile() # print(yaml.dump(conf.profiles)) # conf.saveconfig("test.yaml") # *** -# test = conn.node("test", "10.21.96.45") +# test = conn.node("test", "10.21.96.45", user="fluzzi32", password="32A2dsqe!DB01") # xr=conn.node("xr@home", **conf.getitem("xr@home"), config=conf) # ios=conn.node("ios@home", **conf.connections["home"]["ios"], config=conf) # norman = conn.node("norman@home", **conf.connections["home"]["norman"], config=conf) @@ -27,7 +27,8 @@ conf = conn.configfile() # router228.interact() # router228.run(["term len 0","show ip int br"]) # xroutput = xr.run("show run") -# ios.run("show run", folder=".",stdout=True) +# ios.run("show run",stdout=True) +# test.run("show run",stdout=True) # norman.run(["ls -la", "pwd"]) # test = eve.run(["ls -la", "pwd"]) # print(norman.output)