This commit is contained in:
fluzzi 2022-03-26 16:30:37 -03:00
parent 555b285d36
commit 2042178cbe
4 changed files with 93 additions and 7 deletions

View File

@ -1,2 +1,84 @@
# connpy # Conn
Network connection and automation tool
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
```

View File

@ -3,7 +3,6 @@
from .core import node from .core import node
from .configfile import configfile from .configfile import configfile
from .connapp import connapp from .connapp import connapp
import __main__
__version__ = "2.0" __version__ = "2.0"
__all__ = [node, configfile, connapp] __all__ = [node, configfile, connapp]

View File

@ -25,7 +25,7 @@ class connapp:
defaultparser = argparse.ArgumentParser(prog = "conn", description = "SSH and Telnet connection manager", formatter_class=argparse.RawTextHelpFormatter) defaultparser = argparse.ArgumentParser(prog = "conn", description = "SSH and Telnet connection manager", formatter_class=argparse.RawTextHelpFormatter)
subparsers = defaultparser.add_subparsers(title="Commands") subparsers = defaultparser.add_subparsers(title="Commands")
#NODEPARSER #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() 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")) 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") 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.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) configparser.set_defaults(func=self._func_others)
#Set default subparser and tune arguments #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"] profilecmds = ["--add", "--del", "--rm", "--mod", "--edit", "--show"]
if len(sys.argv) >= 3 and sys.argv[2] == "profile" and sys.argv[1] in profilecmds: if len(sys.argv) >= 3 and sys.argv[2] == "profile" and sys.argv[1] in profilecmds:
sys.argv[2] = sys.argv[1] sys.argv[2] = sys.argv[1]
@ -623,6 +623,10 @@ class connapp:
def _help(self, type): def _help(self, type):
if type == "node": 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" 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): def _getallnodes(self):
nodes = [] nodes = []

View File

@ -18,7 +18,7 @@ conf = conn.configfile()
# print(yaml.dump(conf.profiles)) # print(yaml.dump(conf.profiles))
# conf.saveconfig("test.yaml") # 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) # xr=conn.node("xr@home", **conf.getitem("xr@home"), config=conf)
# ios=conn.node("ios@home", **conf.connections["home"]["ios"], config=conf) # ios=conn.node("ios@home", **conf.connections["home"]["ios"], config=conf)
# norman = conn.node("norman@home", **conf.connections["home"]["norman"], config=conf) # norman = conn.node("norman@home", **conf.connections["home"]["norman"], config=conf)
@ -27,7 +27,8 @@ conf = conn.configfile()
# router228.interact() # router228.interact()
# router228.run(["term len 0","show ip int br"]) # router228.run(["term len 0","show ip int br"])
# xroutput = xr.run("show run") # 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"]) # norman.run(["ls -la", "pwd"])
# test = eve.run(["ls -la", "pwd"]) # test = eve.run(["ls -la", "pwd"])
# print(norman.output) # print(norman.output)