2022-04-03 09:38:00 -03:00
<!doctype html>
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1, minimum-scale=1" / >
< meta name = "generator" content = "pdoc 0.10.0" / >
2022-04-04 11:40:51 -03:00
< title > connpy API documentation< / title >
2022-04-03 18:25:58 -03:00
< meta name = "description" content = "Connection manager …" / >
2022-04-03 09:38:00 -03:00
< link rel = "preload stylesheet" as = "style" href = "https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity = "sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin >
< link rel = "preload stylesheet" as = "style" href = "https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity = "sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin >
2023-01-05 16:39:22 -03:00
< link rel = "stylesheet preload" as = "style" href = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin >
2022-04-03 09:38:00 -03:00
< style > : root { --highlight-color : #fe9 } . flex { display : flex !important } body { line-height : 1.5 em } # content { padding : 20 px } # sidebar { padding : 30 px ; overflow : hidden } # sidebar > * : last-child { margin-bottom : 2 cm } . http-server-breadcrumbs { font-size : 130 % ; margin : 0 0 15 px 0 } # footer { font-size : .75 em ; padding : 5 px 30 px ; border-top : 1 px solid #ddd ; text-align : right } # footer p { margin : 0 0 0 1 em ; display : inline-block } # footer p : last-child { margin-right : 30 px } h1 , h2 , h3 , h4 , h5 { font-weight : 300 } h1 { font-size : 2.5 em ; line-height : 1.1 em } h2 { font-size : 1.75 em ; margin : 1 em 0 .50 em 0 } h3 { font-size : 1.4 em ; margin : 25 px 0 10 px 0 } h4 { margin : 0 ; font-size : 105 % } h1 : target , h2 : target , h3 : target , h4 : target , h5 : target , h6 : target { background : var ( - - highlight - color ) ; padding : .2 em 0 } a { color : #058 ; text-decoration : none ; transition : color .3 s ease-in-out } a : hover { color : #e82 } . title code { font-weight : bold } h2 [ id ^ = "header-" ] { margin-top : 2 em } . ident { color : #900 } pre code { background : #f8f8f8 ; font-size : .8 em ; line-height : 1.4 em } code { background : #f2f2f1 ; padding : 1 px 4 px ; overflow-wrap : break-word } h1 code { background : transparent } pre { background : #f8f8f8 ; border : 0 ; border-top : 1 px solid #ccc ; border-bottom : 1 px solid #ccc ; margin : 1 em 0 ; padding : 1 ex } # http-server-module-list { display : flex ; flex-flow : column } # http-server-module-list div { display : flex } # http-server-module-list dt { min-width : 10 % } # http-server-module-list p { margin-top : 0 } . toc ul , # index { list-style-type : none ; margin : 0 ; padding : 0 } # index code { background : transparent } # index h3 { border-bottom : 1 px solid #ddd } # index ul { padding : 0 } # index h4 { margin-top : .6 em ; font-weight : bold } @ media ( min-width : 200ex ) { # index . two-column { column-count : 2 } } @ media ( min-width : 300ex ) { # index . two-column { column-count : 3 } } dl { margin-bottom : 2 em } dl dl : last-child { margin-bottom : 4 em } dd { margin : 0 0 1 em 3 em } # header-classes + dl > dd { margin-bottom : 3 em } dd dd { margin-left : 2 em } dd p { margin : 10 px 0 } . name { background : #eee ; font-weight : bold ; font-size : .85 em ; padding : 5 px 10 px ; display : inline-block ; min-width : 40 % } . name : hover { background : #e0e0e0 } dt : target . name { background : var ( - - highlight - color ) } . name > span : first-child { white-space : nowrap } . name . class > span : nth-child ( 2 ) { margin-left : .4 em } . inherited { color : #999 ; border-left : 5 px solid #eee ; padding-left : 1 em } . inheritance em { font-style : normal ; font-weight : bold } . desc h2 { font-weight : 400 ; font-size : 1.25 em } . desc h3 { font-size : 1 em } . desc dt code { background : inherit } . source summary , . git-link-div { color : #666 ; text-align : right ; font-weight : 400 ; font-size : .8 em ; text-transform : uppercase } . source summary > * { white-space : nowrap ; cursor : pointer } . git-link { color : inherit ; margin-left : 1 em } . source pre { max-height : 500 px ; overflow : auto ; margin : 0 } . source pre code { font-size : 12 px ; overflow : visible } . hlist { list-style : none } . hlist li { display : inline } . hlist li : after { content : ',\2002' } . hlist li : last-child : after { content : none } . hlist . hlist { display : inline ; padding-left : 1 em } img { max-width : 100 % } td { padding : 0 .5 em } . admonition { padding : .1 em .5 em ; margin-bottom : 1 em } . admonition-title { font-weight : bold } . admonition . note , . admonition . info , . admonition . important { background : #aef } . admonition . todo , . admonition . versionadded , . admonition . tip , . admonition . hint { background : #dfd } . admonition . warning , . admonition . versionchanged , . admonition . deprecated { background : #fd4 } . admonition . error , . admonition . danger , . admonition . caution { background : lightpink } < / style >
< style media = "screen and (min-width: 700px)" > @ media screen and ( min-width : 700px ) { # sidebar { width : 30 % ; height : 100 vh ; overflow : auto ; position : sticky ; top : 0 } # content { width : 70 % ; max-width : 100 ch ; padding : 3 em 4 em ; border-left : 1 px solid #ddd } pre code { font-size : 1 em } . item . name { font-size : 1 em } main { display : flex ; flex-direction : row-reverse ; justify-content : flex-end } . toc ul ul , # index ul { padding-left : 1.5 em } . toc > ul > li { margin-top : .5 em } } < / style >
< style media = "print" > @ media print { # sidebar h1 { page-break-before : always } . source { display : none } } @ media print { * { background : transparent !important ; color : #000 !important ; box-shadow : none !important ; text-shadow : none !important } a [ href ] : after { content : " (" attr ( href ) ")" ; font-size : 90 % } a [ href ] [ title ] : after { content : none } abbr [ title ] : after { content : " (" attr ( title ) ")" } . ir a : after , a [ href ^ = "javascript:" ] : after , a [ href ^ = "#" ] : after { content : "" } pre , blockquote { border : 1 px solid #999 ; page-break-inside : avoid } thead { display : table-header-group } tr , img { page-break-inside : avoid } img { max-width : 100 % !important } @ page { margin : 0 . 5cm } p , h2 , h3 { orphans : 3 ; widows : 3 } h1 , h2 , h3 , h4 , h5 , h6 { page-break-after : avoid } } < / style >
2022-04-03 10:26:08 -03:00
< script defer src = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity = "sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin > < / script >
< script > window . addEventListener ( 'DOMContentLoaded' , ( ) => hljs . initHighlighting ( ) ) < / script >
2022-04-03 09:38:00 -03:00
< / head >
< body >
< main >
< article id = "content" >
< header >
2022-04-04 11:40:51 -03:00
< h1 class = "title" > Package < code > connpy< / code > < / h1 >
2022-04-03 09:38:00 -03:00
< / header >
< section id = "section-intro" >
2022-04-03 12:00:35 -03:00
< h2 id = "connection-manager" > Connection manager< / h2 >
2022-04-04 11:40:51 -03:00
< p > Connpy is a connection manager that allows you to store nodes to connect them fast and password free.< / p >
2022-04-03 18:25:58 -03:00
< h3 id = "features" > Features< / h3 >
< pre > < code > - You can generate profiles and reference them from nodes using @profilename
so you dont need to edit multiple nodes when changing password or other
information.
- Nodes can be stored on @folder or @subfolder@folder to organize your
devices. Then can be referenced using node@subfolder@folder or node@folder
2022-04-18 19:19:25 -03:00
- If you have too many nodes. Get completion script using: conn config
--completion, or use fzf installing pyfzf and running conn config --fzf true
2022-04-03 18:25:58 -03:00
- Much more!
< / code > < / pre >
< h3 id = "usage" > Usage< / h3 >
2023-11-03 11:59:00 -03:00
< pre > < code > usage: conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]
2023-12-15 12:27:51 -03:00
conn {profile,move,mv,copy,cp,list,ls,bulk,export,import,ai,run,api,plugin,config} ...
2022-04-03 12:00:35 -03:00
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:
2022-04-04 14:41:51 -03:00
-h, --help show this help message and exit
2022-04-18 19:19:25 -03:00
-v, --version Show version
2022-04-04 14:41:51 -03:00
-a, --add Add new node[@subfolder][@folder] or [@subfolder]@folder
-r, --del, --rm Delete node[@subfolder][@folder] or [@subfolder]@folder
-e, --mod, --edit Modify node[@subfolder][@folder]
-s, --show Show node[@subfolder][@folder]
-d, --debug Display all conections steps
2023-11-03 11:59:00 -03:00
-t, --sftp Connects using sftp instead of ssh
2022-04-03 12:00:35 -03:00
Commands:
2023-12-15 12:27:51 -03:00
profile Manage profiles
move(mv) Move node
copy(cp) Copy node
list(ls) List profiles, nodes or folders
bulk Add nodes in bulk
export Export connection folder to Yaml file
import Import connection folder to config from Yaml file
ai Make request to an AI
run Run scripts or commands on nodes
api Start and stop connpy api
plugin Manage plugins
config Manage app config
2022-04-03 12:00:35 -03:00
< / code > < / pre >
2022-04-03 18:25:58 -03:00
< h3 id = "manage-profiles" > Manage profiles< / h3 >
2022-04-03 12:00:35 -03:00
< pre > < code > usage: conn profile [-h] (--add | --del | --mod | --show) profile
positional arguments:
profile Name of profile to manage
options:
2022-04-04 14:41:51 -03:00
-h, --help show this help message and exit
-a, --add Add new profile
-r, --del, --rm Delete profile
-e, --mod, --edit Modify profile
-s, --show Show profile
2022-04-03 12:00:35 -03:00
< / code > < / pre >
2022-04-03 18:25:58 -03:00
< h3 id = "examples" > Examples< / h3 >
2022-04-03 12:00:35 -03:00
< pre > < code > 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
< / code > < / pre >
2023-12-15 12:27:51 -03:00
< h2 id = "plugin-requirements-for-connpy" > Plugin Requirements for Connpy< / h2 >
< h3 id = "general-structure" > General Structure< / h3 >
< ul >
< li > The plugin script must be a Python file.< / li >
< li > Only the following top-level elements are allowed in the plugin script:< / li >
< li > Class definitions< / li >
< li > Function definitions< / li >
< li > Import statements< / li >
< li > The < code > if __name__ == "__main__":< / code > block for standalone execution< / li >
< li > Pass statements< / li >
< / ul >
< h3 id = "specific-class-requirements" > Specific Class Requirements< / h3 >
< ul >
< li > The plugin script must define at least two specific classes:< / li >
< li > < strong > Class < code > Parser< / code > < / strong > :< ul >
< li > Must contain only one method: < code > __init__< / code > .< / li >
< li > The < code > __init__< / code > method must initialize at least two attributes:< / li >
< li > < code > self.parser< / code > : An instance of < code > argparse.ArgumentParser< / code > .< / li >
< li > < code > self.description< / code > : A string containing the description of the parser.< / li >
< / ul >
< / li >
< li > < strong > Class < code > Entrypoint< / code > < / strong > :< ul >
< li > Must have an < code > __init__< / code > method that accepts exactly three parameters besides < code > self< / code > :< / li >
< li > < code > args< / code > : Arguments passed to the plugin.< / li >
< li > The parser instance (typically < code > self.parser< / code > from the < code > Parser< / code > class).< / li >
< li > The Connapp instance to interact with the Connpy app.< / li >
< / ul >
< / li >
< / ul >
< h3 id = "executable-block" > Executable Block< / h3 >
< ul >
< li > The plugin script can include an executable block:< / li >
< li > < code > if __name__ == "__main__":< / code > < / li >
< li > This block allows the plugin to be run as a standalone script for testing or independent use.< / li >
< / ul >
< h3 id = "script-verification" > Script Verification< / h3 >
< ul >
< li > The < code > verify_script< / code > method in < code > plugins.py< / code > is used to check the plugin script's compliance with these standards.< / li >
< li > Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system.< / li >
< li > < / li >
< / ul >
< h3 id = "example-script" > Example Script< / h3 >
< p > For a practical example of how to write a compatible plugin script, please refer to the following example:< / p >
< p > < a href = "https://github.com/fluzzi/awspy" > Example Plugin Script< / a > < / p >
< p > This script demonstrates the required structure and implementation details according to the plugin system's standards.< / p >
2023-04-14 11:44:56 -03:00
< h2 id = "http-api" > http API< / h2 >
< p > With the Connpy API you can run commands on devices using http requests< / p >
< h3 id = "1-list-nodes" > 1. List Nodes< / h3 >
< p > < strong > Endpoint< / strong > : < code > /list_nodes< / code > < / p >
< p > < strong > Method< / strong > : < code > POST< / code > < / p >
< p > < strong > Description< / strong > : This route returns a list of nodes. It can also filter the list based on a given keyword.< / p >
< h4 id = "request-body" > Request Body:< / h4 >
< pre > < code class = "language-json" > {
" filter" : " < keyword> "
}
< / code > < / pre >
< ul >
< li > < code > filter< / code > (optional): A keyword to filter the list of nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.< / li >
< / ul >
< h4 id = "response" > Response:< / h4 >
< ul >
< li > A JSON array containing the filtered list of nodes.< / li >
< / ul >
< hr >
2023-05-05 13:41:32 -03:00
< h3 id = "2-get-nodes" > 2. Get Nodes< / h3 >
< p > < strong > Endpoint< / strong > : < code > /get_nodes< / code > < / p >
< p > < strong > Method< / strong > : < code > POST< / code > < / p >
< p > < strong > Description< / strong > : This route returns a dictionary of nodes with all their attributes. It can also filter the nodes based on a given keyword.< / p >
< h4 id = "request-body_1" > Request Body:< / h4 >
< pre > < code class = "language-json" > {
" filter" : " < keyword> "
}
< / code > < / pre >
< ul >
< li > < code > filter< / code > (optional): A keyword to filter the nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.< / li >
< / ul >
< h4 id = "response_1" > Response:< / h4 >
< ul >
< li > A JSON array containing the filtered nodes.< / li >
< / ul >
< hr >
< h3 id = "3-run-commands" > 3. Run Commands< / h3 >
2023-04-14 11:44:56 -03:00
< p > < strong > Endpoint< / strong > : < code > /run_commands< / code > < / p >
< p > < strong > Method< / strong > : < code > POST< / code > < / p >
< p > < strong > Description< / strong > : This route runs commands on selected nodes based on the provided action, nodes, and commands. It also supports executing tests by providing expected results.< / p >
2023-05-05 13:41:32 -03:00
< h4 id = "request-body_2" > Request Body:< / h4 >
2023-04-14 11:44:56 -03:00
< pre > < code class = "language-json" > {
" action" : " < action> " ,
" nodes" : " < nodes> " ,
" commands" : " < commands> " ,
" expected" : " < expected> " ,
" options" : " < options> "
}
< / code > < / pre >
< ul >
< li > < code > action< / code > (required): The action to be performed. Possible values: < code > run< / code > or < code > test< / code > .< / li >
< li > < code > < a title = "connpy.nodes" href = "#connpy.nodes" > nodes< / a > < / code > (required): A list of nodes or a single node on which the commands will be executed. The nodes can be specified as individual node names or a node group with the < code > @< / code > prefix. Node groups can also be specified as arrays with a list of nodes inside the group.< / li >
< li > < code > commands< / code > (required): A list of commands to be executed on the specified nodes.< / li >
< li > < code > expected< / code > (optional, only used when the action is < code > test< / code > ): A single expected result for the test.< / li >
< li > < code > options< / code > (optional): Array to pass options to the run command, options are: < code > prompt< / code > , < code > parallel< / code > , < code > timeout< / code >
< / li >
< / ul >
2023-05-05 13:41:32 -03:00
< h4 id = "response_2" > Response:< / h4 >
2023-04-14 11:44:56 -03:00
< ul >
< li > A JSON object with the results of the executed commands on the nodes.< / li >
< / ul >
2023-05-12 12:05:25 -03:00
< hr >
2023-05-05 13:41:32 -03:00
< h3 id = "4-ask-ai" > 4. Ask AI< / h3 >
< p > < strong > Endpoint< / strong > : < code > /ask_ai< / code > < / p >
< p > < strong > Method< / strong > : < code > POST< / code > < / p >
< p > < strong > Description< / strong > : This route sends to chatgpt IA a request that will parse it into an understandable output for the application and then run the request.< / p >
< h4 id = "request-body_3" > Request Body:< / h4 >
< pre > < code class = "language-json" > {
" input" : " < user input request> " ,
" dryrun" : true or false
}
< / code > < / pre >
< ul >
< li > < code > input< / code > (required): The user input requesting the AI to perform an action on some devices or get the devices list.< / li >
< li > < code > dryrun< / code > (optional): If set to true, it will return the parameters to run the request but it won't run it. default is false.< / li >
< / ul >
< h4 id = "response_3" > Response:< / h4 >
< ul >
< li > A JSON array containing the action to run and the parameters and the result of the action.< / li >
< / ul >
2022-04-03 18:25:58 -03:00
< h2 id = "automation-module" > Automation module< / h2 >
2023-05-05 13:41:32 -03:00
< p > The automation module< / p >
2022-04-03 18:25:58 -03:00
< h3 id = "standalone-module" > Standalone module< / h3 >
2022-04-04 11:40:51 -03:00
< pre > < code > import connpy
2023-05-05 13:41:32 -03:00
router = connpy.node(" uniqueName" ," ip/host" , user=" user" , password=" pass" )
2022-04-03 18:25:58 -03:00
router.run([" term len 0" ," show run" ])
print(router.output)
hasip = router.test(" show ip int brief" ," 1.1.1.1" )
if hasip:
print(" Router has ip 1.1.1.1" )
else:
2022-04-04 13:52:26 -03:00
print(" router does not have ip 1.1.1.1" )
2022-04-03 18:25:58 -03:00
< / code > < / pre >
< h3 id = "using-manager-configuration" > Using manager configuration< / h3 >
2022-04-04 11:40:51 -03:00
< pre > < code > import connpy
conf = connpy.configfile()
2022-04-03 18:25:58 -03:00
device = conf.getitem(" server@office" )
2022-04-04 11:40:51 -03:00
server = connpy.node(" unique name" , **device, config=conf)
2022-04-03 18:25:58 -03:00
result = server.run([" cd /" , " ls -la" ])
print(result)
< / code > < / pre >
< h3 id = "running-parallel-tasks" > Running parallel tasks< / h3 >
2022-04-04 11:40:51 -03:00
< pre > < code > import connpy
conf = connpy.configfile()
2022-04-03 18:25:58 -03:00
#You can get the nodes from the config from a folder and fitlering in it
nodes = conf.getitem(" @office" , [" router1" , " router2" , " router3" ])
#You can also get each node individually:
nodes = {}
nodes[" router1" ] = conf.getitem(" router1@office" )
nodes[" router2" ] = conf.getitem(" router2@office" )
nodes[" router10" ] = conf.getitem(" router10@datacenter" )
#Also, you can create the nodes manually:
nodes = {}
2023-05-05 13:41:32 -03:00
nodes[" router1" ] = {" host" : " 1.1.1.1" , " user" : " user" , " password" : " pass1" }
nodes[" router2" ] = {" host" : " 1.1.1.2" , " user" : " user" , " password" : " pass2" }
nodes[" router3" ] = {" host" : " 1.1.1.2" , " user" : " user" , " password" : " pass3" }
2022-04-03 18:25:58 -03:00
#Finally you run some tasks on the nodes
2022-04-04 11:40:51 -03:00
mynodes = connpy.nodes(nodes, config = conf)
2022-04-03 18:25:58 -03:00
result = mynodes.test([" show ip int br" ], " 1.1.1.2" )
for i in result:
print(" ---" + i + " ---" )
print(result[i])
print()
# Or for one specific node
mynodes.router1.run([" term len 0" . " show run" ], folder = " /home/user/logs" )
< / code > < / pre >
2022-04-28 12:49:31 -03:00
< h3 id = "using-variables" > Using variables< / h3 >
< pre > < code > import connpy
config = connpy.configfile()
nodes = config.getitem(" @office" , [" router1" , " router2" , " router3" ])
commands = []
commands.append(" config t" )
commands.append(" interface lo {id}" )
commands.append(" ip add {ip} {mask}" )
commands.append(" end" )
variables = {}
2022-05-11 14:25:43 -03:00
variables[" router1@office" ] = {" ip" : " 10.57.57.1" }
variables[" router2@office" ] = {" ip" : " 10.57.57.2" }
variables[" router3@office" ] = {" ip" : " 10.57.57.3" }
2022-04-28 12:49:31 -03:00
variables[" __global__" ] = {" id" : " 57" }
variables[" __global__" ][" mask" ] = " 255.255.255.255"
expected = " !"
routers = connpy.nodes(nodes, config = config)
routers.run(commands, variables)
routers.test(" ping {ip}" , expected, variables)
for key in routers.result:
print(key, ' ---> ', (" pass" if routers.result[key] else " fail" ))
< / code > < / pre >
2023-05-05 13:41:32 -03:00
< h3 id = "using-ai" > Using AI< / h3 >
< pre > < code > import connpy
conf = connpy.configfile()
organization = 'openai-org'
api_key = " openai-key"
myia = ai(conf, organization, api_key)
input = " go to router 1 and get me the full configuration"
result = myia.ask(input, dryrun = False)
print(result)
< / code > < / pre >
2022-04-03 09:38:00 -03:00
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > #!/usr/bin/env python3
' ' '
2022-04-03 12:00:35 -03:00
## Connection manager
2022-04-03 18:25:58 -03:00
2022-04-04 11:40:51 -03:00
Connpy is a connection manager that allows you to store nodes to connect them fast and password free.
2022-04-03 18:25:58 -03:00
### Features
- You can generate profiles and reference them from nodes using @profilename
so you dont need to edit multiple nodes when changing password or other
information.
- Nodes can be stored on @folder or @subfolder@folder to organize your
devices. Then can be referenced using node@subfolder@folder or node@folder
2022-04-18 19:19:25 -03:00
- If you have too many nodes. Get completion script using: conn config
--completion, or use fzf installing pyfzf and running conn config --fzf true
2022-04-03 18:25:58 -03:00
- Much more!
### Usage
2022-04-03 12:00:35 -03:00
```
2023-11-03 11:59:00 -03:00
usage: conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]
2023-12-15 12:27:51 -03:00
conn {profile,move,mv,copy,cp,list,ls,bulk,export,import,ai,run,api,plugin,config} ...
2022-04-03 12:00:35 -03:00
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:
2022-04-04 14:41:51 -03:00
-h, --help show this help message and exit
2022-04-18 19:19:25 -03:00
-v, --version Show version
2022-04-04 14:41:51 -03:00
-a, --add Add new node[@subfolder][@folder] or [@subfolder]@folder
-r, --del, --rm Delete node[@subfolder][@folder] or [@subfolder]@folder
-e, --mod, --edit Modify node[@subfolder][@folder]
-s, --show Show node[@subfolder][@folder]
-d, --debug Display all conections steps
2023-11-03 11:59:00 -03:00
-t, --sftp Connects using sftp instead of ssh
2022-04-03 12:00:35 -03:00
Commands:
2023-12-15 12:27:51 -03:00
profile Manage profiles
move(mv) Move node
copy(cp) Copy node
list(ls) List profiles, nodes or folders
bulk Add nodes in bulk
export Export connection folder to Yaml file
import Import connection folder to config from Yaml file
ai Make request to an AI
run Run scripts or commands on nodes
api Start and stop connpy api
plugin Manage plugins
config Manage app config
2022-04-03 12:00:35 -03:00
```
2022-04-03 18:25:58 -03:00
### Manage profiles
2022-04-03 12:00:35 -03:00
```
usage: conn profile [-h] (--add | --del | --mod | --show) profile
positional arguments:
profile Name of profile to manage
options:
2022-04-04 14:41:51 -03:00
-h, --help show this help message and exit
-a, --add Add new profile
-r, --del, --rm Delete profile
-e, --mod, --edit Modify profile
-s, --show Show profile
2022-04-03 12:00:35 -03:00
```
2022-04-03 18:25:58 -03:00
### Examples
2022-04-03 12:00:35 -03:00
```
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
```
2023-12-15 12:27:51 -03:00
## Plugin Requirements for Connpy
### General Structure
- The plugin script must be a Python file.
- Only the following top-level elements are allowed in the plugin script:
- Class definitions
- Function definitions
- Import statements
- The `if __name__ == " __main__" :` block for standalone execution
- Pass statements
### Specific Class Requirements
- The plugin script must define at least two specific classes:
1. **Class `Parser`**:
- Must contain only one method: `__init__`.
- The `__init__` method must initialize at least two attributes:
- `self.parser`: An instance of `argparse.ArgumentParser`.
- `self.description`: A string containing the description of the parser.
2. **Class `Entrypoint`**:
- Must have an `__init__` method that accepts exactly three parameters besides `self`:
- `args`: Arguments passed to the plugin.
- The parser instance (typically `self.parser` from the `Parser` class).
- The Connapp instance to interact with the Connpy app.
### Executable Block
- The plugin script can include an executable block:
- `if __name__ == " __main__" :`
- This block allows the plugin to be run as a standalone script for testing or independent use.
### Script Verification
- The `verify_script` method in `plugins.py` is used to check the plugin script' s compliance with these standards.
- Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system.
-
### Example Script
For a practical example of how to write a compatible plugin script, please refer to the following example:
[Example Plugin Script](https://github.com/fluzzi/awspy)
This script demonstrates the required structure and implementation details according to the plugin system' s standards.
2023-04-14 11:44:56 -03:00
## http API
With the Connpy API you can run commands on devices using http requests
### 1. List Nodes
**Endpoint**: `/list_nodes`
**Method**: `POST`
**Description**: This route returns a list of nodes. It can also filter the list based on a given keyword.
#### Request Body:
```json
{
" filter" : " < keyword> "
}
```
* `filter` (optional): A keyword to filter the list of nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.
#### Response:
- A JSON array containing the filtered list of nodes.
---
2022-04-03 18:25:58 -03:00
2023-05-05 13:41:32 -03:00
### 2. Get Nodes
**Endpoint**: `/get_nodes`
**Method**: `POST`
**Description**: This route returns a dictionary of nodes with all their attributes. It can also filter the nodes based on a given keyword.
#### Request Body:
```json
{
" filter" : " < keyword> "
}
```
* `filter` (optional): A keyword to filter the nodes. It returns only the nodes that contain the keyword. If not provided, the route will return the entire list of nodes.
#### Response:
- A JSON array containing the filtered nodes.
---
### 3. Run Commands
2023-04-14 11:44:56 -03:00
**Endpoint**: `/run_commands`
**Method**: `POST`
**Description**: This route runs commands on selected nodes based on the provided action, nodes, and commands. It also supports executing tests by providing expected results.
#### Request Body:
```json
{
" action" : " < action> " ,
" nodes" : " < nodes> " ,
" commands" : " < commands> " ,
" expected" : " < expected> " ,
" options" : " < options> "
}
```
* `action` (required): The action to be performed. Possible values: `run` or `test`.
* `nodes` (required): A list of nodes or a single node on which the commands will be executed. The nodes can be specified as individual node names or a node group with the `@` prefix. Node groups can also be specified as arrays with a list of nodes inside the group.
* `commands` (required): A list of commands to be executed on the specified nodes.
* `expected` (optional, only used when the action is `test`): A single expected result for the test.
* `options` (optional): Array to pass options to the run command, options are: `prompt`, `parallel`, `timeout`
#### Response:
- A JSON object with the results of the executed commands on the nodes.
2022-04-03 18:25:58 -03:00
2023-05-12 12:05:25 -03:00
---
2023-05-05 13:41:32 -03:00
### 4. Ask AI
**Endpoint**: `/ask_ai`
**Method**: `POST`
**Description**: This route sends to chatgpt IA a request that will parse it into an understandable output for the application and then run the request.
#### Request Body:
```json
{
" input" : " < user input request> " ,
" dryrun" : true or false
}
```
* `input` (required): The user input requesting the AI to perform an action on some devices or get the devices list.
* `dryrun` (optional): If set to true, it will return the parameters to run the request but it won' t run it. default is false.
#### Response:
- A JSON array containing the action to run and the parameters and the result of the action.
## Automation module
The automation module
2022-04-03 18:25:58 -03:00
### Standalone module
```
2022-04-04 11:40:51 -03:00
import connpy
2023-05-05 13:41:32 -03:00
router = connpy.node(" uniqueName" ," ip/host" , user=" user" , password=" pass" )
2022-04-03 18:25:58 -03:00
router.run([" term len 0" ," show run" ])
print(router.output)
hasip = router.test(" show ip int brief" ," 1.1.1.1" )
if hasip:
print(" Router has ip 1.1.1.1" )
else:
2022-04-04 13:52:26 -03:00
print(" router does not have ip 1.1.1.1" )
2022-04-03 18:25:58 -03:00
```
### Using manager configuration
```
2022-04-04 11:40:51 -03:00
import connpy
conf = connpy.configfile()
2022-04-03 18:25:58 -03:00
device = conf.getitem(" server@office" )
2022-04-04 11:40:51 -03:00
server = connpy.node(" unique name" , **device, config=conf)
2022-04-03 18:25:58 -03:00
result = server.run([" cd /" , " ls -la" ])
print(result)
```
### Running parallel tasks
```
2022-04-04 11:40:51 -03:00
import connpy
conf = connpy.configfile()
2022-04-03 18:25:58 -03:00
#You can get the nodes from the config from a folder and fitlering in it
nodes = conf.getitem(" @office" , [" router1" , " router2" , " router3" ])
#You can also get each node individually:
nodes = {}
nodes[" router1" ] = conf.getitem(" router1@office" )
nodes[" router2" ] = conf.getitem(" router2@office" )
nodes[" router10" ] = conf.getitem(" router10@datacenter" )
#Also, you can create the nodes manually:
nodes = {}
2023-05-05 13:41:32 -03:00
nodes[" router1" ] = {" host" : " 1.1.1.1" , " user" : " user" , " password" : " pass1" }
nodes[" router2" ] = {" host" : " 1.1.1.2" , " user" : " user" , " password" : " pass2" }
nodes[" router3" ] = {" host" : " 1.1.1.2" , " user" : " user" , " password" : " pass3" }
2022-04-03 18:25:58 -03:00
#Finally you run some tasks on the nodes
2022-04-04 11:40:51 -03:00
mynodes = connpy.nodes(nodes, config = conf)
2022-04-03 18:25:58 -03:00
result = mynodes.test([" show ip int br" ], " 1.1.1.2" )
for i in result:
print(" ---" + i + " ---" )
print(result[i])
print()
# Or for one specific node
mynodes.router1.run([" term len 0" . " show run" ], folder = " /home/user/logs" )
```
2022-04-28 12:49:31 -03:00
### Using variables
```
import connpy
config = connpy.configfile()
nodes = config.getitem(" @office" , [" router1" , " router2" , " router3" ])
commands = []
commands.append(" config t" )
commands.append(" interface lo {id}" )
commands.append(" ip add {ip} {mask}" )
commands.append(" end" )
variables = {}
2022-05-11 14:25:43 -03:00
variables[" router1@office" ] = {" ip" : " 10.57.57.1" }
variables[" router2@office" ] = {" ip" : " 10.57.57.2" }
variables[" router3@office" ] = {" ip" : " 10.57.57.3" }
2022-04-28 12:49:31 -03:00
variables[" __global__" ] = {" id" : " 57" }
variables[" __global__" ][" mask" ] = " 255.255.255.255"
expected = " !"
routers = connpy.nodes(nodes, config = config)
routers.run(commands, variables)
routers.test(" ping {ip}" , expected, variables)
for key in routers.result:
print(key, ' ---> ' , (" pass" if routers.result[key] else " fail" ))
```
2023-05-05 13:41:32 -03:00
### Using AI
```
import connpy
conf = connpy.configfile()
organization = ' openai-org'
api_key = " openai-key"
myia = ai(conf, organization, api_key)
input = " go to router 1 and get me the full configuration"
result = myia.ask(input, dryrun = False)
print(result)
```
2022-04-03 09:38:00 -03:00
' ' '
from .core import node,nodes
from .configfile import configfile
from .connapp import connapp
2023-05-05 13:41:32 -03:00
from .api import *
from .ai import ai
2023-12-15 12:27:51 -03:00
from .plugins import Plugins
2022-04-05 20:04:18 -03:00
from ._version import __version__
2022-04-03 09:38:00 -03:00
from pkg_resources import get_distribution
2023-12-15 12:27:51 -03:00
__all__ = [" node" , " nodes" , " configfile" , " connapp" , " ai" , " Plugins" ]
2022-04-03 09:38:00 -03:00
__author__ = " Federico Luzzi"
__pdoc__ = {
' core' : False,
2022-04-18 19:19:25 -03:00
' completion' : False,
2023-12-15 12:27:51 -03:00
' api' : False,
' plugins' : False
2022-04-03 09:38:00 -03:00
}< / code > < / pre >
< / details >
< / section >
< section >
< / section >
< section >
< / section >
< section >
< / section >
< section >
< h2 class = "section-title" id = "header-classes" > Classes< / h2 >
< dl >
2023-12-15 12:27:51 -03:00
< dt id = "connpy.Plugins" > < code class = "flex name class" >
< span > class < span class = "ident" > Plugins< / span > < / span >
< / code > < / dt >
< dd >
< div class = "desc" > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > class Plugins:
def __init__(self):
self.plugins = {}
self.plugin_parsers = {}
def verify_script(self, file_path):
" " "
Verifies that a given Python script meets specific structural requirements.
This function checks a Python script for compliance with predefined structural
rules. It ensures that the script contains only allowed top-level elements
(functions, classes, imports, pass statements, and a specific if __name__ block)
and that it includes mandatory classes with specific attributes and methods.
### Arguments:
- file_path (str): The file path of the Python script to be verified.
### Returns:
- str: A message indicating the type of violation if the script doesn' t meet
the requirements, or False if all requirements are met.
### Verifications:
- The presence of only allowed top-level elements.
- The existence of two specific classes: ' Parser' and ' Entrypoint' .
- ' Parser' class must only have an ' __init__' method and must assign ' self.parser'
and ' self.description' .
- ' Entrypoint' class must have an ' __init__' method accepting specific arguments.
If any of these checks fail, the function returns an error message indicating
the reason. If the script passes all checks, the function returns False,
indicating successful verification.
### Exceptions:
- SyntaxError: If the script contains a syntax error, it is caught and
returned as a part of the error message.
" " "
with open(file_path, ' r' ) as file:
source_code = file.read()
try:
tree = ast.parse(source_code)
except SyntaxError as e:
return f" Syntax error in file: {e}"
required_classes = {' Parser' , ' Entrypoint' }
found_classes = set()
for node in tree.body:
# Allow only function definitions, class definitions, and pass statements at top-level
if isinstance(node, ast.If):
# Check for the ' if __name__ == " __main__" :' block
if not (isinstance(node.test, ast.Compare) and
isinstance(node.test.left, ast.Name) and
node.test.left.id == ' __name__' and
isinstance(node.test.comparators[0], ast.Str) and
node.test.comparators[0].s == ' __main__' ):
return " Only __name__ == __main__ If is allowed"
elif not isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Import, ast.ImportFrom, ast.Pass)):
return f" Plugin can only have pass, functions, classes and imports. {node} is not allowed" # Reject any other AST types
if isinstance(node, ast.ClassDef) and node.name in required_classes:
found_classes.add(node.name)
if node.name == ' Parser' :
# Ensure Parser class has only the __init__ method and assigns self.parser
if not all(isinstance(method, ast.FunctionDef) and method.name == ' __init__' for method in node.body):
return " Parser class should only have __init__ method"
# Check if ' self.parser' and ' self.description' are assigned in __init__ method
init_method = node.body[0]
assigned_attrs = [target.attr for expr in init_method.body if isinstance(expr, ast.Assign) for target in expr.targets if isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name) and target.value.id == ' self' ]
if ' parser' not in assigned_attrs or ' description' not in assigned_attrs:
return " Parser class should set self.parser and self.description" # ' self.parser' or ' self.description' not assigned in __init__
elif node.name == ' Entrypoint' :
init_method = next((item for item in node.body if isinstance(item, ast.FunctionDef) and item.name == ' __init__' ), None)
if not init_method or len(init_method.args.args) != 4: # self, args, parser, conapp
return " Entrypoint class should accept only arguments: args, parser and connapp" # ' Entrypoint' __init__ does not have correct signature
if required_classes == found_classes:
return False
else:
return " Classes Entrypoint and Parser are mandatory"
def _import_from_path(self, path):
spec = importlib.util.spec_from_file_location(" module.name" , path)
module = importlib.util.module_from_spec(spec)
sys.modules[" module.name" ] = module
spec.loader.exec_module(module)
return module
def _import_plugins_to_argparse(self, directory, subparsers):
for filename in os.listdir(directory):
commands = subparsers.choices.keys()
if filename.endswith(" .py" ):
root_filename = os.path.splitext(filename)[0]
if root_filename in commands:
continue
# Construct the full path
filepath = os.path.join(directory, filename)
check_file = self.verify_script(filepath)
if check_file:
continue
else:
self.plugins[root_filename] = self._import_from_path(filepath)
self.plugin_parsers[root_filename] = self.plugins[root_filename].Parser()
subparsers.add_parser(root_filename, parents=[self.plugin_parsers[root_filename].parser], add_help=False, description=self.plugin_parsers[root_filename].description)< / code > < / pre >
< / details >
< h3 > Methods< / h3 >
< dl >
< dt id = "connpy.Plugins.verify_script" > < code class = "name flex" >
< span > def < span class = "ident" > verify_script< / span > < / span > (< span > self, file_path)< / span >
< / code > < / dt >
< dd >
< div class = "desc" > < p > Verifies that a given Python script meets specific structural requirements.< / p >
< p > This function checks a Python script for compliance with predefined structural
rules. It ensures that the script contains only allowed top-level elements
(functions, classes, imports, pass statements, and a specific if < strong > name< / strong > block)
and that it includes mandatory classes with specific attributes and methods.< / p >
< h3 id = "arguments" > Arguments:< / h3 >
< pre > < code > - file_path (str): The file path of the Python script to be verified.
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > - str: A message indicating the type of violation if the script doesn't meet
the requirements, or False if all requirements are met.
< / code > < / pre >
< h3 id = "verifications" > Verifications:< / h3 >
< pre > < code > - The presence of only allowed top-level elements.
- The existence of two specific classes: 'Parser' and 'Entrypoint'.
- 'Parser' class must only have an '__init__' method and must assign 'self.parser'
and 'self.description'.
- 'Entrypoint' class must have an '__init__' method accepting specific arguments.
< / code > < / pre >
< p > If any of these checks fail, the function returns an error message indicating
the reason. If the script passes all checks, the function returns False,
indicating successful verification.< / p >
< h3 id = "exceptions" > Exceptions:< / h3 >
< pre > < code > - SyntaxError: If the script contains a syntax error, it is caught and
returned as a part of the error message.
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > def verify_script(self, file_path):
" " "
Verifies that a given Python script meets specific structural requirements.
This function checks a Python script for compliance with predefined structural
rules. It ensures that the script contains only allowed top-level elements
(functions, classes, imports, pass statements, and a specific if __name__ block)
and that it includes mandatory classes with specific attributes and methods.
### Arguments:
- file_path (str): The file path of the Python script to be verified.
### Returns:
- str: A message indicating the type of violation if the script doesn' t meet
the requirements, or False if all requirements are met.
### Verifications:
- The presence of only allowed top-level elements.
- The existence of two specific classes: ' Parser' and ' Entrypoint' .
- ' Parser' class must only have an ' __init__' method and must assign ' self.parser'
and ' self.description' .
- ' Entrypoint' class must have an ' __init__' method accepting specific arguments.
If any of these checks fail, the function returns an error message indicating
the reason. If the script passes all checks, the function returns False,
indicating successful verification.
### Exceptions:
- SyntaxError: If the script contains a syntax error, it is caught and
returned as a part of the error message.
" " "
with open(file_path, ' r' ) as file:
source_code = file.read()
try:
tree = ast.parse(source_code)
except SyntaxError as e:
return f" Syntax error in file: {e}"
required_classes = {' Parser' , ' Entrypoint' }
found_classes = set()
for node in tree.body:
# Allow only function definitions, class definitions, and pass statements at top-level
if isinstance(node, ast.If):
# Check for the ' if __name__ == " __main__" :' block
if not (isinstance(node.test, ast.Compare) and
isinstance(node.test.left, ast.Name) and
node.test.left.id == ' __name__' and
isinstance(node.test.comparators[0], ast.Str) and
node.test.comparators[0].s == ' __main__' ):
return " Only __name__ == __main__ If is allowed"
elif not isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Import, ast.ImportFrom, ast.Pass)):
return f" Plugin can only have pass, functions, classes and imports. {node} is not allowed" # Reject any other AST types
if isinstance(node, ast.ClassDef) and node.name in required_classes:
found_classes.add(node.name)
if node.name == ' Parser' :
# Ensure Parser class has only the __init__ method and assigns self.parser
if not all(isinstance(method, ast.FunctionDef) and method.name == ' __init__' for method in node.body):
return " Parser class should only have __init__ method"
# Check if ' self.parser' and ' self.description' are assigned in __init__ method
init_method = node.body[0]
assigned_attrs = [target.attr for expr in init_method.body if isinstance(expr, ast.Assign) for target in expr.targets if isinstance(target, ast.Attribute) and isinstance(target.value, ast.Name) and target.value.id == ' self' ]
if ' parser' not in assigned_attrs or ' description' not in assigned_attrs:
return " Parser class should set self.parser and self.description" # ' self.parser' or ' self.description' not assigned in __init__
elif node.name == ' Entrypoint' :
init_method = next((item for item in node.body if isinstance(item, ast.FunctionDef) and item.name == ' __init__' ), None)
if not init_method or len(init_method.args.args) != 4: # self, args, parser, conapp
return " Entrypoint class should accept only arguments: args, parser and connapp" # ' Entrypoint' __init__ does not have correct signature
if required_classes == found_classes:
return False
else:
return " Classes Entrypoint and Parser are mandatory" < / code > < / pre >
< / details >
< / dd >
< / dl >
< / dd >
2023-05-05 13:41:32 -03:00
< dt id = "connpy.ai" > < code class = "flex name class" >
< span > class < span class = "ident" > ai< / span > < / span >
2023-07-11 19:33:21 -03:00
< span > (< / span > < span > config, org=None, api_key=None, model=None, temp=0.7)< / span >
2023-05-05 13:41:32 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < p > This class generates a ai object. Containts all the information and methods to make requests to openAI chatGPT to run actions on the application.< / p >
< h3 id = "attributes" > Attributes:< / h3 >
< pre > < code > - model (str): Model of GPT api to use. Default is gpt-3.5-turbo.
- temp (float): Value between 0 and 1 that control the randomness
of generated text, with higher values increasing
creativity. Default is 0.7.
< / code > < / pre >
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - config (obj): Pass the object created with class configfile with
key for decryption and extra configuration if you
are using connection manager.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - org (str): A unique token identifying the user organization
to interact with the API.
- api_key (str): A unique authentication token required to access
and interact with the API.
- model (str): Model of GPT api to use. Default is gpt-3.5-turbo.
- temp (float): Value between 0 and 1 that control the randomness
of generated text, with higher values increasing
creativity. Default is 0.7.
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > class ai:
' ' ' This class generates a ai object. Containts all the information and methods to make requests to openAI chatGPT to run actions on the application.
### Attributes:
- model (str): Model of GPT api to use. Default is gpt-3.5-turbo.
- temp (float): Value between 0 and 1 that control the randomness
of generated text, with higher values increasing
creativity. Default is 0.7.
' ' '
2023-07-11 19:33:21 -03:00
def __init__(self, config, org = None, api_key = None, model = None, temp = 0.7):
2023-05-05 13:41:32 -03:00
' ' '
### Parameters:
- config (obj): Pass the object created with class configfile with
key for decryption and extra configuration if you
are using connection manager.
### Optional Parameters:
- org (str): A unique token identifying the user organization
to interact with the API.
- api_key (str): A unique authentication token required to access
and interact with the API.
- model (str): Model of GPT api to use. Default is gpt-3.5-turbo.
- temp (float): Value between 0 and 1 that control the randomness
of generated text, with higher values increasing
creativity. Default is 0.7.
' ' '
self.config = config
if org:
openai.organization = org
else:
try:
openai.organization = self.config.config[" openai" ][" organization" ]
except:
raise ValueError(" Missing openai organization" )
if api_key:
openai.api_key = api_key
else:
try:
openai.api_key = self.config.config[" openai" ][" api_key" ]
except:
raise ValueError(" Missing openai api_key" )
2023-07-11 19:33:21 -03:00
if model:
self.model = model
else:
try:
self.model = self.config.config[" openai" ][" model" ]
except:
2023-10-26 17:33:44 -03:00
self.model = " gpt-3.5-turbo"
2023-07-11 19:33:21 -03:00
self.temp = temp
2023-05-05 13:41:32 -03:00
self.__prompt = {}
self.__prompt[" original_system" ] = " " "
2023-07-11 19:33:21 -03:00
You are the AI chatbot and assistant of a network connection manager and automation app called connpy. When provided with user input analyze the input and extract the following information. If user wants to chat just reply and don' t call a function:
2023-05-05 13:41:32 -03:00
- type: Given a user input, identify the type of request they want to make. The input will represent one of two options:
2023-05-22 18:32:49 -03:00
1. " command" - The user wants to get information from devices by running commands.
2. " list_nodes" - The user wants to get a list of nodes, devices, servers, or routers.
The ' type' field should reflect whether the user input is a command or a request for a list of nodes.
2023-07-11 19:33:21 -03:00
- filter: One or more regex patterns indicating the device or group of devices the command should be run on. The filter can have different formats, such as:
2023-05-05 13:41:32 -03:00
- hostname
- hostname@folder
- hostname@subfolder@folder
- partofhostname
- @folder
- @subfolder@folder
- regex_pattern
2023-05-22 18:32:49 -03:00
2023-05-05 13:41:32 -03:00
The filter should be extracted from the user input exactly as it was provided.
Always preserve the exact filter pattern provided by the user, with no modifications. Do not process any regex, the application can do that.
2023-05-22 18:32:49 -03:00
2023-05-05 13:41:32 -03:00
" " "
self.__prompt[" original_user" ] = " Get the IP addresses of loopback0 for all routers from w2az1 and e1.*(prod|dev) and check if they have the ip 192.168.1.1"
2023-07-11 19:33:21 -03:00
self.__prompt[" original_assistant" ] = {" name" : " get_network_device_info" , " arguments" : " {\n \" type\" : \" command\" ,\n \" filter\" : [\" w2az1\" ,\" e1.*(prod|dev)\" ]\n}" }
self.__prompt[" original_function" ] = {}
self.__prompt[" original_function" ][" name" ] = " get_network_device_info"
self.__prompt[" original_function" ][" descriptions" ] = " You are the AI chatbot and assistant of a network connection manager and automation app called connpy. When provided with user input analyze the input and extract the information acording to the function, If user wants to chat just reply and don' t call a function" ,
self.__prompt[" original_function" ][" parameters" ] = {}
self.__prompt[" original_function" ][" parameters" ][" type" ] = " object"
self.__prompt[" original_function" ][" parameters" ][" properties" ] = {}
self.__prompt[" original_function" ][" parameters" ][" properties" ][" type" ] = {}
self.__prompt[" original_function" ][" parameters" ][" properties" ][" type" ][" type" ] = " string"
self.__prompt[" original_function" ][" parameters" ][" properties" ][" type" ][" description" ] =" " "
Categorize the user' s request based on the operation they want to perform on the nodes. The requests can be classified into the following categories:
1. " command" - This represents a request to retrieve specific information or configurations from nodes. An example would be: " go to routers in @office and get the config" .
2. " list_nodes" - This is when the user wants a list of nodes. An example could be: " get me the nodes in @office" .
" " "
self.__prompt[" original_function" ][" parameters" ][" properties" ][" type" ][" enum" ] = [" command" , " list_nodes" ]
self.__prompt[" original_function" ][" parameters" ][" properties" ][" filter" ] = {}
self.__prompt[" original_function" ][" parameters" ][" properties" ][" filter" ][" type" ] = " array"
self.__prompt[" original_function" ][" parameters" ][" properties" ][" filter" ][" items" ] = {}
self.__prompt[" original_function" ][" parameters" ][" properties" ][" filter" ][" items" ][" type" ] = " string"
self.__prompt[" original_function" ][" parameters" ][" properties" ][" filter" ][" items" ][" description" ] = " " " One or more regex patterns indicating the device or group of devices the command should be run on. The filter should be extracted from the user input exactly as it was provided.
The filter can have different formats, such as:
- hostname
- hostname@folder
- hostname@subfolder@folder
- partofhostname
- @folder
- @subfolder@folder
- regex_pattern
" " "
self.__prompt[" original_function" ][" parameters" ][" required" ] = [" type" , " filter" ]
2023-05-05 13:41:32 -03:00
self.__prompt[" command_system" ] = " " "
2023-10-26 17:33:44 -03:00
For each OS listed below, provide the command(s) needed to perform the specified action, depending on the device OS (e.g., Cisco IOSXR router, Linux server).
2023-05-12 18:30:47 -03:00
The application knows how to connect to devices via SSH, so you only need to provide the command(s) to run after connecting.
If the commands needed are not for the specific OS type, just send an empty list (e.g., []).
2023-05-22 18:32:49 -03:00
Note: Preserving the integrity of user-provided commands is of utmost importance. If a user has provided a specific command to run, include that command exactly as it was given, even if it' s not recognized or understood. Under no circumstances should you modify or alter user-provided commands.
2023-05-05 13:41:32 -03:00
" " "
self.__prompt[" command_user" ]= " " "
input: show me the full configuration for all this devices:
2023-10-26 17:33:44 -03:00
OS:
cisco ios:
2023-05-05 13:41:32 -03:00
" " "
2023-10-26 17:33:44 -03:00
self.__prompt[" command_assistant" ] = {" name" : " get_commands" , " arguments" : " {\n \" cisco ios\" : \" show running-configuration\" \n}" }
2023-07-11 19:33:21 -03:00
self.__prompt[" command_function" ] = {}
self.__prompt[" command_function" ][" name" ] = " get_commands"
self.__prompt[" command_function" ][" descriptions" ] = " " "
2023-10-26 17:33:44 -03:00
For each OS listed below, provide the command(s) needed to perform the specified action, depending on the device OS (e.g., Cisco IOSXR router, Linux server).
2023-07-11 19:33:21 -03:00
The application knows how to connect to devices via SSH, so you only need to provide the command(s) to run after connecting.
If the commands needed are not for the specific OS type, just send an empty list (e.g., []).
2023-05-05 13:41:32 -03:00
" " "
2023-07-11 19:33:21 -03:00
self.__prompt[" command_function" ][" parameters" ] = {}
self.__prompt[" command_function" ][" parameters" ][" type" ] = " object"
self.__prompt[" command_function" ][" parameters" ][" properties" ] = {}
2023-05-12 12:05:25 -03:00
self.__prompt[" confirmation_system" ] = " " "
Please analyze the user' s input and categorize it as either an affirmation or negation. Based on this analysis, respond with:
2023-07-11 19:33:21 -03:00
' true' if the input is an affirmation like ' do it' , ' go ahead' , ' sure' , etc.
' false' if the input is a negation.
' none' If the input does not fit into either of these categories.
2023-05-12 12:05:25 -03:00
" " "
self.__prompt[" confirmation_user" ] = " Yes go ahead!"
self.__prompt[" confirmation_assistant" ] = " True"
2023-07-11 19:33:21 -03:00
self.__prompt[" confirmation_function" ] = {}
self.__prompt[" confirmation_function" ][" name" ] = " get_confirmation"
self.__prompt[" confirmation_function" ][" descriptions" ] = " " "
Analize user request and respond:
" " "
self.__prompt[" confirmation_function" ][" parameters" ] = {}
self.__prompt[" confirmation_function" ][" parameters" ][" type" ] = " object"
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ] = {}
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ][" result" ] = {}
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ][" result" ][" description" ] = " " " ' true' if the input is an affirmation like ' do it' , ' go ahead' , ' sure' , etc.
' false' if the input is a negation.
' none' If the input does not fit into either of these categories" " "
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ][" result" ][" type" ] = " string"
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ][" result" ][" enum" ] = [" true" , " false" , " none" ]
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ][" response" ] = {}
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ][" response" ][" description" ] = " If the user don' t message is not an affiramtion or negation, kindly ask the user to rephrase."
self.__prompt[" confirmation_function" ][" parameters" ][" properties" ][" response" ][" type" ] = " string"
self.__prompt[" confirmation_function" ][" parameters" ][" required" ] = [" result" ]
2023-05-05 13:41:32 -03:00
2023-05-23 16:15:03 -03:00
def process_string(self, s):
if s.startswith(' [' ) and s.endswith(' ]' ) and not (s.startswith(" [' " ) and s.endswith(" ' ]" )) and not (s.startswith(' [" ' ) and s.endswith(' " ]' )):
# Extract the content inside square brackets and split by comma
content = s[1:-1].split(' ,' )
# Add single quotes around each item and join them back together with commas
new_content = ' , ' .join(f" ' {item.strip()}' " for item in content)
# Replace the old content with the new content
s = ' [' + new_content + ' ]'
return s
2023-05-12 12:05:25 -03:00
def _retry_function(self, function, max_retries, backoff_num, *args):
#Retry openai requests
retries = 0
while retries < max_retries:
try:
myfunction = function(*args)
break
2023-05-24 18:14:01 -03:00
except:
2023-05-12 12:05:25 -03:00
wait_time = backoff_num * (2 ** retries)
time.sleep(wait_time)
retries += 1
continue
if retries == max_retries:
myfunction = False
return myfunction
2023-10-26 17:33:44 -03:00
def _clean_command_response(self, raw_response, node_list):
2023-05-05 13:41:32 -03:00
#Parse response for command request to openAI GPT.
info_dict = {}
info_dict[" commands" ] = []
info_dict[" variables" ] = {}
info_dict[" variables" ][" __global__" ] = {}
2023-10-26 17:33:44 -03:00
for key, value in node_list.items():
2023-07-11 19:33:21 -03:00
newvalue = {}
2023-10-26 17:33:44 -03:00
commands = raw_response[value]
for i,e in enumerate(commands, start=1):
2023-07-11 19:33:21 -03:00
newvalue[f" command{i}" ] = e
if f" {{command{i}}}" not in info_dict[" commands" ]:
info_dict[" commands" ].append(f" {{command{i}}}" )
info_dict[" variables" ][" __global__" ][f" command{i}" ] = " "
info_dict[" variables" ][key] = newvalue
2023-05-05 13:41:32 -03:00
return info_dict
def _get_commands(self, user_input, nodes):
#Send the request for commands for each device to openAI GPT.
output_list = []
2023-07-11 19:33:21 -03:00
command_function = deepcopy(self.__prompt[" command_function" ])
2023-10-26 17:33:44 -03:00
node_list = {}
2023-05-05 13:41:32 -03:00
for key, value in nodes.items():
tags = value.get(' tags' , {})
try:
2023-05-23 16:15:03 -03:00
if os_value := tags.get(' os' ):
2023-10-26 17:33:44 -03:00
node_list[key] = os_value
output_list.append(f" {os_value}" )
command_function[" parameters" ][" properties" ][os_value] = {}
command_function[" parameters" ][" properties" ][os_value][" type" ] = " array"
command_function[" parameters" ][" properties" ][os_value][" description" ] = f" OS: {os_value}"
command_function[" parameters" ][" properties" ][os_value][" items" ] = {}
command_function[" parameters" ][" properties" ][os_value][" items" ][" type" ] = " string"
2023-05-05 13:41:32 -03:00
except:
2023-05-23 16:15:03 -03:00
pass
2023-10-26 17:33:44 -03:00
output_str = " \n" .join(list(set(output_list)))
command_input = f" input: {user_input}\n\nOS:\n{output_str}"
2023-05-05 13:41:32 -03:00
message = []
2023-05-22 18:32:49 -03:00
message.append({" role" : " system" , " content" : dedent(self.__prompt[" command_system" ]).strip()})
message.append({" role" : " user" , " content" : dedent(self.__prompt[" command_user" ]).strip()})
2023-07-11 19:33:21 -03:00
message.append({" role" : " assistant" , " content" : None, " function_call" : self.__prompt[" command_assistant" ]})
2023-05-05 13:41:32 -03:00
message.append({" role" : " user" , " content" : command_input})
2023-07-11 19:33:21 -03:00
functions = [command_function]
2023-05-05 13:41:32 -03:00
response = openai.ChatCompletion.create(
model=self.model,
messages=message,
2023-07-11 19:33:21 -03:00
functions=functions,
function_call={" name" : " get_commands" },
2023-05-05 13:41:32 -03:00
temperature=self.temp
)
output = {}
2023-07-11 19:33:21 -03:00
result = response[" choices" ][0][" message" ].to_dict()
json_result = json.loads(result[" function_call" ][" arguments" ])
2023-10-26 17:33:44 -03:00
output[" response" ] = self._clean_command_response(json_result, node_list)
2023-05-05 13:41:32 -03:00
return output
2023-05-10 12:34:19 -03:00
def _get_filter(self, user_input, chat_history = None):
2023-05-05 13:41:32 -03:00
#Send the request to identify the filter and other attributes from the user input to GPT.
message = []
2023-05-22 18:32:49 -03:00
message.append({" role" : " system" , " content" : dedent(self.__prompt[" original_system" ]).strip()})
message.append({" role" : " user" , " content" : dedent(self.__prompt[" original_user" ]).strip()})
2023-07-11 19:33:21 -03:00
message.append({" role" : " assistant" , " content" : None, " function_call" : self.__prompt[" original_assistant" ]})
functions = [self.__prompt[" original_function" ]]
2023-05-10 12:34:19 -03:00
if not chat_history:
chat_history = []
chat_history.append({" role" : " user" , " content" : user_input})
message.extend(chat_history)
2023-05-05 13:41:32 -03:00
response = openai.ChatCompletion.create(
model=self.model,
messages=message,
2023-07-11 19:33:21 -03:00
functions=functions,
function_call=" auto" ,
2023-05-05 13:41:32 -03:00
temperature=self.temp,
top_p=1
)
2023-07-11 19:33:21 -03:00
def extract_quoted_strings(text):
pattern = r' [" \' ](.*?)[" \' ]'
matches = re.findall(pattern, text)
return matches
expected = extract_quoted_strings(user_input)
2023-05-05 13:41:32 -03:00
output = {}
2023-07-11 19:33:21 -03:00
result = response[" choices" ][0][" message" ].to_dict()
if result[" content" ]:
output[" app_related" ] = False
chat_history.append({" role" : " assistant" , " content" : result[" content" ]})
output[" response" ] = result[" content" ]
else:
json_result = json.loads(result[" function_call" ][" arguments" ])
output[" app_related" ] = True
output[" filter" ] = json_result[" filter" ]
output[" type" ] = json_result[" type" ]
chat_history.append({" role" : " assistant" , " content" : result[" content" ], " function_call" : {" name" : result[" function_call" ][" name" ], " arguments" : json.dumps(json_result)}})
output[" expected" ] = expected
2023-05-10 12:34:19 -03:00
output[" chat_history" ] = chat_history
2023-05-05 13:41:32 -03:00
return output
2023-05-12 12:05:25 -03:00
def _get_confirmation(self, user_input):
#Send the request to identify if user is confirming or denying the task
message = []
message.append({" role" : " user" , " content" : user_input})
2023-07-11 19:33:21 -03:00
functions = [self.__prompt[" confirmation_function" ]]
2023-05-12 12:05:25 -03:00
response = openai.ChatCompletion.create(
model=self.model,
messages=message,
2023-07-11 19:33:21 -03:00
functions=functions,
function_call={" name" : " get_confirmation" },
2023-05-12 12:05:25 -03:00
temperature=self.temp,
top_p=1
)
2023-07-11 19:33:21 -03:00
result = response[" choices" ][0][" message" ].to_dict()
json_result = json.loads(result[" function_call" ][" arguments" ])
2023-05-12 12:05:25 -03:00
output = {}
2023-07-11 19:33:21 -03:00
if json_result[" result" ] == " true" :
output[" result" ] = True
elif json_result[" result" ] == " false" :
output[" result" ] = False
elif json_result[" result" ] == " none" :
output[" result" ] = json_result[" response" ]
2023-05-12 12:05:25 -03:00
return output
def confirm(self, user_input, max_retries=3, backoff_num=1):
' ' '
Send the user input to openAI GPT and verify if response is afirmative or negative.
### Parameters:
- user_input (str): User response confirming or denying.
### Optional Parameters:
- max_retries (int): Maximum number of retries for gpt api.
- backoff_num (int): Backoff factor for exponential wait time
between retries.
### Returns:
bool or str: True, False or str if AI coudn' t understand the response
' ' '
result = self._retry_function(self._get_confirmation, max_retries, backoff_num, user_input)
if result:
2023-07-11 19:33:21 -03:00
output = result[" result" ]
2023-05-12 12:05:25 -03:00
else:
output = f" {self.model} api is not responding right now, please try again later."
return output
def ask(self, user_input, dryrun = False, chat_history = None, max_retries=3, backoff_num=1):
2023-05-05 13:41:32 -03:00
' ' '
Send the user input to openAI GPT and parse the response to run an action in the application.
### Parameters:
- user_input (str): Request to send to openAI that will be parsed
and returned to execute on the application.
AI understands the following tasks:
- Run a command on a group of devices.
- List a group of devices.
- Test a command on a group of devices
and verify if the output contain an
expected value.
### Optional Parameters:
2023-05-12 12:05:25 -03:00
- dryrun (bool): Set to true to get the arguments to use to
run in the app. Default is false and it
will run the actions directly.
- chat_history (list): List in gpt api format for the chat history.
- max_retries (int): Maximum number of retries for gpt api.
- backoff_num (int): Backoff factor for exponential wait time
between retries.
2023-05-05 13:41:32 -03:00
### Returns:
dict: Dictionary formed with the following keys:
- input: User input received
- app_related: True if GPT detected the request to be related
to the application.
- dryrun: True/False
- response: If the request is not related to the app. this
key will contain chatGPT answer.
- action: The action detected by the AI to run in the app.
- filter: If it was detected by the AI, the filter used
to get the list of nodes to work on.
- nodes: If it' s not a dryrun, the list of nodes matched by
the filter.
- args: A dictionary of arguments required to run command(s)
on the nodes.
- result: A dictionary with the output of the commands or
the test.
2023-05-10 12:34:19 -03:00
- chat_history: The chat history between user and chatbot.
It can be used as an attribute for next request.
2023-05-05 13:41:32 -03:00
' ' '
output = {}
2023-05-18 18:27:25 -03:00
output[" dryrun" ] = dryrun
output[" input" ] = user_input
2023-05-12 12:05:25 -03:00
original = self._retry_function(self._get_filter, max_retries, backoff_num, user_input, chat_history)
if not original:
output[" app_related" ] = False
output[" response" ] = f" {self.model} api is not responding right now, please try again later."
return output
2023-07-11 19:33:21 -03:00
output[" app_related" ] = original[" app_related" ]
2023-05-10 12:34:19 -03:00
output[" chat_history" ] = original[" chat_history" ]
2023-05-05 13:41:32 -03:00
if not output[" app_related" ]:
2023-07-11 19:33:21 -03:00
output[" response" ] = original[" response" ]
2023-05-05 13:41:32 -03:00
else:
2023-07-11 19:33:21 -03:00
type = original[" type" ]
if " filter" in original:
output[" filter" ] = original[" filter" ]
2023-05-05 13:41:32 -03:00
if not self.config.config[" case" ]:
if isinstance(output[" filter" ], list):
output[" filter" ] = [item.lower() for item in output[" filter" ]]
else:
output[" filter" ] = output[" filter" ].lower()
if not dryrun or type == " command" :
thisnodes = self.config._getallnodesfull(output[" filter" ])
output[" nodes" ] = list(thisnodes.keys())
if not type == " command" :
2023-05-10 12:34:19 -03:00
output[" action" ] = " list_nodes"
2023-05-05 13:41:32 -03:00
else:
2023-05-18 18:27:25 -03:00
if thisnodes:
commands = self._retry_function(self._get_commands, max_retries, backoff_num, user_input, thisnodes)
else:
output[" app_related" ] = False
filterlist = " , " .join(output[" filter" ])
output[" response" ] = f" I' m sorry, I coudn' t find any device with filter{' s' if len(output[' filter' ]) != 1 else ' ' }: {filterlist}."
return output
2023-05-12 12:05:25 -03:00
if not commands:
output[" app_related" ] = False
output[" response" ] = f" {self.model} api is not responding right now, please try again later."
return output
2023-05-05 13:41:32 -03:00
output[" args" ] = {}
output[" args" ][" commands" ] = commands[" response" ][" commands" ]
output[" args" ][" vars" ] = commands[" response" ][" variables" ]
2023-05-23 16:15:03 -03:00
output[" nodes" ] = [item for item in output[" nodes" ] if output[" args" ][" vars" ].get(item)]
2023-07-11 19:33:21 -03:00
if original.get(" expected" ):
output[" args" ][" expected" ] = original[" expected" ]
2023-05-05 13:41:32 -03:00
output[" action" ] = " test"
else:
output[" action" ] = " run"
2023-05-12 12:05:25 -03:00
if dryrun:
output[" task" ] = []
if output[" action" ] == " test" :
output[" task" ].append({" Task" : " Verify if expected value is in command(s) output" })
output[" task" ].append({" Expected value to verify" : output[" args" ][" expected" ]})
elif output[" action" ] == " run" :
output[" task" ].append({" Task" : " Run command(s) on devices and return output" })
varstocommands = deepcopy(output[" args" ][" vars" ])
del varstocommands[" __global__" ]
output[" task" ].append({" Devices" : varstocommands})
2023-05-05 13:41:32 -03:00
if not dryrun:
mynodes = nodes(self.config.getitems(output[" nodes" ]),config=self.config)
if output[" action" ] == " test" :
output[" result" ] = mynodes.test(**output[" args" ])
2023-05-12 12:05:25 -03:00
output[" logs" ] = mynodes.output
2023-05-05 13:41:32 -03:00
elif output[" action" ] == " run" :
output[" result" ] = mynodes.run(**output[" args" ])
return output< / code > < / pre >
< / details >
< h3 > Methods< / h3 >
< dl >
< dt id = "connpy.ai.ask" > < code class = "name flex" >
2023-05-12 12:05:25 -03:00
< span > def < span class = "ident" > ask< / span > < / span > (< span > self, user_input, dryrun=False, chat_history=None, max_retries=3, backoff_num=1)< / span >
2023-05-05 13:41:32 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < p > Send the user input to openAI GPT and parse the response to run an action in the application.< / p >
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - user_input (str): Request to send to openAI that will be parsed
and returned to execute on the application.
AI understands the following tasks:
- Run a command on a group of devices.
- List a group of devices.
- Test a command on a group of devices
and verify if the output contain an
expected value.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
2023-05-12 12:05:25 -03:00
< pre > < code > - dryrun (bool): Set to true to get the arguments to use to
run in the app. Default is false and it
will run the actions directly.
- chat_history (list): List in gpt api format for the chat history.
- max_retries (int): Maximum number of retries for gpt api.
- backoff_num (int): Backoff factor for exponential wait time
between retries.
2023-05-05 13:41:32 -03:00
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > dict: Dictionary formed with the following keys:
- input: User input received
- app_related: True if GPT detected the request to be related
to the application.
- dryrun: True/False
- response: If the request is not related to the app. this
key will contain chatGPT answer.
- action: The action detected by the AI to run in the app.
- filter: If it was detected by the AI, the filter used
to get the list of nodes to work on.
- nodes: If it's not a dryrun, the list of nodes matched by
the filter.
- args: A dictionary of arguments required to run command(s)
on the nodes.
- result: A dictionary with the output of the commands or
the test.
2023-05-10 12:34:19 -03:00
- chat_history: The chat history between user and chatbot.
It can be used as an attribute for next request.
2023-05-05 13:41:32 -03:00
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2023-05-12 12:05:25 -03:00
< pre > < code class = "python" > def ask(self, user_input, dryrun = False, chat_history = None, max_retries=3, backoff_num=1):
2023-05-05 13:41:32 -03:00
' ' '
Send the user input to openAI GPT and parse the response to run an action in the application.
### Parameters:
- user_input (str): Request to send to openAI that will be parsed
and returned to execute on the application.
AI understands the following tasks:
- Run a command on a group of devices.
- List a group of devices.
- Test a command on a group of devices
and verify if the output contain an
expected value.
### Optional Parameters:
2023-05-12 12:05:25 -03:00
- dryrun (bool): Set to true to get the arguments to use to
run in the app. Default is false and it
will run the actions directly.
- chat_history (list): List in gpt api format for the chat history.
- max_retries (int): Maximum number of retries for gpt api.
- backoff_num (int): Backoff factor for exponential wait time
between retries.
2023-05-05 13:41:32 -03:00
### Returns:
dict: Dictionary formed with the following keys:
- input: User input received
- app_related: True if GPT detected the request to be related
to the application.
- dryrun: True/False
- response: If the request is not related to the app. this
key will contain chatGPT answer.
- action: The action detected by the AI to run in the app.
- filter: If it was detected by the AI, the filter used
to get the list of nodes to work on.
- nodes: If it' s not a dryrun, the list of nodes matched by
the filter.
- args: A dictionary of arguments required to run command(s)
on the nodes.
- result: A dictionary with the output of the commands or
the test.
2023-05-10 12:34:19 -03:00
- chat_history: The chat history between user and chatbot.
It can be used as an attribute for next request.
2023-05-05 13:41:32 -03:00
' ' '
output = {}
2023-05-18 18:27:25 -03:00
output[" dryrun" ] = dryrun
output[" input" ] = user_input
2023-05-12 12:05:25 -03:00
original = self._retry_function(self._get_filter, max_retries, backoff_num, user_input, chat_history)
if not original:
output[" app_related" ] = False
output[" response" ] = f" {self.model} api is not responding right now, please try again later."
return output
2023-07-11 19:33:21 -03:00
output[" app_related" ] = original[" app_related" ]
2023-05-10 12:34:19 -03:00
output[" chat_history" ] = original[" chat_history" ]
2023-05-05 13:41:32 -03:00
if not output[" app_related" ]:
2023-07-11 19:33:21 -03:00
output[" response" ] = original[" response" ]
2023-05-05 13:41:32 -03:00
else:
2023-07-11 19:33:21 -03:00
type = original[" type" ]
if " filter" in original:
output[" filter" ] = original[" filter" ]
2023-05-05 13:41:32 -03:00
if not self.config.config[" case" ]:
if isinstance(output[" filter" ], list):
output[" filter" ] = [item.lower() for item in output[" filter" ]]
else:
output[" filter" ] = output[" filter" ].lower()
if not dryrun or type == " command" :
thisnodes = self.config._getallnodesfull(output[" filter" ])
output[" nodes" ] = list(thisnodes.keys())
if not type == " command" :
2023-05-10 12:34:19 -03:00
output[" action" ] = " list_nodes"
2023-05-05 13:41:32 -03:00
else:
2023-05-18 18:27:25 -03:00
if thisnodes:
commands = self._retry_function(self._get_commands, max_retries, backoff_num, user_input, thisnodes)
else:
output[" app_related" ] = False
filterlist = " , " .join(output[" filter" ])
output[" response" ] = f" I' m sorry, I coudn' t find any device with filter{' s' if len(output[' filter' ]) != 1 else ' ' }: {filterlist}."
return output
2023-05-12 12:05:25 -03:00
if not commands:
output[" app_related" ] = False
output[" response" ] = f" {self.model} api is not responding right now, please try again later."
return output
2023-05-05 13:41:32 -03:00
output[" args" ] = {}
output[" args" ][" commands" ] = commands[" response" ][" commands" ]
output[" args" ][" vars" ] = commands[" response" ][" variables" ]
2023-05-23 16:15:03 -03:00
output[" nodes" ] = [item for item in output[" nodes" ] if output[" args" ][" vars" ].get(item)]
2023-07-11 19:33:21 -03:00
if original.get(" expected" ):
output[" args" ][" expected" ] = original[" expected" ]
2023-05-05 13:41:32 -03:00
output[" action" ] = " test"
else:
output[" action" ] = " run"
2023-05-12 12:05:25 -03:00
if dryrun:
output[" task" ] = []
if output[" action" ] == " test" :
output[" task" ].append({" Task" : " Verify if expected value is in command(s) output" })
output[" task" ].append({" Expected value to verify" : output[" args" ][" expected" ]})
elif output[" action" ] == " run" :
output[" task" ].append({" Task" : " Run command(s) on devices and return output" })
varstocommands = deepcopy(output[" args" ][" vars" ])
del varstocommands[" __global__" ]
output[" task" ].append({" Devices" : varstocommands})
2023-05-05 13:41:32 -03:00
if not dryrun:
mynodes = nodes(self.config.getitems(output[" nodes" ]),config=self.config)
if output[" action" ] == " test" :
output[" result" ] = mynodes.test(**output[" args" ])
2023-05-12 12:05:25 -03:00
output[" logs" ] = mynodes.output
2023-05-05 13:41:32 -03:00
elif output[" action" ] == " run" :
output[" result" ] = mynodes.run(**output[" args" ])
return output< / code > < / pre >
< / details >
< / dd >
2023-05-12 12:05:25 -03:00
< dt id = "connpy.ai.confirm" > < code class = "name flex" >
< span > def < span class = "ident" > confirm< / span > < / span > (< span > self, user_input, max_retries=3, backoff_num=1)< / span >
< / code > < / dt >
< dd >
< div class = "desc" > < p > Send the user input to openAI GPT and verify if response is afirmative or negative.< / p >
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - user_input (str): User response confirming or denying.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - max_retries (int): Maximum number of retries for gpt api.
- backoff_num (int): Backoff factor for exponential wait time
between retries.
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > bool or str: True, False or str if AI coudn't understand the response
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > def confirm(self, user_input, max_retries=3, backoff_num=1):
' ' '
Send the user input to openAI GPT and verify if response is afirmative or negative.
### Parameters:
- user_input (str): User response confirming or denying.
### Optional Parameters:
- max_retries (int): Maximum number of retries for gpt api.
- backoff_num (int): Backoff factor for exponential wait time
between retries.
### Returns:
bool or str: True, False or str if AI coudn' t understand the response
' ' '
result = self._retry_function(self._get_confirmation, max_retries, backoff_num, user_input)
if result:
2023-07-11 19:33:21 -03:00
output = result[" result" ]
2023-05-12 12:05:25 -03:00
else:
output = f" {self.model} api is not responding right now, please try again later."
return output< / code > < / pre >
< / details >
< / dd >
2023-05-23 16:15:03 -03:00
< dt id = "connpy.ai.process_string" > < code class = "name flex" >
< span > def < span class = "ident" > process_string< / span > < / span > (< span > self, s)< / span >
< / code > < / dt >
< dd >
< div class = "desc" > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > def process_string(self, s):
if s.startswith(' [' ) and s.endswith(' ]' ) and not (s.startswith(" [' " ) and s.endswith(" ' ]" )) and not (s.startswith(' [" ' ) and s.endswith(' " ]' )):
# Extract the content inside square brackets and split by comma
content = s[1:-1].split(' ,' )
# Add single quotes around each item and join them back together with commas
new_content = ' , ' .join(f" ' {item.strip()}' " for item in content)
# Replace the old content with the new content
s = ' [' + new_content + ' ]'
return s< / code > < / pre >
< / details >
< / dd >
2023-05-05 13:41:32 -03:00
< / dl >
< / dd >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.configfile" > < code class = "flex name class" >
2022-04-03 09:38:00 -03:00
< span > class < span class = "ident" > configfile< / span > < / span >
2022-04-03 18:25:58 -03:00
< span > (< / span > < span > conf=None, key=None)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
2022-04-03 18:25:58 -03:00
< div class = "desc" > < p > This class generates a configfile object. Containts a dictionary storing, config, nodes and profiles, normaly used by connection manager.< / p >
< h3 id = "attributes" > Attributes:< / h3 >
< pre > < code > - file (str): Path/file to config file.
- key (str): Path/file to RSA key file.
- config (dict): Dictionary containing information of connection
manager configuration.
- connections (dict): Dictionary containing all the nodes added to
connection manager.
- profiles (dict): Dictionary containing all the profiles added to
connection manager.
- privatekey (obj): Object containing the private key to encrypt
passwords.
- publickey (obj): Object containing the public key to decrypt
passwords.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - conf (str): Path/file to config file. If left empty default
path is ~/.config/conn/config.json
- key (str): Path/file to RSA key file. If left empty default
path is ~/.config/conn/.osk
< / code > < / pre > < / div >
2022-04-03 09:38:00 -03:00
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > class configfile:
2022-04-03 18:25:58 -03:00
' ' ' This class generates a configfile object. Containts a dictionary storing, config, nodes and profiles, normaly used by connection manager.
### Attributes:
- file (str): Path/file to config file.
- key (str): Path/file to RSA key file.
- config (dict): Dictionary containing information of connection
manager configuration.
- connections (dict): Dictionary containing all the nodes added to
connection manager.
- profiles (dict): Dictionary containing all the profiles added to
connection manager.
- privatekey (obj): Object containing the private key to encrypt
passwords.
- publickey (obj): Object containing the public key to decrypt
passwords.
' ' '
def __init__(self, conf = None, key = None):
' ' '
### Optional Parameters:
- conf (str): Path/file to config file. If left empty default
path is ~/.config/conn/config.json
- key (str): Path/file to RSA key file. If left empty default
path is ~/.config/conn/.osk
' ' '
2022-04-03 09:38:00 -03:00
home = os.path.expanduser(" ~" )
2022-04-03 18:25:58 -03:00
defaultdir = home + ' /.config/conn'
2023-12-15 12:27:51 -03:00
self.defaultdir = defaultdir
2022-04-03 18:25:58 -03:00
Path(defaultdir).mkdir(parents=True, exist_ok=True)
2023-12-15 12:27:51 -03:00
Path(f" {defaultdir}/plugins" ).mkdir(parents=True, exist_ok=True)
2023-04-14 11:44:56 -03:00
pathfile = defaultdir + ' /.folder'
try:
with open(pathfile, " r" ) as f:
configdir = f.read().strip()
except:
with open(pathfile, " w" ) as f:
f.write(str(defaultdir))
configdir = defaultdir
defaultfile = configdir + ' /config.json'
defaultkey = configdir + ' /.osk'
2022-04-03 09:38:00 -03:00
if conf == None:
2022-04-03 18:25:58 -03:00
self.file = defaultfile
2022-04-03 09:38:00 -03:00
else:
self.file = conf
if key == None:
2022-04-03 18:25:58 -03:00
self.key = defaultkey
2022-04-03 09:38:00 -03:00
else:
self.key = key
if os.path.exists(self.file):
2022-04-03 18:25:58 -03:00
config = self._loadconfig(self.file)
2022-04-03 09:38:00 -03:00
else:
2022-04-03 18:25:58 -03:00
config = self._createconfig(self.file)
2022-04-03 09:38:00 -03:00
self.config = config[" config" ]
self.connections = config[" connections" ]
self.profiles = config[" profiles" ]
if not os.path.exists(self.key):
2022-04-03 18:25:58 -03:00
self._createkey(self.key)
2022-06-10 13:24:26 -03:00
with open(self.key) as f:
self.privatekey = RSA.import_key(f.read())
f.close()
2022-04-03 09:38:00 -03:00
self.publickey = self.privatekey.publickey()
2022-04-03 18:25:58 -03:00
def _loadconfig(self, conf):
#Loads config file
2022-04-03 09:38:00 -03:00
jsonconf = open(conf)
2022-06-10 13:24:26 -03:00
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
2022-04-03 09:38:00 -03:00
2022-04-03 18:25:58 -03:00
def _createconfig(self, conf):
#Create config file
2023-12-06 10:53:42 -03:00
defaultconfig = {' config' : {' case' : False, ' idletime' : 30, ' fzf' : False}, ' connections' : {}, ' profiles' : { " default" : { " host" :" " , " protocol" :" ssh" , " port" :" " , " user" :" " , " password" :" " , " options" :" " , " logs" :" " , " tags" : " " , " jumphost" :" " }}}
2022-04-03 09:38:00 -03:00
if not os.path.exists(conf):
with open(conf, " w" ) as f:
json.dump(defaultconfig, f, indent = 4)
f.close()
os.chmod(conf, 0o600)
jsonconf = open(conf)
2022-06-10 13:24:26 -03:00
jsondata = json.load(jsonconf)
jsonconf.close()
return jsondata
2022-04-03 09:38:00 -03:00
2022-04-03 18:25:58 -03:00
def _saveconfig(self, conf):
#Save config file
2022-04-03 09:38:00 -03:00
newconfig = {" config" :{}, " connections" : {}, " profiles" : {}}
newconfig[" config" ] = self.config
newconfig[" connections" ] = self.connections
newconfig[" profiles" ] = self.profiles
with open(conf, " w" ) as f:
json.dump(newconfig, f, indent = 4)
f.close()
2022-04-03 18:25:58 -03:00
def _createkey(self, keyfile):
#Create key file
2022-04-03 09:38:00 -03:00
key = RSA.generate(2048)
with open(keyfile,' wb' ) as f:
f.write(key.export_key(' PEM' ))
f.close()
os.chmod(keyfile, 0o600)
2022-06-10 13:24:26 -03:00
return key
2022-04-03 09:38:00 -03:00
def _explode_unique(self, unique):
2022-04-03 18:25:58 -03:00
#Divide unique name into folder, subfolder and id
2022-04-03 09:38:00 -03:00
uniques = unique.split(" @" )
if not unique.startswith(" @" ):
result = {" id" : uniques[0]}
else:
result = {}
if len(uniques) == 2:
result[" folder" ] = uniques[1]
if result[" folder" ] == " " :
return False
elif len(uniques) == 3:
result[" folder" ] = uniques[2]
result[" subfolder" ] = uniques[1]
if result[" folder" ] == " " or result[" subfolder" ] == " " :
return False
elif len(uniques) > 3:
return False
return result
def getitem(self, unique, keys = None):
2022-04-03 18:25:58 -03:00
' ' '
Get an node or a group of nodes from configfile which can be passed to node/nodes class
### Parameters:
- unique (str): Unique name of the node or folder in config using
connection manager style: node[@subfolder][@folder]
or [@subfolder]@folder
### Optional Parameters:
- keys (list): In case you pass a folder as unique, you can filter
nodes inside the folder passing a list.
### Returns:
2023-05-05 13:41:32 -03:00
dict: Dictionary containing information of node or multiple
dictionaries of multiple nodes.
2022-04-03 18:25:58 -03:00
' ' '
uniques = self._explode_unique(unique)
if unique.startswith(" @" ):
if uniques.keys() > = {" folder" , " subfolder" }:
folder = self.connections[uniques[" folder" ]][uniques[" subfolder" ]]
2022-04-03 09:38:00 -03:00
else:
2022-04-03 18:25:58 -03:00
folder = self.connections[uniques[" folder" ]]
2022-05-11 14:25:43 -03:00
newfolder = deepcopy(folder)
2022-04-03 18:25:58 -03:00
newfolder.pop(" type" )
2022-04-05 18:31:19 -03:00
for node in folder.keys():
if node == " type" :
continue
2022-04-03 18:25:58 -03:00
if " type" in newfolder[node].keys():
2022-04-05 18:31:19 -03:00
if newfolder[node][" type" ] == " subfolder" :
newfolder.pop(node)
else:
newfolder[node].pop(" type" )
2022-04-03 18:25:58 -03:00
if keys == None:
2022-05-11 14:25:43 -03:00
newfolder = {" {}{}" .format(k,unique):v for k,v in newfolder.items()}
2022-04-03 18:25:58 -03:00
return newfolder
else:
f_newfolder = dict((k, newfolder[k]) for k in keys)
2022-05-11 14:25:43 -03:00
f_newfolder = {" {}{}" .format(k,unique):v for k,v in f_newfolder.items()}
2022-04-03 18:25:58 -03:00
return f_newfolder
else:
if uniques.keys() > = {" folder" , " subfolder" }:
node = self.connections[uniques[" folder" ]][uniques[" subfolder" ]][uniques[" id" ]]
elif " folder" in uniques.keys():
node = self.connections[uniques[" folder" ]][uniques[" id" ]]
else:
node = self.connections[uniques[" id" ]]
2022-05-11 14:25:43 -03:00
newnode = deepcopy(node)
2022-04-03 18:25:58 -03:00
newnode.pop(" type" )
return newnode
2022-04-03 09:38:00 -03:00
2023-05-05 13:41:32 -03:00
def getitems(self, uniques):
' ' '
Get a group of nodes from configfile which can be passed to node/nodes class
### Parameters:
2023-09-12 12:33:33 -03:00
- uniques (str/list): String name that will match hostnames
2023-05-05 13:41:32 -03:00
from the connection manager. It can be a
list of strings.
### Returns:
dict: Dictionary containing information of node or multiple
dictionaries of multiple nodes.
' ' '
nodes = {}
2023-09-12 12:33:33 -03:00
if isinstance(uniques, str):
uniques = [uniques]
2023-05-05 13:41:32 -03:00
for i in uniques:
if isinstance(i, dict):
name = list(i.keys())[0]
mylist = i[name]
if not self.config[" case" ]:
name = name.lower()
mylist = [item.lower() for item in mylist]
this = self.getitem(name, mylist)
nodes.update(this)
elif i.startswith(" @" ):
if not self.config[" case" ]:
i = i.lower()
this = self.getitem(i)
nodes.update(this)
else:
if not self.config[" case" ]:
i = i.lower()
this = self.getitem(i)
nodes[i] = this
return nodes
2023-12-06 10:53:42 -03:00
def _connections_add(self,*, id, host, folder=' ' , subfolder=' ' , options=' ' , logs=' ' , password=' ' , port=' ' , protocol=' ' , user=' ' , tags=' ' , jumphost=' ' , type = " connection" ):
2022-04-03 18:25:58 -03:00
#Add connection from config
2022-04-03 09:38:00 -03:00
if folder == ' ' :
2023-12-06 10:53:42 -03:00
self.connections[id] = {" host" : host, " options" : options, " logs" : logs, " password" : password, " port" : port, " protocol" : protocol, " user" : user, " tags" : tags," jumphost" : jumphost," type" : type}
2022-04-03 09:38:00 -03:00
elif folder != ' ' and subfolder == ' ' :
2023-12-06 10:53:42 -03:00
self.connections[folder][id] = {" host" : host, " options" : options, " logs" : logs, " password" : password, " port" : port, " protocol" : protocol, " user" : user, " tags" : tags, " jumphost" : jumphost, " type" : type}
2022-04-03 09:38:00 -03:00
elif folder != ' ' and subfolder != ' ' :
2023-12-06 10:53:42 -03:00
self.connections[folder][subfolder][id] = {" host" : host, " options" : options, " logs" : logs, " password" : password, " port" : port, " protocol" : protocol, " user" : user, " tags" : tags, " jumphost" : jumphost, " type" : type}
2022-04-03 09:38:00 -03:00
def _connections_del(self,*, id, folder=' ' , subfolder=' ' ):
2022-04-03 18:25:58 -03:00
#Delete connection from config
2022-04-03 09:38:00 -03:00
if folder == ' ' :
del self.connections[id]
elif folder != ' ' and subfolder == ' ' :
del self.connections[folder][id]
elif folder != ' ' and subfolder != ' ' :
del self.connections[folder][subfolder][id]
def _folder_add(self,*, folder, subfolder = ' ' ):
2022-04-03 18:25:58 -03:00
#Add Folder from config
2022-04-03 09:38:00 -03:00
if subfolder == ' ' :
if folder not in self.connections:
self.connections[folder] = {" type" : " folder" }
else:
if subfolder not in self.connections[folder]:
self.connections[folder][subfolder] = {" type" : " subfolder" }
def _folder_del(self,*, folder, subfolder=' ' ):
2022-04-03 18:25:58 -03:00
#Delete folder from config
2022-04-03 09:38:00 -03:00
if subfolder == ' ' :
del self.connections[folder]
else:
del self.connections[folder][subfolder]
2023-12-06 10:53:42 -03:00
def _profiles_add(self,*, id, host = ' ' , options=' ' , logs=' ' , password=' ' , port=' ' , protocol=' ' , user=' ' , tags=' ' , jumphost=' ' ):
2022-04-03 18:25:58 -03:00
#Add profile from config
2023-12-06 10:53:42 -03:00
self.profiles[id] = {" host" : host, " options" : options, " logs" : logs, " password" : password, " port" : port, " protocol" : protocol, " user" : user, " tags" : tags, " jumphost" : jumphost}
2022-04-03 09:38:00 -03:00
def _profiles_del(self,*, id ):
2022-04-03 18:25:58 -03:00
#Delete profile from config
2023-04-14 11:44:56 -03:00
del self.profiles[id]
2023-05-05 13:41:32 -03:00
def _getallnodes(self, filter = None):
2023-04-14 11:44:56 -03:00
#get all nodes on configfile
nodes = []
layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v[" type" ] == " connection" ]
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v[" type" ] == " folder" ]
nodes.extend(layer1)
for f in folders:
layer2 = [k + " @" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v[" type" ] == " connection" ]
nodes.extend(layer2)
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v[" type" ] == " subfolder" ]
for s in subfolders:
layer3 = [k + " @" + s + " @" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v[" type" ] == " connection" ]
nodes.extend(layer3)
2023-05-05 13:41:32 -03:00
if filter:
if isinstance(filter, str):
nodes = [item for item in nodes if re.search(filter, item)]
elif isinstance(filter, list):
nodes = [item for item in nodes if any(re.search(pattern, item) for pattern in filter)]
else:
raise ValueError(" filter must be a string or a list of strings" )
2023-04-14 11:44:56 -03:00
return nodes
2023-09-12 12:33:33 -03:00
def _getallnodesfull(self, filter = None, extract = True):
2023-05-05 13:41:32 -03:00
#get all nodes on configfile with all their attributes.
nodes = {}
layer1 = {k:v for k,v in self.connections.items() if isinstance(v, dict) and v[" type" ] == " connection" }
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v[" type" ] == " folder" ]
nodes.update(layer1)
for f in folders:
layer2 = {k + " @" + f:v for k,v in self.connections[f].items() if isinstance(v, dict) and v[" type" ] == " connection" }
nodes.update(layer2)
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v[" type" ] == " subfolder" ]
for s in subfolders:
layer3 = {k + " @" + s + " @" + f:v for k,v in self.connections[f][s].items() if isinstance(v, dict) and v[" type" ] == " connection" }
nodes.update(layer3)
if filter:
if isinstance(filter, str):
2023-09-21 17:28:09 -03:00
filter = " ^(?!.*@).+$" if filter == " @" else filter
2023-05-05 13:41:32 -03:00
nodes = {k: v for k, v in nodes.items() if re.search(filter, k)}
elif isinstance(filter, list):
2023-09-21 17:28:09 -03:00
filter = [" ^(?!.*@).+$" if item == " @" else item for item in filter]
2023-05-05 13:41:32 -03:00
nodes = {k: v for k, v in nodes.items() if any(re.search(pattern, k) for pattern in filter)}
else:
raise ValueError(" filter must be a string or a list of strings" )
2023-09-12 12:33:33 -03:00
if extract:
for node, keys in nodes.items():
for key, value in keys.items():
profile = re.search(" ^@(.*)" , str(value))
if profile:
try:
nodes[node][key] = self.profiles[profile.group(1)][key]
except:
nodes[node][key] = " "
elif value == ' ' and key == " protocol" :
try:
nodes[node][key] = config.profiles[" default" ][key]
except:
nodes[node][key] = " ssh"
2023-05-05 13:41:32 -03:00
return nodes
2023-04-14 11:44:56 -03:00
def _getallfolders(self):
#get all folders on configfile
folders = [" @" + k for k,v in self.connections.items() if isinstance(v, dict) and v[" type" ] == " folder" ]
subfolders = []
for f in folders:
s = [" @" + k + f for k,v in self.connections[f[1:]].items() if isinstance(v, dict) and v[" type" ] == " subfolder" ]
subfolders.extend(s)
folders.extend(subfolders)
return folders
def _profileused(self, profile):
#Check if profile is used before deleting it
nodes = []
layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v[" type" ] == " connection" and (" @" + profile in v.values() or ( isinstance(v[" password" ],list) and " @" + profile in v[" password" ]))]
folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v[" type" ] == " folder" ]
nodes.extend(layer1)
for f in folders:
layer2 = [k + " @" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v[" type" ] == " connection" and (" @" + profile in v.values() or ( isinstance(v[" password" ],list) and " @" + profile in v[" password" ]))]
nodes.extend(layer2)
subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v[" type" ] == " subfolder" ]
for s in subfolders:
layer3 = [k + " @" + s + " @" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v[" type" ] == " connection" and (" @" + profile in v.values() or ( isinstance(v[" password" ],list) and " @" + profile in v[" password" ]))]
nodes.extend(layer3)
return nodes< / code > < / pre >
2022-04-03 09:38:00 -03:00
< / details >
< h3 > Methods< / h3 >
< dl >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.configfile.getitem" > < code class = "name flex" >
2022-04-03 18:25:58 -03:00
< span > def < span class = "ident" > getitem< / span > < / span > (< span > self, unique, keys=None)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
2022-04-03 18:25:58 -03:00
< div class = "desc" > < p > Get an node or a group of nodes from configfile which can be passed to node/nodes class< / p >
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - unique (str): Unique name of the node or folder in config using
connection manager style: node[@subfolder][@folder]
or [@subfolder]@folder
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - keys (list): In case you pass a folder as unique, you can filter
nodes inside the folder passing a list.
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
2023-05-05 13:41:32 -03:00
< pre > < code > dict: Dictionary containing information of node or multiple
dictionaries of multiple nodes.
2022-04-03 18:25:58 -03:00
< / code > < / pre > < / div >
2022-04-03 09:38:00 -03:00
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2022-04-03 18:25:58 -03:00
< pre > < code class = "python" > def getitem(self, unique, keys = None):
' ' '
Get an node or a group of nodes from configfile which can be passed to node/nodes class
### Parameters:
- unique (str): Unique name of the node or folder in config using
connection manager style: node[@subfolder][@folder]
or [@subfolder]@folder
### Optional Parameters:
- keys (list): In case you pass a folder as unique, you can filter
nodes inside the folder passing a list.
### Returns:
2023-05-05 13:41:32 -03:00
dict: Dictionary containing information of node or multiple
dictionaries of multiple nodes.
2022-04-03 18:25:58 -03:00
' ' '
uniques = self._explode_unique(unique)
if unique.startswith(" @" ):
if uniques.keys() > = {" folder" , " subfolder" }:
folder = self.connections[uniques[" folder" ]][uniques[" subfolder" ]]
else:
folder = self.connections[uniques[" folder" ]]
2022-05-11 14:25:43 -03:00
newfolder = deepcopy(folder)
2022-04-03 18:25:58 -03:00
newfolder.pop(" type" )
2022-04-05 18:31:19 -03:00
for node in folder.keys():
if node == " type" :
continue
2022-04-03 18:25:58 -03:00
if " type" in newfolder[node].keys():
2022-04-05 18:31:19 -03:00
if newfolder[node][" type" ] == " subfolder" :
newfolder.pop(node)
else:
newfolder[node].pop(" type" )
2022-04-03 18:25:58 -03:00
if keys == None:
2022-05-11 14:25:43 -03:00
newfolder = {" {}{}" .format(k,unique):v for k,v in newfolder.items()}
2022-04-03 18:25:58 -03:00
return newfolder
else:
f_newfolder = dict((k, newfolder[k]) for k in keys)
2022-05-11 14:25:43 -03:00
f_newfolder = {" {}{}" .format(k,unique):v for k,v in f_newfolder.items()}
2022-04-03 18:25:58 -03:00
return f_newfolder
else:
if uniques.keys() > = {" folder" , " subfolder" }:
node = self.connections[uniques[" folder" ]][uniques[" subfolder" ]][uniques[" id" ]]
elif " folder" in uniques.keys():
node = self.connections[uniques[" folder" ]][uniques[" id" ]]
else:
node = self.connections[uniques[" id" ]]
2022-05-11 14:25:43 -03:00
newnode = deepcopy(node)
2022-04-03 18:25:58 -03:00
newnode.pop(" type" )
return newnode< / code > < / pre >
2022-04-03 09:38:00 -03:00
< / details >
< / dd >
2023-05-05 13:41:32 -03:00
< dt id = "connpy.configfile.getitems" > < code class = "name flex" >
< span > def < span class = "ident" > getitems< / span > < / span > (< span > self, uniques)< / span >
< / code > < / dt >
< dd >
< div class = "desc" > < p > Get a group of nodes from configfile which can be passed to node/nodes class< / p >
< h3 id = "parameters" > Parameters:< / h3 >
2023-09-12 12:33:33 -03:00
< pre > < code > - uniques (str/list): String name that will match hostnames
2023-05-05 13:41:32 -03:00
from the connection manager. It can be a
list of strings.
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > dict: Dictionary containing information of node or multiple
dictionaries of multiple nodes.
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > def getitems(self, uniques):
' ' '
Get a group of nodes from configfile which can be passed to node/nodes class
### Parameters:
2023-09-12 12:33:33 -03:00
- uniques (str/list): String name that will match hostnames
2023-05-05 13:41:32 -03:00
from the connection manager. It can be a
list of strings.
### Returns:
dict: Dictionary containing information of node or multiple
dictionaries of multiple nodes.
' ' '
nodes = {}
2023-09-12 12:33:33 -03:00
if isinstance(uniques, str):
uniques = [uniques]
2023-05-05 13:41:32 -03:00
for i in uniques:
if isinstance(i, dict):
name = list(i.keys())[0]
mylist = i[name]
if not self.config[" case" ]:
name = name.lower()
mylist = [item.lower() for item in mylist]
this = self.getitem(name, mylist)
nodes.update(this)
elif i.startswith(" @" ):
if not self.config[" case" ]:
i = i.lower()
this = self.getitem(i)
nodes.update(this)
else:
if not self.config[" case" ]:
i = i.lower()
this = self.getitem(i)
nodes[i] = this
return nodes< / code > < / pre >
< / details >
< / dd >
2022-04-03 18:25:58 -03:00
< / dl >
2022-04-03 09:38:00 -03:00
< / dd >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.connapp" > < code class = "flex name class" >
2022-04-03 18:25:58 -03:00
< span > class < span class = "ident" > connapp< / span > < / span >
< span > (< / span > < span > config)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
2022-04-03 18:25:58 -03:00
< div class = "desc" > < p > This class starts the connection manager app. It's normally used by connection manager but you can use it on a script to run the connection manager your way and use a different configfile and key.< / p >
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - config (obj): Object generated with configfile class, it contains
the nodes configuration and the methods to manage
the config file.
< / code > < / pre > < / div >
2022-04-03 09:38:00 -03:00
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2022-04-03 18:25:58 -03:00
< pre > < code class = "python" > class connapp:
' ' ' This class starts the connection manager app. It' s normally used by connection manager but you can use it on a script to run the connection manager your way and use a different configfile and key.
' ' '
def __init__(self, config):
' ' '
### Parameters:
- config (obj): Object generated with configfile class, it contains
the nodes configuration and the methods to manage
the config file.
' ' '
self.node = node
2022-05-11 14:25:43 -03:00
self.connnodes = nodes
2022-04-03 18:25:58 -03:00
self.config = config
2023-04-14 11:44:56 -03:00
self.nodes = self.config._getallnodes()
self.folders = self.config._getallfolders()
2022-04-03 18:25:58 -03:00
self.profiles = list(self.config.profiles.keys())
self.case = self.config.config[" case" ]
2022-04-18 19:19:25 -03:00
try:
self.fzf = self.config.config[" fzf" ]
except:
self.fzf = False
2022-05-19 16:11:41 -03:00
def start(self,argv = sys.argv[1:]):
' ' '
### Parameters:
- argv (list): List of arguments to pass to the app.
Default: sys.argv[1:]
' ' '
2022-04-03 18:25:58 -03:00
#DEFAULTPARSER
defaultparser = argparse.ArgumentParser(prog = " conn" , description = " SSH and Telnet connection manager" , formatter_class=argparse.RawTextHelpFormatter)
2023-12-15 12:27:51 -03:00
subparsers = defaultparser.add_subparsers(title=" Commands" , dest=" subcommand" )
2022-04-03 18:25:58 -03:00
#NODEPARSER
2023-12-15 12:27:51 -03:00
nodeparser = subparsers.add_parser(" node" , formatter_class=argparse.RawTextHelpFormatter)
2022-04-03 18:25:58 -03:00
nodecrud = nodeparser.add_mutually_exclusive_group()
2023-10-26 17:33:44 -03:00
nodeparser.add_argument(" node" , metavar=" node|folder" , nargs=' ?' , default=None, action=self._store_type, help=self._help(" node" ))
2022-04-18 19:19:25 -03:00
nodecrud.add_argument(" -v" ," --version" , dest=" action" , action=" store_const" , help=" Show version" , const=" version" , default=" connect" )
2022-04-04 14:41:51 -03:00
nodecrud.add_argument(" -a" ," --add" , dest=" action" , action=" store_const" , help=" Add new node[@subfolder][@folder] or [@subfolder]@folder" , const=" add" , default=" connect" )
nodecrud.add_argument(" -r" ," --del" , " --rm" , dest=" action" , action=" store_const" , help=" Delete node[@subfolder][@folder] or [@subfolder]@folder" , const=" del" , default=" connect" )
nodecrud.add_argument(" -e" ," --mod" , " --edit" , dest=" action" , action=" store_const" , help=" Modify node[@subfolder][@folder]" , const=" mod" , default=" connect" )
nodecrud.add_argument(" -s" ," --show" , dest=" action" , action=" store_const" , help=" Show node[@subfolder][@folder]" , const=" show" , default=" connect" )
2023-11-03 11:59:00 -03:00
nodecrud.add_argument(" -d" ," --debug" , dest=" debug" , action=" store_true" , help=" Display all conections steps" )
nodeparser.add_argument(" -t" ," --sftp" , dest=" sftp" , action=" store_true" , help=" Connects using sftp instead of ssh" )
2022-04-03 18:25:58 -03:00
nodeparser.set_defaults(func=self._func_node)
#PROFILEPARSER
2023-12-15 12:27:51 -03:00
profileparser = subparsers.add_parser(" profile" , description=" Manage profiles" )
2022-04-03 18:25:58 -03:00
profileparser.add_argument(" profile" , nargs=1, action=self._store_type, type=self._type_profile, help=" Name of profile to manage" )
profilecrud = profileparser.add_mutually_exclusive_group(required=True)
2022-04-04 14:41:51 -03:00
profilecrud.add_argument(" -a" , " --add" , dest=" action" , action=" store_const" , help=" Add new profile" , const=" add" )
profilecrud.add_argument(" -r" , " --del" , " --rm" , dest=" action" , action=" store_const" , help=" Delete profile" , const=" del" )
profilecrud.add_argument(" -e" , " --mod" , " --edit" , dest=" action" , action=" store_const" , help=" Modify profile" , const=" mod" )
profilecrud.add_argument(" -s" , " --show" , dest=" action" , action=" store_const" , help=" Show profile" , const=" show" )
2022-04-03 18:25:58 -03:00
profileparser.set_defaults(func=self._func_profile)
#MOVEPARSER
2023-12-15 12:27:51 -03:00
moveparser = subparsers.add_parser(" move" , aliases=[" mv" ], description=" Move node" )
2022-04-03 18:25:58 -03:00
moveparser.add_argument(" move" , nargs=2, action=self._store_type, help=" Move node[@subfolder][@folder] dest_node[@subfolder][@folder]" , default=" move" , type=self._type_node)
moveparser.set_defaults(func=self._func_others)
#COPYPARSER
2023-12-15 12:27:51 -03:00
copyparser = subparsers.add_parser(" copy" , aliases=[" cp" ], description=" Copy node" )
2022-04-03 18:25:58 -03:00
copyparser.add_argument(" cp" , nargs=2, action=self._store_type, help=" Copy node[@subfolder][@folder] new_node[@subfolder][@folder]" , default=" cp" , type=self._type_node)
copyparser.set_defaults(func=self._func_others)
#LISTPARSER
2023-12-15 12:27:51 -03:00
lsparser = subparsers.add_parser(" list" , aliases=[" ls" ], description=" List profiles, nodes or folders" )
2022-04-03 18:25:58 -03:00
lsparser.add_argument(" ls" , action=self._store_type, choices=[" profiles" ," nodes" ," folders" ], help=" List profiles, nodes or folders" , default=False)
2023-10-26 17:33:44 -03:00
lsparser.add_argument(" --filter" , nargs=1, help=" Filter results" )
lsparser.add_argument(" --format" , nargs=1, help=" Format of the output of nodes using {name}, {NAME}, {location}, {LOCATION}, {host} and {HOST}" )
2022-04-03 18:25:58 -03:00
lsparser.set_defaults(func=self._func_others)
#BULKPARSER
2023-12-15 12:27:51 -03:00
bulkparser = subparsers.add_parser(" bulk" , description=" Add nodes in bulk" )
2022-04-03 18:25:58 -03:00
bulkparser.add_argument(" bulk" , const=" bulk" , nargs=0, action=self._store_type, help=" Add nodes in bulk" )
bulkparser.set_defaults(func=self._func_others)
2023-09-12 12:33:33 -03:00
# EXPORTPARSER
2023-12-15 12:27:51 -03:00
exportparser = subparsers.add_parser(" export" , description=" Export connection folder to Yaml file" )
2023-09-21 17:28:09 -03:00
exportparser.add_argument(" export" , nargs=" +" , action=self._store_type, help=" Export /path/to/file.yml [@subfolder1][@folder1] [@subfolderN][@folderN]" )
2023-09-12 12:33:33 -03:00
exportparser.set_defaults(func=self._func_export)
# IMPORTPARSER
2023-12-15 12:27:51 -03:00
importparser = subparsers.add_parser(" import" , description=" Import connection folder to config from Yaml file" )
2023-09-13 11:17:56 -03:00
importparser.add_argument(" file" , nargs=1, action=self._store_type, help=" Import /path/to/file.yml" )
2023-09-12 12:33:33 -03:00
importparser.set_defaults(func=self._func_import)
2023-07-11 19:33:21 -03:00
# AIPARSER
2023-12-15 12:27:51 -03:00
aiparser = subparsers.add_parser(" ai" , description=" Make request to an AI" )
2023-07-11 19:33:21 -03:00
aiparser.add_argument(" ask" , nargs=' *' , help=" Ask connpy AI something" )
aiparser.add_argument(" --model" , nargs=1, help=" Set the OPENAI model id" )
aiparser.add_argument(" --org" , nargs=1, help=" Set the OPENAI organization id" )
aiparser.add_argument(" --api_key" , nargs=1, help=" Set the OPENAI API key" )
aiparser.set_defaults(func=self._func_ai)
2022-05-11 14:25:43 -03:00
#RUNPARSER
2023-12-15 12:27:51 -03:00
runparser = subparsers.add_parser(" run" , description=" Run scripts or commands on nodes" , formatter_class=argparse.RawTextHelpFormatter)
2023-01-05 16:39:22 -03:00
runparser.add_argument(" run" , nargs=' +' , action=self._store_type, help=self._help(" run" ), default=" run" )
2022-05-11 14:25:43 -03:00
runparser.add_argument(" -g" ," --generate" , dest=" action" , action=" store_const" , help=" Generate yaml file template" , const=" generate" , default=" run" )
runparser.set_defaults(func=self._func_run)
2023-04-14 11:44:56 -03:00
#APIPARSER
2023-12-15 12:27:51 -03:00
apiparser = subparsers.add_parser(" api" , description=" Start and stop connpy api" )
2023-04-14 11:44:56 -03:00
apicrud = apiparser.add_mutually_exclusive_group(required=True)
2023-04-15 22:38:52 -03:00
apicrud.add_argument(" -s" ," --start" , dest=" start" , nargs=" ?" , action=self._store_type, help=" Start conppy api" , type=int, default=8048, metavar=" PORT" )
apicrud.add_argument(" -r" ," --restart" , dest=" restart" , nargs=0, action=self._store_type, help=" Restart conppy api" )
apicrud.add_argument(" -x" ," --stop" , dest=" stop" , nargs=0, action=self._store_type, help=" Stop conppy api" )
apicrud.add_argument(" -d" , " --debug" , dest=" debug" , nargs=" ?" , action=self._store_type, help=" Run connpy server on debug mode" , type=int, default=8048, metavar=" PORT" )
2023-04-14 11:44:56 -03:00
apiparser.set_defaults(func=self._func_api)
2023-12-15 12:27:51 -03:00
#PLUGINSPARSER
pluginparser = subparsers.add_parser(" plugin" , description=" Manage plugins" )
plugincrud = pluginparser.add_mutually_exclusive_group(required=True)
plugincrud.add_argument(" --add" , metavar=(" PLUGIN" , " FILE" ), nargs=2, help=" Add new plugin" )
plugincrud.add_argument(" --del" , dest=" delete" , metavar=" PLUGIN" , nargs=1, help=" Delete plugin" )
plugincrud.add_argument(" --enable" , metavar=" PLUGIN" , nargs=1, help=" Enable plugin" )
plugincrud.add_argument(" --disable" , metavar=" PLUGIN" , nargs=1, help=" Disable plugin" )
plugincrud.add_argument(" --list" , dest=" list" , action=" store_true" , help=" Disable plugin" )
pluginparser.set_defaults(func=self._func_plugin)
2022-04-03 18:25:58 -03:00
#CONFIGPARSER
2023-12-15 12:27:51 -03:00
configparser = subparsers.add_parser(" config" , description=" Manage app config" )
2022-04-18 19:19:25 -03:00
configcrud = configparser.add_mutually_exclusive_group(required=True)
configcrud.add_argument(" --allow-uppercase" , dest=" case" , nargs=1, action=self._store_type, help=" Allow case sensitive names" , choices=[" true" ," false" ])
configcrud.add_argument(" --fzf" , dest=" fzf" , nargs=1, action=self._store_type, help=" Use fzf for lists" , choices=[" true" ," false" ])
configcrud.add_argument(" --keepalive" , dest=" idletime" , nargs=1, action=self._store_type, help=" Set keepalive time in seconds, 0 to disable" , type=int, metavar=" INT" )
configcrud.add_argument(" --completion" , dest=" completion" , nargs=1, choices=[" bash" ," zsh" ], action=self._store_type, help=" Get terminal completion configuration for conn" )
2023-04-14 11:44:56 -03:00
configcrud.add_argument(" --configfolder" , dest=" configfolder" , nargs=1, action=self._store_type, help=" Set the default location for config file" , metavar=" FOLDER" )
2023-07-11 19:33:21 -03:00
configcrud.add_argument(" --openai-org" , dest=" organization" , nargs=1, action=self._store_type, help=" Set openai organization" , metavar=" ORGANIZATION" )
configcrud.add_argument(" --openai-api-key" , dest=" api_key" , nargs=1, action=self._store_type, help=" Set openai api_key" , metavar=" API_KEY" )
configcrud.add_argument(" --openai-model" , dest=" model" , nargs=1, action=self._store_type, help=" Set openai model" , metavar=" MODEL" )
2022-04-03 18:25:58 -03:00
configparser.set_defaults(func=self._func_others)
2023-12-15 12:27:51 -03:00
#Add plugins
file_path = self.config.defaultdir + " /plugins"
self.plugins = Plugins()
self.plugins._import_plugins_to_argparse(file_path, subparsers)
#Generate helps
nodeparser.usage = self._help(" usage" , subparsers)
nodeparser.epilog = self._help(" end" , subparsers)
nodeparser.help = self._help(" node" )
2022-05-11 14:25:43 -03:00
#Manage sys arguments
2023-12-15 12:27:51 -03:00
self.commands = list(subparsers.choices.keys())
profilecmds = []
for action in profileparser._actions:
profilecmds.extend(action.option_strings)
2022-05-19 16:11:41 -03:00
if len(argv) > = 2 and argv[1] == " profile" and argv[0] in profilecmds:
argv[1] = argv[0]
argv[0] = " profile"
2023-12-15 12:27:51 -03:00
if len(argv) < 1 or argv[0] not in self.commands:
2022-05-19 16:11:41 -03:00
argv.insert(0," node" )
args = defaultparser.parse_args(argv)
2023-12-15 12:27:51 -03:00
if args.subcommand in self.plugins.plugins:
self.plugins.plugins[args.subcommand].Entrypoint(args, self.plugins.plugin_parsers[args.subcommand].parser, self)
else:
return args.func(args)
2022-04-03 18:25:58 -03:00
class _store_type(argparse.Action):
#Custom store type for cli app.
def __call__(self, parser, args, values, option_string=None):
setattr(args, " data" , values)
delattr(args,self.dest)
setattr(args, " command" , self.dest)
def _func_node(self, args):
#Function called when connecting or managing nodes.
if not self.case and args.data != None:
args.data = args.data.lower()
2023-11-03 11:59:00 -03:00
actions = {" version" : self._version, " connect" : self._connect, " add" : self._add, " del" : self._del, " mod" : self._mod, " show" : self._show}
2022-06-10 13:24:26 -03:00
return actions.get(args.action)(args)
def _version(self, args):
print(__version__)
def _connect(self, args):
if args.data == None:
matches = self.nodes
2022-04-03 18:25:58 -03:00
if len(matches) == 0:
2022-06-10 13:24:26 -03:00
print(" There are no nodes created" )
print(" try: conn --help" )
exit(9)
else:
if args.data.startswith(" @" ):
matches = list(filter(lambda k: args.data in k, self.nodes))
2022-04-03 18:25:58 -03:00
else:
2022-06-10 13:24:26 -03:00
matches = list(filter(lambda k: k.startswith(args.data), self.nodes))
if len(matches) == 0:
print(" {} not found" .format(args.data))
exit(2)
elif len(matches) > 1:
matches[0] = self._choose(matches," node" , " connect" )
if matches[0] == None:
exit(7)
node = self.config.getitem(matches[0])
node = self.node(matches[0],**node, config = self.config)
2023-11-03 11:59:00 -03:00
if args.sftp:
node.protocol = " sftp"
if args.debug:
2022-06-10 13:24:26 -03:00
node.interact(debug = True)
else:
node.interact()
def _del(self, args):
if args.data == None:
print(" Missing argument node" )
exit(3)
elif args.data.startswith(" @" ):
matches = list(filter(lambda k: k == args.data, self.folders))
else:
2023-10-26 17:33:44 -03:00
matches = self.config._getallnodes(args.data)
2022-06-10 13:24:26 -03:00
if len(matches) == 0:
print(" {} not found" .format(args.data))
exit(2)
2023-10-26 17:33:44 -03:00
print(" Removing: {}" .format(matches))
question = [inquirer.Confirm(" delete" , message=" Are you sure you want to continue?" )]
2022-06-10 13:24:26 -03:00
confirm = inquirer.prompt(question)
if confirm == None:
exit(7)
if confirm[" delete" ]:
if args.data.startswith(" @" ):
2023-10-26 17:33:44 -03:00
uniques = self.config._explode_unique(matches[0])
2022-06-10 13:24:26 -03:00
self.config._folder_del(**uniques)
2022-04-03 18:25:58 -03:00
else:
2023-10-26 17:33:44 -03:00
for node in matches:
nodeuniques = self.config._explode_unique(node)
self.config._connections_del(**nodeuniques)
2022-06-10 13:24:26 -03:00
self.config._saveconfig(self.config.file)
2023-10-26 17:33:44 -03:00
if len(matches) == 1:
print(" {} deleted succesfully" .format(matches[0]))
else:
print(f" {len(matches)} nodes deleted succesfully" )
2022-06-10 13:24:26 -03:00
def _add(self, args):
2023-10-26 17:33:44 -03:00
args.data = self._type_node(args.data)
2022-06-10 13:24:26 -03:00
if args.data == None:
print(" Missing argument node" )
exit(3)
elif args.data.startswith(" @" ):
type = " folder"
matches = list(filter(lambda k: k == args.data, self.folders))
reversematches = list(filter(lambda k: " @" + k == args.data, self.nodes))
else:
type = " node"
matches = list(filter(lambda k: k == args.data, self.nodes))
reversematches = list(filter(lambda k: k == " @" + args.data, self.folders))
if len(matches) > 0:
print(" {} already exist" .format(matches[0]))
exit(4)
if len(reversematches) > 0:
print(" {} already exist" .format(reversematches[0]))
exit(4)
else:
if type == " folder" :
uniques = self.config._explode_unique(args.data)
if uniques == False:
print(" Invalid folder {}" .format(args.data))
exit(5)
if " subfolder" in uniques.keys():
parent = " @" + uniques[" folder" ]
if parent not in self.folders:
print(" Folder {} not found" .format(uniques[" folder" ]))
exit(2)
self.config._folder_add(**uniques)
self.config._saveconfig(self.config.file)
print(" {} added succesfully" .format(args.data))
if type == " node" :
nodefolder = args.data.partition(" @" )
nodefolder = " @" + nodefolder[2]
if nodefolder not in self.folders and nodefolder != " @" :
print(nodefolder + " not found" )
exit(2)
uniques = self.config._explode_unique(args.data)
if uniques == False:
print(" Invalid node {}" .format(args.data))
exit(5)
print(" You can use the configured setting in a profile using @profilename." )
print(" You can also leave empty any value except hostname/IP." )
print(" You can pass 1 or more passwords using comma separated @profiles" )
print(" You can use this variables on logging file name: ${id} ${unique} ${host} ${port} ${user} ${protocol}" )
2023-05-05 13:41:32 -03:00
print(" Some useful tags to set for automation are ' os' , ' screen_length_command' , and ' prompt' ." )
2022-06-10 13:24:26 -03:00
newnode = self._questions_nodes(args.data, uniques)
if newnode == False:
exit(7)
self.config._connections_add(**newnode)
2022-04-03 18:25:58 -03:00
self.config._saveconfig(self.config.file)
2022-06-10 13:24:26 -03:00
print(" {} added succesfully" .format(args.data))
def _show(self, args):
if args.data == None:
print(" Missing argument node" )
exit(3)
matches = list(filter(lambda k: k == args.data, self.nodes))
if len(matches) == 0:
print(" {} not found" .format(args.data))
exit(2)
node = self.config.getitem(matches[0])
for k, v in node.items():
if isinstance(v, str):
print(k + " : " + v)
2023-07-11 19:33:21 -03:00
elif isinstance(v, list):
2022-06-10 13:24:26 -03:00
print(k + " :" )
for i in v:
print(" - " + i)
2023-07-11 19:33:21 -03:00
elif isinstance(v, dict):
print(k + " :" )
for i,d in v.items():
print(" - " + i + " : " + d)
2022-06-10 13:24:26 -03:00
def _mod(self, args):
if args.data == None:
print(" Missing argument node" )
exit(3)
2023-09-12 12:33:33 -03:00
matches = self.config._getallnodes(args.data)
2022-06-10 13:24:26 -03:00
if len(matches) == 0:
2023-09-12 12:33:33 -03:00
print(" No connection found with filter: {}" .format(args.data))
2022-06-10 13:24:26 -03:00
exit(2)
2023-09-12 12:33:33 -03:00
elif len(matches) == 1:
uniques = self.config._explode_unique(args.data)
unique = matches[0]
else:
uniques = {" id" : None, " folder" : None}
unique = None
print(" Editing: {}" .format(matches))
node = {}
for i in matches:
node[i] = self.config.getitem(i)
2022-06-10 13:24:26 -03:00
edits = self._questions_edit()
if edits == None:
exit(7)
2023-09-12 12:33:33 -03:00
updatenode = self._questions_nodes(unique, uniques, edit=edits)
2022-06-10 13:24:26 -03:00
if not updatenode:
exit(7)
2023-09-12 12:33:33 -03:00
if len(matches) == 1:
uniques.update(node[matches[0]])
uniques[" type" ] = " connection"
if sorted(updatenode.items()) == sorted(uniques.items()):
print(" Nothing to do here" )
return
else:
self.config._connections_add(**updatenode)
self.config._saveconfig(self.config.file)
print(" {} edited succesfully" .format(args.data))
2022-06-10 13:24:26 -03:00
else:
2023-09-12 12:33:33 -03:00
for k in node:
updatednode = self.config._explode_unique(k)
updatednode[" type" ] = " connection"
updatednode.update(node[k])
editcount = 0
for key, should_edit in edits.items():
if should_edit:
editcount += 1
updatednode[key] = updatenode[key]
if not editcount:
print(" Nothing to do here" )
return
else:
self.config._connections_add(**updatednode)
2022-06-10 13:24:26 -03:00
self.config._saveconfig(self.config.file)
2023-09-12 12:33:33 -03:00
print(" {} edited succesfully" .format(matches))
return
2022-04-03 18:25:58 -03:00
def _func_profile(self, args):
#Function called when managing profiles
if not self.case:
args.data[0] = args.data[0].lower()
2022-06-10 13:24:26 -03:00
actions = {" add" : self._profile_add, " del" : self._profile_del, " mod" : self._profile_mod, " show" : self._profile_show}
return actions.get(args.action)(args)
def _profile_del(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print(" {} not found" .format(args.data[0]))
exit(2)
if matches[0] == " default" :
print(" Can' t delete default profile" )
exit(6)
2023-04-14 11:44:56 -03:00
usedprofile = self.config._profileused(matches[0])
2022-06-10 13:24:26 -03:00
if len(usedprofile) > 0:
print(" Profile {} used in the following nodes:" .format(matches[0]))
print(" , " .join(usedprofile))
exit(8)
question = [inquirer.Confirm(" delete" , message=" Are you sure you want to delete {}?" .format(matches[0]))]
confirm = inquirer.prompt(question)
if confirm[" delete" ]:
self.config._profiles_del(id = matches[0])
2022-04-03 18:25:58 -03:00
self.config._saveconfig(self.config.file)
2022-06-10 13:24:26 -03:00
print(" {} deleted succesfully" .format(matches[0]))
def _profile_show(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print(" {} not found" .format(args.data[0]))
exit(2)
profile = self.config.profiles[matches[0]]
for k, v in profile.items():
if isinstance(v, str):
print(k + " : " + v)
2023-07-11 19:33:21 -03:00
elif isinstance(v, list):
2022-06-10 13:24:26 -03:00
print(k + " :" )
for i in v:
print(" - " + i)
2023-07-11 19:33:21 -03:00
elif isinstance(v, dict):
print(k + " :" )
for i,d in v.items():
print(" - " + i + " : " + d)
2022-06-10 13:24:26 -03:00
def _profile_add(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) > 0:
print(" Profile {} Already exist" .format(matches[0]))
exit(4)
newprofile = self._questions_profiles(args.data[0])
if newprofile == False:
exit(7)
self.config._profiles_add(**newprofile)
self.config._saveconfig(self.config.file)
print(" {} added succesfully" .format(args.data[0]))
def _profile_mod(self, args):
matches = list(filter(lambda k: k == args.data[0], self.profiles))
if len(matches) == 0:
print(" {} not found" .format(args.data[0]))
exit(2)
profile = self.config.profiles[matches[0]]
oldprofile = {" id" : matches[0]}
oldprofile.update(profile)
edits = self._questions_edit()
if edits == None:
exit(7)
updateprofile = self._questions_profiles(matches[0], edit=edits)
if not updateprofile:
exit(7)
if sorted(updateprofile.items()) == sorted(oldprofile.items()):
print(" Nothing to do here" )
return
else:
self.config._profiles_add(**updateprofile)
self.config._saveconfig(self.config.file)
print(" {} edited succesfully" .format(args.data[0]))
2022-04-03 18:25:58 -03:00
def _func_others(self, args):
#Function called when using other commands
2023-07-11 19:33:21 -03:00
actions = {" ls" : self._ls, " move" : self._mvcp, " cp" : self._mvcp, " bulk" : self._bulk, " completion" : self._completion, " case" : self._case, " fzf" : self._fzf, " idletime" : self._idletime, " configfolder" : self._configfolder, " organization" : self._openai, " api_key" : self._openai, " model" : self._openai}
2022-06-10 13:24:26 -03:00
return actions.get(args.command)(args)
def _ls(self, args):
2023-10-26 17:33:44 -03:00
items = getattr(self, args.data)
if args.filter:
items = [ item for item in items if re.search(args.filter[0], item)]
if args.format and args.data == " nodes" :
newitems = []
for i in items:
formated = {}
info = self.config.getitem(i)
if " @" in i:
name_part, location_part = i.split(" @" , 1)
formated[" location" ] = " @" + location_part
else:
name_part = i
formated[" location" ] = " "
formated[" name" ] = name_part
formated[" host" ] = info[" host" ]
items_copy = list(formated.items())
for key, value in items_copy:
upper_key = key.upper()
upper_value = value.upper()
formated[upper_key] = upper_value
newitems.append(args.format[0].format(**formated))
items = newitems
print(*items, sep=" \n" )
2022-06-10 13:24:26 -03:00
def _mvcp(self, args):
if not self.case:
args.data[0] = args.data[0].lower()
args.data[1] = args.data[1].lower()
source = list(filter(lambda k: k == args.data[0], self.nodes))
dest = list(filter(lambda k: k == args.data[1], self.nodes))
if len(source) != 1:
print(" {} not found" .format(args.data[0]))
exit(2)
if len(dest) > 0:
print(" Node {} Already exist" .format(args.data[1]))
exit(4)
nodefolder = args.data[1].partition(" @" )
nodefolder = " @" + nodefolder[2]
if nodefolder not in self.folders and nodefolder != " @" :
print(" {} not found" .format(nodefolder))
exit(2)
olduniques = self.config._explode_unique(args.data[0])
newuniques = self.config._explode_unique(args.data[1])
if newuniques == False:
print(" Invalid node {}" .format(args.data[1]))
exit(5)
node = self.config.getitem(source[0])
newnode = {**newuniques, **node}
self.config._connections_add(**newnode)
if args.command == " move" :
self.config._connections_del(**olduniques)
self.config._saveconfig(self.config.file)
action = " moved" if args.command == " move" else " copied"
print(" {} {} succesfully to {}" .format(args.data[0],action, args.data[1]))
def _bulk(self, args):
newnodes = self._questions_bulk()
if newnodes == False:
exit(7)
if not self.case:
newnodes[" location" ] = newnodes[" location" ].lower()
newnodes[" ids" ] = newnodes[" ids" ].lower()
ids = newnodes[" ids" ].split(" ," )
hosts = newnodes[" host" ].split(" ," )
count = 0
for n in ids:
unique = n + newnodes[" location" ]
matches = list(filter(lambda k: k == unique, self.nodes))
reversematches = list(filter(lambda k: k == " @" + unique, self.folders))
if len(matches) > 0:
print(" Node {} already exist, ignoring it" .format(unique))
continue
if len(reversematches) > 0:
print(" Folder with name {} already exist, ignoring it" .format(unique))
continue
newnode = {" id" : n}
if newnodes[" location" ] != " " :
location = self.config._explode_unique(newnodes[" location" ])
newnode.update(location)
if len(hosts) > 1:
index = ids.index(n)
newnode[" host" ] = hosts[index]
else:
newnode[" host" ] = hosts[0]
newnode[" protocol" ] = newnodes[" protocol" ]
newnode[" port" ] = newnodes[" port" ]
newnode[" options" ] = newnodes[" options" ]
newnode[" logs" ] = newnodes[" logs" ]
2023-05-05 13:41:32 -03:00
newnode[" tags" ] = newnodes[" tags" ]
2023-12-06 10:53:42 -03:00
newnode[" jumphost" ] = newnodes[" jumphost" ]
2022-06-10 13:24:26 -03:00
newnode[" user" ] = newnodes[" user" ]
newnode[" password" ] = newnodes[" password" ]
count +=1
2022-04-03 18:25:58 -03:00
self.config._connections_add(**newnode)
2023-04-14 11:44:56 -03:00
self.nodes = self.config._getallnodes()
2022-06-10 13:24:26 -03:00
if count > 0:
2022-04-03 18:25:58 -03:00
self.config._saveconfig(self.config.file)
2022-06-10 13:24:26 -03:00
print(" Succesfully added {} nodes" .format(count))
2022-04-03 09:38:00 -03:00
else:
2022-06-10 13:24:26 -03:00
print(" 0 nodes added" )
def _completion(self, args):
if args.data[0] == " bash" :
print(self._help(" bashcompletion" ))
elif args.data[0] == " zsh" :
print(self._help(" zshcompletion" ))
def _case(self, args):
if args.data[0] == " true" :
args.data[0] = True
elif args.data[0] == " false" :
args.data[0] = False
self._change_settings(args.command, args.data[0])
def _fzf(self, args):
if args.data[0] == " true" :
args.data[0] = True
elif args.data[0] == " false" :
args.data[0] = False
self._change_settings(args.command, args.data[0])
def _idletime(self, args):
if args.data[0] < 0:
args.data[0] = 0
self._change_settings(args.command, args.data[0])
2023-04-14 11:44:56 -03:00
def _configfolder(self, args):
if not os.path.isdir(args.data[0]):
raise argparse.ArgumentTypeError(f" readable_dir:{args.data[0]} is not a valid path" )
else:
2023-12-15 12:27:51 -03:00
pathfile = self.config.defaultdir + " /.folder"
2023-04-14 11:44:56 -03:00
folder = os.path.abspath(args.data[0]).rstrip(' /' )
with open(pathfile, " w" ) as f:
f.write(str(folder))
print(" Config saved" )
2023-05-05 13:41:32 -03:00
def _openai(self, args):
2023-07-11 19:33:21 -03:00
if " openai" in self.config.config:
openaikeys = self.config.config[" openai" ]
else:
openaikeys = {}
openaikeys[args.command] = args.data[0]
self._change_settings(" openai" , openaikeys)
2023-05-05 13:41:32 -03:00
2023-04-14 11:44:56 -03:00
2022-06-10 13:24:26 -03:00
def _change_settings(self, name, value):
self.config.config[name] = value
self.config._saveconfig(self.config.file)
print(" Config saved" )
2022-04-03 18:25:58 -03:00
2023-12-15 12:27:51 -03:00
def _func_plugin(self, args):
if args.add:
if not os.path.exists(args.add[1]):
print(" File {} dosn' t exists." .format(args.add[1]))
exit(14)
if args.add[0].isalpha() and args.add[0].islower() and len(args.add[0]) < = 15:
disabled_dest_file = os.path.join(self.config.defaultdir + " /plugins" , args.add[0] + " .py.bkp" )
if args.add[0] in self.commands or os.path.exists(disabled_dest_file):
print(" Plugin name can' t be the same as other commands." )
exit(15)
else:
check_bad_script = self.plugins.verify_script(args.add[1])
if check_bad_script:
print(check_bad_script)
exit(16)
else:
try:
dest_file = os.path.join(self.config.defaultdir + " /plugins" , args.add[0] + " .py" )
shutil.copy2(args.add[1], dest_file)
print(f" Plugin {args.add[0]} added succesfully." )
except:
print(" Failed importing plugin file." )
exit(17)
else:
print(" Plugin name should be lowercase letters up to 15 characters." )
exit(15)
elif args.delete:
plugin_file = os.path.join(self.config.defaultdir + " /plugins" , args.delete[0] + " .py" )
disabled_plugin_file = os.path.join(self.config.defaultdir + " /plugins" , args.delete[0] + " .py.bkp" )
plugin_exist = os.path.exists(plugin_file)
disabled_plugin_exist = os.path.exists(disabled_plugin_file)
if not plugin_exist and not disabled_plugin_exist:
print(" Plugin {} dosn' t exist." .format(args.delete[0]))
exit(14)
question = [inquirer.Confirm(" delete" , message=" Are you sure you want to delete {} plugin?" .format(args.delete[0]))]
confirm = inquirer.prompt(question)
if confirm == None:
exit(7)
if confirm[" delete" ]:
try:
if plugin_exist:
os.remove(plugin_file)
elif disabled_plugin_exist:
os.remove(disabled_plugin_file)
print(f" plugin {args.delete[0]} deleted succesfully." )
except:
print(" Failed deleting plugin file." )
exit(17)
elif args.disable:
plugin_file = os.path.join(self.config.defaultdir + " /plugins" , args.disable[0] + " .py" )
disabled_plugin_file = os.path.join(self.config.defaultdir + " /plugins" , args.disable[0] + " .py.bkp" )
if not os.path.exists(plugin_file) or os.path.exists(disabled_plugin_file):
print(" Plugin {} dosn' t exist or it' s disabled." .format(args.disable[0]))
exit(14)
try:
os.rename(plugin_file, disabled_plugin_file)
print(f" plugin {args.disable[0]} disabled succesfully." )
except:
print(" Failed disabling plugin file." )
exit(17)
elif args.enable:
plugin_file = os.path.join(self.config.defaultdir + " /plugins" , args.enable[0] + " .py" )
disabled_plugin_file = os.path.join(self.config.defaultdir + " /plugins" , args.enable[0] + " .py.bkp" )
if os.path.exists(plugin_file) or not os.path.exists(disabled_plugin_file):
print(" Plugin {} dosn' t exist or it' s enabled." .format(args.enable[0]))
exit(14)
try:
os.rename(disabled_plugin_file, plugin_file)
print(f" plugin {args.enable[0]} enabled succesfully." )
except:
print(" Failed enabling plugin file." )
exit(17)
elif args.list:
enabled_files = []
disabled_files = []
plugins = {}
# Iterate over all files in the specified folder
for file in os.listdir(self.config.defaultdir + " /plugins" ):
# Check if the file is a Python file
if file.endswith(' .py' ):
enabled_files.append(os.path.splitext(file)[0])
# Check if the file is a Python backup file
elif file.endswith(' .py.bkp' ):
disabled_files.append(os.path.splitext(os.path.splitext(file)[0])[0])
if enabled_files:
plugins[" Enabled" ] = enabled_files
if disabled_files:
plugins[" Disabled" ] = disabled_files
if plugins:
print(yaml.dump(plugins, sort_keys=False))
else:
print(" There are no plugins added." )
2023-09-12 12:33:33 -03:00
def _func_import(self, args):
if not os.path.exists(args.data[0]):
2023-12-15 12:27:51 -03:00
print(" File {} dosn' t exist" .format(args.data[0]))
2023-09-12 12:33:33 -03:00
exit(14)
2023-09-13 11:17:56 -03:00
print(" This could overwrite your current configuration!" )
question = [inquirer.Confirm(" import" , message=" Are you sure you want to import {} file?" .format(args.data[0]))]
2023-09-12 12:33:33 -03:00
confirm = inquirer.prompt(question)
if confirm == None:
exit(7)
if confirm[" import" ]:
try:
with open(args.data[0]) as file:
imported = yaml.load(file, Loader=yaml.FullLoader)
except:
print(" failed reading file {}" .format(args.data[0]))
exit(10)
for k,v in imported.items():
uniques = self.config._explode_unique(k)
2023-09-21 17:28:09 -03:00
if " folder" in uniques:
folder = f" @{uniques[' folder' ]}"
matches = list(filter(lambda k: k == folder, self.folders))
if len(matches) == 0:
uniquefolder = self.config._explode_unique(folder)
self.config._folder_add(**uniquefolder)
2023-09-12 12:33:33 -03:00
if " subfolder" in uniques:
subfolder = f" @{uniques[' subfolder' ]}@{uniques[' folder' ]}"
matches = list(filter(lambda k: k == subfolder, self.folders))
if len(matches) == 0:
uniquesubfolder = self.config._explode_unique(subfolder)
self.config._folder_add(**uniquesubfolder)
uniques.update(v)
self.config._connections_add(**uniques)
self.config._saveconfig(self.config.file)
print(" File {} imported succesfully" .format(args.data[0]))
return
def _func_export(self, args):
2023-09-21 17:28:09 -03:00
if os.path.exists(args.data[0]):
print(" File {} already exists" .format(args.data[0]))
2023-09-12 12:33:33 -03:00
exit(14)
2023-09-21 17:28:09 -03:00
if len(args.data[1:]) == 0:
foldercons = self.config._getallnodesfull(extract = False)
2023-09-12 12:33:33 -03:00
else:
2023-09-21 17:28:09 -03:00
for folder in args.data[1:]:
matches = list(filter(lambda k: k == folder, self.folders))
if len(matches) == 0 and folder != " @" :
print(" {} folder not found" .format(folder))
exit(2)
foldercons = self.config._getallnodesfull(args.data[1:], extract = False)
with open(args.data[0], " w" ) as file:
yaml.dump(foldercons, file, Dumper=NoAliasDumper, default_flow_style=False)
file.close()
print(" File {} generated succesfully" .format(args.data[0]))
exit()
2023-09-12 12:33:33 -03:00
return
2022-05-11 14:25:43 -03:00
def _func_run(self, args):
if len(args.data) > 1:
2022-06-10 13:24:26 -03:00
args.action = " noderun"
actions = {" noderun" : self._node_run, " generate" : self._yaml_generate, " run" : self._yaml_run}
return actions.get(args.action)(args)
2023-07-11 19:33:21 -03:00
def _func_ai(self, args):
arguments = {}
if args.model:
arguments[" model" ] = args.model[0]
if args.org:
arguments[" org" ] = args.org[0]
if args.api_key:
arguments[" api_key" ] = args.api_key[0]
self.myai = ai(self.config, **arguments)
if args.ask:
input = " " .join(args.ask)
request = self.myai.ask(input, dryrun = True)
if not request[" app_related" ]:
mdprint(Markdown(request[" response" ]))
print(" \r" )
else:
if request[" action" ] == " list_nodes" :
if request[" filter" ]:
nodes = self.config._getallnodes(request[" filter" ])
else:
nodes = self.config._getallnodes()
list = " \n" .join(nodes)
print(list)
else:
yaml_data = yaml.dump(request[" task" ])
confirmation = f" I' m going to run the following task:\n```{yaml_data}```"
mdprint(Markdown(confirmation))
question = [inquirer.Confirm(" task" , message=" Are you sure you want to continue?" )]
print(" \r" )
confirm = inquirer.prompt(question)
if confirm == None:
exit(7)
if confirm[" task" ]:
script = {}
script[" name" ] = " RESULT"
script[" output" ] = " stdout"
script[" nodes" ] = request[" nodes" ]
script[" action" ] = request[" action" ]
if " expected" in request:
script[" expected" ] = request[" expected" ]
script.update(request[" args" ])
self._cli_run(script)
else:
history = None
mdprint(Markdown(" **Chatbot**: Hi! How can I help you today?\n\n---" ))
while True:
questions = [
inquirer.Text(' message' , message=" User" , validate=self._ai_validation),
]
answers = inquirer.prompt(questions)
if answers == None:
exit(7)
response, history = self._process_input(answers[" message" ], history)
mdprint(Markdown(f" " " **Chatbot**:\n{response}\n\n---" " " ))
return
def _ai_validation(self, answers, current, regex = " ^.+$" ):
#Validate ai user chat.
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Can' t send empty messages" )
return True
def _process_input(self, input, history):
response = self.myai.ask(input , chat_history = history, dryrun = True)
if not response[" app_related" ]:
2023-07-31 12:18:10 -03:00
try:
if not history:
history = []
history.extend(response[" chat_history" ])
except:
if not history:
history = None
2023-07-11 19:33:21 -03:00
return response[" response" ], history
else:
history = None
if response[" action" ] == " list_nodes" :
if response[" filter" ]:
nodes = self.config._getallnodes(response[" filter" ])
else:
nodes = self.config._getallnodes()
list = " \n" .join(nodes)
response = f" ```{list}\n```"
else:
yaml_data = yaml.dump(response[" task" ])
confirmresponse = f" I' m going to run the following task:\n```{yaml_data}```\nPlease confirm"
while True:
mdprint(Markdown(f" " " **Chatbot**:\n{confirmresponse}" " " ))
questions = [
inquirer.Text(' message' , message=" User" , validate=self._ai_validation),
]
answers = inquirer.prompt(questions)
if answers == None:
exit(7)
confirmation = self.myai.confirm(answers[" message" ])
if isinstance(confirmation, bool):
if not confirmation:
response = " Request cancelled"
else:
nodes = self.connnodes(self.config.getitems(response[" nodes" ]), config = self.config)
if response[" action" ] == " run" :
output = nodes.run(**response[" args" ])
response = " "
elif response[" action" ] == " test" :
result = nodes.test(**response[" args" ])
yaml_result = yaml.dump(result,default_flow_style=False, indent=4)
output = nodes.output
response = f" This is the result for your test:\n```\n{yaml_result}\n```"
for k,v in output.items():
response += f" \n***{k}***:\n```\n{v}\n```\n"
break
return response, history
2023-04-14 11:44:56 -03:00
def _func_api(self, args):
if args.command == " stop" or args.command == " restart" :
2023-04-15 22:38:52 -03:00
args.data = stop_api()
2023-04-14 11:44:56 -03:00
if args.command == " start" or args.command == " restart" :
2023-04-15 22:38:52 -03:00
if args.data:
start_api(args.data)
else:
start_api()
if args.command == " debug" :
if args.data:
debug_api(args.data)
else:
debug_api()
2023-04-14 11:44:56 -03:00
return
2022-06-10 13:24:26 -03:00
def _node_run(self, args):
command = " " .join(args.data[1:])
2023-10-26 17:33:44 -03:00
script = {}
script[" name" ] = " Output"
script[" action" ] = " run"
script[" nodes" ] = args.data[0]
script[" commands" ] = [command]
script[" output" ] = " stdout"
self._cli_run(script)
2022-06-10 13:24:26 -03:00
def _yaml_generate(self, args):
if os.path.exists(args.data[0]):
print(" File {} already exists" .format(args.data[0]))
exit(14)
2022-05-11 14:25:43 -03:00
else:
2022-06-10 13:24:26 -03:00
with open(args.data[0], " w" ) as file:
file.write(self._help(" generate" ))
file.close()
print(" File {} generated succesfully" .format(args.data[0]))
exit()
def _yaml_run(self, args):
try:
with open(args.data[0]) as file:
scripts = yaml.load(file, Loader=yaml.FullLoader)
except:
print(" failed reading file {}" .format(args.data[0]))
exit(10)
for script in scripts[" tasks" ]:
2023-07-11 19:33:21 -03:00
self._cli_run(script)
def _cli_run(self, script):
args = {}
try:
action = script[" action" ]
nodelist = script[" nodes" ]
args[" commands" ] = script[" commands" ]
output = script[" output" ]
if action == " test" :
args[" expected" ] = script[" expected" ]
except KeyError as e:
print(" ' {}' is mandatory" .format(e.args[0]))
exit(11)
2023-10-26 17:33:44 -03:00
nodes = self.config._getallnodes(nodelist)
if len(nodes) == 0:
print(" {} don' t match any node" .format(nodelist))
exit(2)
nodes = self.connnodes(self.config.getitems(nodes), config = self.config)
2023-07-11 19:33:21 -03:00
stdout = False
if output is None:
pass
elif output == " stdout" :
stdout = True
elif isinstance(output, str) and action == " run" :
args[" folder" ] = output
if " variables" in script:
args[" vars" ] = script[" variables" ]
if " vars" in script:
args[" vars" ] = script[" vars" ]
try:
options = script[" options" ]
thisoptions = {k: v for k, v in options.items() if k in [" prompt" , " parallel" , " timeout" ]}
args.update(thisoptions)
except:
options = None
2023-10-26 17:33:44 -03:00
try:
size = str(os.get_terminal_size())
p = re.search(r' .*columns=([0-9]+)' , size)
columns = int(p.group(1))
except:
columns = 80
2023-07-11 19:33:21 -03:00
if action == " run" :
nodes.run(**args)
print(script[" name" ].upper() + " -" * (columns - len(script[" name" ])))
for i in nodes.status.keys():
print(" " + i + " " + " -" * (columns - len(i) - 13) + (" PASS(0)" if nodes.status[i] == 0 else " FAIL({})" .format(nodes.status[i])))
if stdout:
for line in nodes.output[i].splitlines():
print(" " + line)
elif action == " test" :
nodes.test(**args)
print(script[" name" ].upper() + " -" * (columns - len(script[" name" ])))
for i in nodes.status.keys():
print(" " + i + " " + " -" * (columns - len(i) - 13) + (" PASS(0)" if nodes.status[i] == 0 else " FAIL({})" .format(nodes.status[i])))
if nodes.status[i] == 0:
max_length = max(len(s) for s in nodes.result[i].keys())
for k,v in nodes.result[i].items():
print(" TEST for ' {}' " .format(k) + " " *(max_length - len(k) + 1) + " --> " + str(v).upper())
if stdout:
2022-06-10 13:24:26 -03:00
if nodes.status[i] == 0:
2023-07-11 19:33:21 -03:00
print(" " + " -" * (max_length + 21))
for line in nodes.output[i].splitlines():
print(" " + line)
else:
print(" Wrong action ' {}' " .format(action))
exit(13)
2022-05-11 14:25:43 -03:00
2022-04-03 18:25:58 -03:00
def _choose(self, list, name, action):
#Generates an inquirer list to pick
2022-04-18 19:19:25 -03:00
if FzfPrompt and self.fzf:
fzf = FzfPrompt(executable_path=" fzf-tmux" )
2023-03-21 18:23:29 -03:00
if not self.case:
fzf = FzfPrompt(executable_path=" fzf-tmux -i" )
2022-04-18 19:19:25 -03:00
answer = fzf.prompt(list, fzf_options=" -d 25%" )
if len(answer) == 0:
return
else:
return answer[0]
2022-04-03 18:25:58 -03:00
else:
2022-04-18 19:19:25 -03:00
questions = [inquirer.List(name, message=" Pick {} to {}:" .format(name,action), choices=list, carousel=True)]
answer = inquirer.prompt(questions)
if answer == None:
return
else:
return answer[name]
2022-04-03 18:25:58 -03:00
def _host_validation(self, answers, current, regex = " ^.+$" ):
#Validate hostname in inquirer when managing nodes
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Host cannot be empty" )
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
return True
def _profile_protocol_validation(self, answers, current, regex = " (^ssh$|^telnet$|^$)" ):
#Validate protocol in inquirer when managing profiles
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Pick between ssh, telnet or leave empty" )
return True
def _protocol_validation(self, answers, current, regex = " (^ssh$|^telnet$|^$|^@.+$)" ):
#Validate protocol in inquirer when managing nodes
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Pick between ssh, telnet, leave empty or @profile" )
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
return True
def _profile_port_validation(self, answers, current, regex = " (^[0-9]*$)" ):
#Validate port in inquirer when managing profiles
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Pick a port between 1-65535, @profile o leave empty" )
try:
port = int(current)
except:
port = 0
if current != " " and not 1 < = int(port) < = 65535:
raise inquirer.errors.ValidationError(" " , reason=" Pick a port between 1-65535 or leave empty" )
return True
def _port_validation(self, answers, current, regex = " (^[0-9]*$|^@.+$)" ):
#Validate port in inquirer when managing nodes
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Pick a port between 1-65535, @profile or leave empty" )
try:
port = int(current)
except:
port = 0
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
elif current != " " and not 1 < = int(port) < = 65535:
raise inquirer.errors.ValidationError(" " , reason=" Pick a port between 1-65535, @profile o leave empty" )
return True
def _pass_validation(self, answers, current, regex = " (^@.+$)" ):
#Validate password in inquirer
profiles = current.split(" ," )
for i in profiles:
if not re.match(regex, i) or i[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(i))
return True
2023-05-05 13:41:32 -03:00
def _tags_validation(self, answers, current):
#Validation for Tags in inquirer when managing nodes
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
elif current != " " :
isdict = False
try:
isdict = ast.literal_eval(current)
except:
pass
if not isinstance (isdict, dict):
raise inquirer.errors.ValidationError(" " , reason=" Tags should be a python dictionary." .format(current))
return True
def _profile_tags_validation(self, answers, current):
#Validation for Tags in inquirer when managing profiles
if current != " " :
isdict = False
try:
isdict = ast.literal_eval(current)
except:
pass
if not isinstance (isdict, dict):
raise inquirer.errors.ValidationError(" " , reason=" Tags should be a python dictionary." .format(current))
return True
2023-12-06 10:53:42 -03:00
def _jumphost_validation(self, answers, current):
#Validation for Jumphost in inquirer when managing nodes
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
elif current != " " :
if current not in self.nodes :
raise inquirer.errors.ValidationError(" " , reason=" Node {} don' t exist." .format(current))
return True
def _profile_jumphost_validation(self, answers, current):
#Validation for Jumphost in inquirer when managing profiles
if current != " " :
if current not in self.nodes :
raise inquirer.errors.ValidationError(" " , reason=" Node {} don' t exist." .format(current))
return True
2022-04-03 18:25:58 -03:00
def _default_validation(self, answers, current):
#Default validation type used in multiples questions in inquirer
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
return True
def _bulk_node_validation(self, answers, current, regex = " ^[0-9a-zA-Z_.,$#-]+$" ):
#Validation of nodes when running bulk command
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Host cannot be empty" )
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
return True
def _bulk_folder_validation(self, answers, current):
#Validation of folders when running bulk command
if not self.case:
current = current.lower()
matches = list(filter(lambda k: k == current, self.folders))
if current != " " and len(matches) == 0:
raise inquirer.errors.ValidationError(" " , reason=" Location {} don' t exist" .format(current))
return True
def _bulk_host_validation(self, answers, current, regex = " ^.+$" ):
#Validate hostname when running bulk command
if not re.match(regex, current):
raise inquirer.errors.ValidationError(" " , reason=" Host cannot be empty" )
if current.startswith(" @" ):
if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError(" " , reason=" Profile {} don' t exist" .format(current))
hosts = current.split(" ," )
nodes = answers[" ids" ].split(" ," )
if len(hosts) > 1 and len(hosts) != len(nodes):
raise inquirer.errors.ValidationError(" " , reason=" Hosts list should be the same length of nodes list" )
return True
def _questions_edit(self):
#Inquirer questions when editing nodes or profiles
questions = []
questions.append(inquirer.Confirm(" host" , message=" Edit Hostname/IP?" ))
questions.append(inquirer.Confirm(" protocol" , message=" Edit Protocol?" ))
questions.append(inquirer.Confirm(" port" , message=" Edit Port?" ))
questions.append(inquirer.Confirm(" options" , message=" Edit Options?" ))
questions.append(inquirer.Confirm(" logs" , message=" Edit logging path/file?" ))
2023-05-05 13:41:32 -03:00
questions.append(inquirer.Confirm(" tags" , message=" Edit tags?" ))
2023-12-06 10:53:42 -03:00
questions.append(inquirer.Confirm(" jumphost" , message=" Edit jumphost?" ))
2022-04-03 18:25:58 -03:00
questions.append(inquirer.Confirm(" user" , message=" Edit User?" ))
questions.append(inquirer.Confirm(" password" , message=" Edit password?" ))
answers = inquirer.prompt(questions)
return answers
def _questions_nodes(self, unique, uniques = None, edit = None):
#Questions when adding or editing nodes
try:
defaults = self.config.getitem(unique)
2023-05-05 13:41:32 -03:00
if " tags" not in defaults:
defaults[" tags" ] = " "
2023-12-06 10:53:42 -03:00
if " jumphost" not in defaults:
defaults[" jumphost" ] = " "
2022-04-03 18:25:58 -03:00
except:
2023-12-06 10:53:42 -03:00
defaults = { " host" :" " , " protocol" :" " , " port" :" " , " user" :" " , " options" :" " , " logs" :" " , " tags" :" " , " password" :" " , " jumphost" :" " }
2022-04-03 18:25:58 -03:00
node = {}
if edit == None:
2023-12-06 10:53:42 -03:00
edit = { " host" :True, " protocol" :True, " port" :True, " user" :True, " password" : True," options" :True, " logs" :True, " tags" :True, " jumphost" :True }
2022-04-03 18:25:58 -03:00
questions = []
if edit[" host" ]:
questions.append(inquirer.Text(" host" , message=" Add Hostname or IP" , validate=self._host_validation, default=defaults[" host" ]))
else:
node[" host" ] = defaults[" host" ]
if edit[" protocol" ]:
questions.append(inquirer.Text(" protocol" , message=" Select Protocol" , validate=self._protocol_validation, default=defaults[" protocol" ]))
else:
node[" protocol" ] = defaults[" protocol" ]
if edit[" port" ]:
questions.append(inquirer.Text(" port" , message=" Select Port Number" , validate=self._port_validation, default=defaults[" port" ]))
else:
node[" port" ] = defaults[" port" ]
if edit[" options" ]:
questions.append(inquirer.Text(" options" , message=" Pass extra options to protocol" , validate=self._default_validation, default=defaults[" options" ]))
else:
node[" options" ] = defaults[" options" ]
if edit[" logs" ]:
2023-04-14 18:30:58 -03:00
questions.append(inquirer.Text(" logs" , message=" Pick logging path/file " , validate=self._default_validation, default=defaults[" logs" ].replace(" {" ," {{" ).replace(" }" ," }}" )))
2022-04-03 18:25:58 -03:00
else:
node[" logs" ] = defaults[" logs" ]
2023-05-05 13:41:32 -03:00
if edit[" tags" ]:
questions.append(inquirer.Text(" tags" , message=" Add tags dictionary" , validate=self._tags_validation, default=str(defaults[" tags" ]).replace(" {" ," {{" ).replace(" }" ," }}" )))
else:
node[" tags" ] = defaults[" tags" ]
2023-12-06 10:53:42 -03:00
if edit[" jumphost" ]:
questions.append(inquirer.Text(" jumphost" , message=" Add Jumphost node" , validate=self._jumphost_validation, default=str(defaults[" jumphost" ]).replace(" {" ," {{" ).replace(" }" ," }}" )))
else:
node[" jumphost" ] = defaults[" jumphost" ]
2022-04-03 18:25:58 -03:00
if edit[" user" ]:
questions.append(inquirer.Text(" user" , message=" Pick username" , validate=self._default_validation, default=defaults[" user" ]))
else:
node[" user" ] = defaults[" user" ]
if edit[" password" ]:
questions.append(inquirer.List(" password" , message=" Password: Use a local password, no password or a list of profiles to reference?" , choices=[" Local Password" , " Profiles" , " No Password" ]))
else:
node[" password" ] = defaults[" password" ]
answer = inquirer.prompt(questions)
if answer == None:
return False
if " password" in answer.keys():
if answer[" password" ] == " Local Password" :
passq = [inquirer.Password(" password" , message=" Set Password" )]
passa = inquirer.prompt(passq)
if passa == None:
return False
answer[" password" ] = self.encrypt(passa[" password" ])
elif answer[" password" ] == " Profiles" :
passq = [(inquirer.Text(" password" , message=" Set a @profile or a comma separated list of @profiles" , validate=self._pass_validation))]
passa = inquirer.prompt(passq)
if passa == None:
return False
answer[" password" ] = passa[" password" ].split(" ," )
elif answer[" password" ] == " No Password" :
answer[" password" ] = " "
2023-05-05 13:41:32 -03:00
if " tags" in answer.keys() and not answer[" tags" ].startswith(" @" ) and answer[" tags" ]:
answer[" tags" ] = ast.literal_eval(answer[" tags" ])
2022-04-03 18:25:58 -03:00
result = {**uniques, **answer, **node}
result[" type" ] = " connection"
return result
def _questions_profiles(self, unique, edit = None):
#Questions when adding or editing profiles
try:
defaults = self.config.profiles[unique]
2023-05-05 13:41:32 -03:00
if " tags" not in defaults:
defaults[" tags" ] = " "
2023-12-06 10:53:42 -03:00
if " jumphost" not in defaults:
defaults[" jumphost" ] = " "
2022-04-03 18:25:58 -03:00
except:
2023-12-06 10:53:42 -03:00
defaults = { " host" :" " , " protocol" :" " , " port" :" " , " user" :" " , " options" :" " , " logs" :" " , " tags" : " " , " jumphost" : " " }
2022-04-03 18:25:58 -03:00
profile = {}
if edit == None:
2023-12-06 10:53:42 -03:00
edit = { " host" :True, " protocol" :True, " port" :True, " user" :True, " password" : True," options" :True, " logs" :True, " tags" :True, " jumphost" :True }
2022-04-03 18:25:58 -03:00
questions = []
if edit[" host" ]:
questions.append(inquirer.Text(" host" , message=" Add Hostname or IP" , default=defaults[" host" ]))
else:
profile[" host" ] = defaults[" host" ]
if edit[" protocol" ]:
questions.append(inquirer.Text(" protocol" , message=" Select Protocol" , validate=self._profile_protocol_validation, default=defaults[" protocol" ]))
else:
profile[" protocol" ] = defaults[" protocol" ]
if edit[" port" ]:
questions.append(inquirer.Text(" port" , message=" Select Port Number" , validate=self._profile_port_validation, default=defaults[" port" ]))
else:
profile[" port" ] = defaults[" port" ]
if edit[" options" ]:
questions.append(inquirer.Text(" options" , message=" Pass extra options to protocol" , default=defaults[" options" ]))
else:
profile[" options" ] = defaults[" options" ]
if edit[" logs" ]:
2023-04-14 18:30:58 -03:00
questions.append(inquirer.Text(" logs" , message=" Pick logging path/file " , default=defaults[" logs" ].replace(" {" ," {{" ).replace(" }" ," }}" )))
2022-04-03 18:25:58 -03:00
else:
profile[" logs" ] = defaults[" logs" ]
2023-05-05 13:41:32 -03:00
if edit[" tags" ]:
questions.append(inquirer.Text(" tags" , message=" Add tags dictionary" , validate=self._profile_tags_validation, default=str(defaults[" tags" ]).replace(" {" ," {{" ).replace(" }" ," }}" )))
else:
profile[" tags" ] = defaults[" tags" ]
2023-12-06 10:53:42 -03:00
if edit[" jumphost" ]:
questions.append(inquirer.Text(" jumphost" , message=" Add Jumphost node" , validate=self._profile_jumphost_validation, default=str(defaults[" jumphost" ]).replace(" {" ," {{" ).replace(" }" ," }}" )))
else:
profile[" jumphost" ] = defaults[" jumphost" ]
2022-04-03 18:25:58 -03:00
if edit[" user" ]:
questions.append(inquirer.Text(" user" , message=" Pick username" , default=defaults[" user" ]))
else:
profile[" user" ] = defaults[" user" ]
if edit[" password" ]:
questions.append(inquirer.Password(" password" , message=" Set Password" ))
else:
profile[" password" ] = defaults[" password" ]
answer = inquirer.prompt(questions)
if answer == None:
return False
if " password" in answer.keys():
if answer[" password" ] != " " :
answer[" password" ] = self.encrypt(answer[" password" ])
2023-05-05 13:41:32 -03:00
if " tags" in answer.keys() and answer[" tags" ]:
answer[" tags" ] = ast.literal_eval(answer[" tags" ])
2022-04-03 18:25:58 -03:00
result = {**answer, **profile}
result[" id" ] = unique
return result
def _questions_bulk(self):
#Questions when using bulk command
questions = []
questions.append(inquirer.Text(" ids" , message=" add a comma separated list of nodes to add" , validate=self._bulk_node_validation))
questions.append(inquirer.Text(" location" , message=" Add a @folder, @subfolder@folder or leave empty" , validate=self._bulk_folder_validation))
questions.append(inquirer.Text(" host" , message=" Add comma separated list of Hostnames or IPs" , validate=self._bulk_host_validation))
questions.append(inquirer.Text(" protocol" , message=" Select Protocol" , validate=self._protocol_validation))
questions.append(inquirer.Text(" port" , message=" Select Port Number" , validate=self._port_validation))
questions.append(inquirer.Text(" options" , message=" Pass extra options to protocol" , validate=self._default_validation))
questions.append(inquirer.Text(" logs" , message=" Pick logging path/file " , validate=self._default_validation))
2023-05-05 13:41:32 -03:00
questions.append(inquirer.Text(" tags" , message=" Add tags dictionary" , validate=self._tags_validation))
2023-12-06 10:53:42 -03:00
questions.append(inquirer.Text(" jumphost" , message=" Add Jumphost node" , validate=self._jumphost_validation))
2022-04-03 18:25:58 -03:00
questions.append(inquirer.Text(" user" , message=" Pick username" , validate=self._default_validation))
questions.append(inquirer.List(" password" , message=" Password: Use a local password, no password or a list of profiles to reference?" , choices=[" Local Password" , " Profiles" , " No Password" ]))
answer = inquirer.prompt(questions)
if answer == None:
return False
if " password" in answer.keys():
if answer[" password" ] == " Local Password" :
passq = [inquirer.Password(" password" , message=" Set Password" )]
passa = inquirer.prompt(passq)
answer[" password" ] = self.encrypt(passa[" password" ])
elif answer[" password" ] == " Profiles" :
passq = [(inquirer.Text(" password" , message=" Set a @profile or a comma separated list of @profiles" , validate=self._pass_validation))]
passa = inquirer.prompt(passq)
answer[" password" ] = passa[" password" ].split(" ," )
elif answer[" password" ] == " No Password" :
answer[" password" ] = " "
answer[" type" ] = " connection"
2023-05-05 13:41:32 -03:00
if " tags" in answer.keys() and not answer[" tags" ].startswith(" @" ) and answer[" tags" ]:
answer[" tags" ] = ast.literal_eval(answer[" tags" ])
2022-04-03 18:25:58 -03:00
return answer
def _type_node(self, arg_value, pat=re.compile(r" ^[0-9a-zA-Z_.$@#-]+$" )):
2023-12-06 10:53:42 -03:00
if arg_value == None:
raise ValueError(" Missing argument node" )
2022-04-03 18:25:58 -03:00
if not pat.match(arg_value):
2023-10-26 17:33:44 -03:00
raise ValueError(f" Argument error: {arg_value}" )
2022-04-03 18:25:58 -03:00
return arg_value
def _type_profile(self, arg_value, pat=re.compile(r" ^[0-9a-zA-Z_.$#-]+$" )):
if not pat.match(arg_value):
2023-10-26 17:33:44 -03:00
raise ValueError
2022-04-03 18:25:58 -03:00
return arg_value
2023-12-15 12:27:51 -03:00
def _help(self, type, parsers = None):
2022-04-03 18:25:58 -03:00
#Store text for help and other commands
if type == " node" :
2023-12-01 15:31:17 -03:00
return " node[@subfolder][@folder]\nConnect to specific node or show all matching nodes\n[@subfolder][@folder]\nShow all available connections globally or in specified path"
2022-04-03 18:25:58 -03:00
if type == " usage" :
2023-12-15 12:27:51 -03:00
commands = []
for subcommand, subparser in parsers.choices.items():
if subparser.description != None:
commands.append(subcommand)
commands = " ," .join(commands)
usage_help = f" conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]\n conn {{{commands}}} ..."
return usage_help
2022-04-03 18:25:58 -03:00
if type == " end" :
2023-12-15 12:27:51 -03:00
help_dict = {}
for subcommand, subparser in parsers.choices.items():
if subparser.description == None and help_dict:
previous_key = next(reversed(help_dict.keys()))
help_dict[f" {previous_key}({subcommand})" ] = help_dict.pop(previous_key)
else:
help_dict[subcommand] = subparser.description
subparser.description = None
commands_help = " Commands:\n"
commands_help += " \n" .join([f" {cmd:< 15} {help_text}" for cmd, help_text in help_dict.items() if help_text != None])
return commands_help
2022-04-04 19:09:00 -03:00
if type == " bashcompletion" :
2022-04-03 18:25:58 -03:00
return ' ' '
#Here starts bash completion for conn
_conn()
{
2023-07-11 19:33:21 -03:00
mapfile -t strings < < (connpy-completion-helper " bash" " ${#COMP_WORDS[@]}" " ${COMP_WORDS[@]}" )
2023-10-05 15:21:17 -03:00
local IFS=$' \t\n'
local home_dir=$(eval echo ~)
local last_word=${COMP_WORDS[-1]/\~/$home_dir}
COMPREPLY=($(compgen -W " $(printf ' %s' " ${strings[@]}" )" -- " $last_word" ))
if [ " $last_word" != " ${COMP_WORDS[-1]}" ]; then
COMPREPLY=(${COMPREPLY[@]/$home_dir/\~})
fi
2022-04-03 18:25:58 -03:00
}
2023-07-11 19:33:21 -03:00
complete -o nospace -o nosort -F _conn conn
complete -o nospace -o nosort -F _conn connpy
2022-04-18 19:19:25 -03:00
#Here ends bash completion for conn
2022-04-03 18:25:58 -03:00
' ' '
2022-04-04 19:09:00 -03:00
if type == " zshcompletion" :
return ' ' '
2022-04-18 19:19:25 -03:00
#Here starts zsh completion for conn
2022-04-04 19:09:00 -03:00
autoload -U compinit & & compinit
_conn()
{
2023-10-05 15:21:17 -03:00
local home_dir=$(eval echo ~)
last_word=${words[-1]/\~/$home_dir}
strings=($(connpy-completion-helper " zsh" ${#words} $words[1,-2] $last_word))
2023-07-11 19:33:21 -03:00
for string in " ${strings[@]}" ; do
2023-10-05 15:21:17 -03:00
#Replace the expanded home directory with ~
if [ " $last_word" != " $words[-1]" ]; then
string=${string/$home_dir/\~}
fi
2023-07-11 19:33:21 -03:00
if [[ " ${string}" =~ .*/$ ]]; then
# If the string ends with a ' /' , do not append a space
2023-10-05 15:21:17 -03:00
compadd -Q -S ' ' -- " $string"
2023-07-11 19:33:21 -03:00
else
# If the string does not end with a ' /' , append a space
2023-10-05 15:21:17 -03:00
compadd -Q -S ' ' -- " $string"
2023-07-11 19:33:21 -03:00
fi
done
2022-04-04 19:09:00 -03:00
}
compdef _conn conn
compdef _conn connpy
2022-04-18 19:19:25 -03:00
#Here ends zsh completion for conn
2022-04-04 19:09:00 -03:00
' ' '
2022-05-11 14:25:43 -03:00
if type == " run" :
return " node[@subfolder][@folder] commmand to run\nRun the specific command on the node and print output\n/path/to/file.yaml\nUse a yaml file to run an automation script"
if type == " generate" :
return ' ' ' ---
tasks:
- name: " Config"
action: ' run' #Action can be test or run. Mandatory
nodes: #List of nodes to work on. Mandatory
- ' router1@office' #You can add specific nodes
- ' @aws' #entire folders or subfolders
2022-05-26 11:33:35 -03:00
- ' @office' : #or filter inside a folder or subfolder
2022-05-11 14:25:43 -03:00
- ' router2'
- ' router7'
commands: #List of commands to send, use {name} to pass variables
- ' term len 0'
- ' conf t'
- ' interface {if}'
- ' ip address 10.100.100.{id} 255.255.255.255'
- ' {commit}'
- ' end'
variables: #Variables to use on commands and expected. Optional
__global__: #Global variables to use on all nodes, fallback if missing in the node.
commit: ' '
if: ' loopback100'
router1@office:
id: 1
router2@office:
id: 2
commit: ' commit'
router3@office:
id: 3
vrouter1@aws:
id: 4
vrouterN@aws:
id: 5
output: /home/user/logs #Type of output, if null you only get Connection and test result. Choices are: null,stdout,/path/to/folder. Folder path only works on ' run' action.
options:
prompt: r' > $|#$|\$$|> .$|#.$|\$.$' #Optional prompt to check on your devices, default should work on most devices.
parallel: 10 #Optional number of nodes to run commands on parallel. Default 10.
timeout: 20 #Optional time to wait in seconds for prompt, expected or EOF. Default 20.
- name: " TestConfig"
action: ' test'
nodes:
- ' router1@office'
- ' @aws'
- ' @office' :
- ' router2'
- ' router7'
commands:
- ' ping 10.100.100.{id}'
expected: ' !' #Expected text to find when running test action. Mandatory for ' test'
variables:
router1@office:
id: 1
router2@office:
id: 2
commit: ' commit'
router3@office:
id: 3
vrouter1@aws:
id: 4
vrouterN@aws:
id: 5
output: null
...' ' '
2022-04-03 18:25:58 -03:00
def encrypt(self, password, keyfile=None):
' ' '
Encrypts password using RSA keyfile
### Parameters:
- password (str): Plaintext password to encrypt.
### Optional Parameters:
- keyfile (str): Path/file to keyfile. Default is config keyfile.
### Returns:
str: Encrypted password.
' ' '
if keyfile is None:
keyfile = self.config.key
2022-06-10 13:24:26 -03:00
with open(keyfile) as f:
key = RSA.import_key(f.read())
f.close()
2022-04-03 18:25:58 -03:00
publickey = key.publickey()
encryptor = PKCS1_OAEP.new(publickey)
password = encryptor.encrypt(password.encode(" utf-8" ))
return str(password)< / code > < / pre >
2022-04-03 09:38:00 -03:00
< / details >
2022-04-03 18:25:58 -03:00
< h3 > Methods< / h3 >
< dl >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.connapp.encrypt" > < code class = "name flex" >
2022-04-03 18:25:58 -03:00
< span > def < span class = "ident" > encrypt< / span > < / span > (< span > self, password, keyfile=None)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
2022-04-03 18:25:58 -03:00
< div class = "desc" > < p > Encrypts password using RSA keyfile< / p >
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - password (str): Plaintext password to encrypt.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - keyfile (str): Path/file to keyfile. Default is config keyfile.
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > str: Encrypted password.
< / code > < / pre > < / div >
2022-04-03 09:38:00 -03:00
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2022-04-03 18:25:58 -03:00
< pre > < code class = "python" > def encrypt(self, password, keyfile=None):
' ' '
Encrypts password using RSA keyfile
### Parameters:
- password (str): Plaintext password to encrypt.
### Optional Parameters:
- keyfile (str): Path/file to keyfile. Default is config keyfile.
### Returns:
str: Encrypted password.
' ' '
if keyfile is None:
keyfile = self.config.key
2022-06-10 13:24:26 -03:00
with open(keyfile) as f:
key = RSA.import_key(f.read())
f.close()
2022-04-03 18:25:58 -03:00
publickey = key.publickey()
encryptor = PKCS1_OAEP.new(publickey)
password = encryptor.encrypt(password.encode(" utf-8" ))
return str(password)< / code > < / pre >
2022-04-03 09:38:00 -03:00
< / details >
< / dd >
2022-05-19 16:11:41 -03:00
< dt id = "connpy.connapp.start" > < code class = "name flex" >
2023-12-15 12:27:51 -03:00
< span > def < span class = "ident" > start< / span > < / span > (< span > self, argv=['--html', 'connpy', '-o', 'docs', '--force'])< / span >
2022-05-19 16:11:41 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - argv (list): List of arguments to pass to the app.
Default: sys.argv[1:]
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > def start(self,argv = sys.argv[1:]):
' ' '
### Parameters:
- argv (list): List of arguments to pass to the app.
Default: sys.argv[1:]
' ' '
#DEFAULTPARSER
defaultparser = argparse.ArgumentParser(prog = " conn" , description = " SSH and Telnet connection manager" , formatter_class=argparse.RawTextHelpFormatter)
2023-12-15 12:27:51 -03:00
subparsers = defaultparser.add_subparsers(title=" Commands" , dest=" subcommand" )
2022-05-19 16:11:41 -03:00
#NODEPARSER
2023-12-15 12:27:51 -03:00
nodeparser = subparsers.add_parser(" node" , formatter_class=argparse.RawTextHelpFormatter)
2022-05-19 16:11:41 -03:00
nodecrud = nodeparser.add_mutually_exclusive_group()
2023-10-26 17:33:44 -03:00
nodeparser.add_argument(" node" , metavar=" node|folder" , nargs=' ?' , default=None, action=self._store_type, help=self._help(" node" ))
2022-05-19 16:11:41 -03:00
nodecrud.add_argument(" -v" ," --version" , dest=" action" , action=" store_const" , help=" Show version" , const=" version" , default=" connect" )
nodecrud.add_argument(" -a" ," --add" , dest=" action" , action=" store_const" , help=" Add new node[@subfolder][@folder] or [@subfolder]@folder" , const=" add" , default=" connect" )
nodecrud.add_argument(" -r" ," --del" , " --rm" , dest=" action" , action=" store_const" , help=" Delete node[@subfolder][@folder] or [@subfolder]@folder" , const=" del" , default=" connect" )
nodecrud.add_argument(" -e" ," --mod" , " --edit" , dest=" action" , action=" store_const" , help=" Modify node[@subfolder][@folder]" , const=" mod" , default=" connect" )
nodecrud.add_argument(" -s" ," --show" , dest=" action" , action=" store_const" , help=" Show node[@subfolder][@folder]" , const=" show" , default=" connect" )
2023-11-03 11:59:00 -03:00
nodecrud.add_argument(" -d" ," --debug" , dest=" debug" , action=" store_true" , help=" Display all conections steps" )
nodeparser.add_argument(" -t" ," --sftp" , dest=" sftp" , action=" store_true" , help=" Connects using sftp instead of ssh" )
2022-05-19 16:11:41 -03:00
nodeparser.set_defaults(func=self._func_node)
#PROFILEPARSER
2023-12-15 12:27:51 -03:00
profileparser = subparsers.add_parser(" profile" , description=" Manage profiles" )
2022-05-19 16:11:41 -03:00
profileparser.add_argument(" profile" , nargs=1, action=self._store_type, type=self._type_profile, help=" Name of profile to manage" )
profilecrud = profileparser.add_mutually_exclusive_group(required=True)
profilecrud.add_argument(" -a" , " --add" , dest=" action" , action=" store_const" , help=" Add new profile" , const=" add" )
profilecrud.add_argument(" -r" , " --del" , " --rm" , dest=" action" , action=" store_const" , help=" Delete profile" , const=" del" )
profilecrud.add_argument(" -e" , " --mod" , " --edit" , dest=" action" , action=" store_const" , help=" Modify profile" , const=" mod" )
profilecrud.add_argument(" -s" , " --show" , dest=" action" , action=" store_const" , help=" Show profile" , const=" show" )
profileparser.set_defaults(func=self._func_profile)
#MOVEPARSER
2023-12-15 12:27:51 -03:00
moveparser = subparsers.add_parser(" move" , aliases=[" mv" ], description=" Move node" )
2022-05-19 16:11:41 -03:00
moveparser.add_argument(" move" , nargs=2, action=self._store_type, help=" Move node[@subfolder][@folder] dest_node[@subfolder][@folder]" , default=" move" , type=self._type_node)
moveparser.set_defaults(func=self._func_others)
#COPYPARSER
2023-12-15 12:27:51 -03:00
copyparser = subparsers.add_parser(" copy" , aliases=[" cp" ], description=" Copy node" )
2022-05-19 16:11:41 -03:00
copyparser.add_argument(" cp" , nargs=2, action=self._store_type, help=" Copy node[@subfolder][@folder] new_node[@subfolder][@folder]" , default=" cp" , type=self._type_node)
copyparser.set_defaults(func=self._func_others)
#LISTPARSER
2023-12-15 12:27:51 -03:00
lsparser = subparsers.add_parser(" list" , aliases=[" ls" ], description=" List profiles, nodes or folders" )
2022-05-19 16:11:41 -03:00
lsparser.add_argument(" ls" , action=self._store_type, choices=[" profiles" ," nodes" ," folders" ], help=" List profiles, nodes or folders" , default=False)
2023-10-26 17:33:44 -03:00
lsparser.add_argument(" --filter" , nargs=1, help=" Filter results" )
lsparser.add_argument(" --format" , nargs=1, help=" Format of the output of nodes using {name}, {NAME}, {location}, {LOCATION}, {host} and {HOST}" )
2022-05-19 16:11:41 -03:00
lsparser.set_defaults(func=self._func_others)
#BULKPARSER
2023-12-15 12:27:51 -03:00
bulkparser = subparsers.add_parser(" bulk" , description=" Add nodes in bulk" )
2022-05-19 16:11:41 -03:00
bulkparser.add_argument(" bulk" , const=" bulk" , nargs=0, action=self._store_type, help=" Add nodes in bulk" )
bulkparser.set_defaults(func=self._func_others)
2023-09-12 12:33:33 -03:00
# EXPORTPARSER
2023-12-15 12:27:51 -03:00
exportparser = subparsers.add_parser(" export" , description=" Export connection folder to Yaml file" )
2023-09-21 17:28:09 -03:00
exportparser.add_argument(" export" , nargs=" +" , action=self._store_type, help=" Export /path/to/file.yml [@subfolder1][@folder1] [@subfolderN][@folderN]" )
2023-09-12 12:33:33 -03:00
exportparser.set_defaults(func=self._func_export)
# IMPORTPARSER
2023-12-15 12:27:51 -03:00
importparser = subparsers.add_parser(" import" , description=" Import connection folder to config from Yaml file" )
2023-09-13 11:17:56 -03:00
importparser.add_argument(" file" , nargs=1, action=self._store_type, help=" Import /path/to/file.yml" )
2023-09-12 12:33:33 -03:00
importparser.set_defaults(func=self._func_import)
2023-07-11 19:33:21 -03:00
# AIPARSER
2023-12-15 12:27:51 -03:00
aiparser = subparsers.add_parser(" ai" , description=" Make request to an AI" )
2023-07-11 19:33:21 -03:00
aiparser.add_argument(" ask" , nargs=' *' , help=" Ask connpy AI something" )
aiparser.add_argument(" --model" , nargs=1, help=" Set the OPENAI model id" )
aiparser.add_argument(" --org" , nargs=1, help=" Set the OPENAI organization id" )
aiparser.add_argument(" --api_key" , nargs=1, help=" Set the OPENAI API key" )
aiparser.set_defaults(func=self._func_ai)
2022-05-19 16:11:41 -03:00
#RUNPARSER
2023-12-15 12:27:51 -03:00
runparser = subparsers.add_parser(" run" , description=" Run scripts or commands on nodes" , formatter_class=argparse.RawTextHelpFormatter)
2023-01-05 16:39:22 -03:00
runparser.add_argument(" run" , nargs=' +' , action=self._store_type, help=self._help(" run" ), default=" run" )
2022-05-19 16:11:41 -03:00
runparser.add_argument(" -g" ," --generate" , dest=" action" , action=" store_const" , help=" Generate yaml file template" , const=" generate" , default=" run" )
runparser.set_defaults(func=self._func_run)
2023-04-14 11:44:56 -03:00
#APIPARSER
2023-12-15 12:27:51 -03:00
apiparser = subparsers.add_parser(" api" , description=" Start and stop connpy api" )
2023-04-14 11:44:56 -03:00
apicrud = apiparser.add_mutually_exclusive_group(required=True)
2023-04-15 22:38:52 -03:00
apicrud.add_argument(" -s" ," --start" , dest=" start" , nargs=" ?" , action=self._store_type, help=" Start conppy api" , type=int, default=8048, metavar=" PORT" )
apicrud.add_argument(" -r" ," --restart" , dest=" restart" , nargs=0, action=self._store_type, help=" Restart conppy api" )
apicrud.add_argument(" -x" ," --stop" , dest=" stop" , nargs=0, action=self._store_type, help=" Stop conppy api" )
apicrud.add_argument(" -d" , " --debug" , dest=" debug" , nargs=" ?" , action=self._store_type, help=" Run connpy server on debug mode" , type=int, default=8048, metavar=" PORT" )
2023-04-14 11:44:56 -03:00
apiparser.set_defaults(func=self._func_api)
2023-12-15 12:27:51 -03:00
#PLUGINSPARSER
pluginparser = subparsers.add_parser(" plugin" , description=" Manage plugins" )
plugincrud = pluginparser.add_mutually_exclusive_group(required=True)
plugincrud.add_argument(" --add" , metavar=(" PLUGIN" , " FILE" ), nargs=2, help=" Add new plugin" )
plugincrud.add_argument(" --del" , dest=" delete" , metavar=" PLUGIN" , nargs=1, help=" Delete plugin" )
plugincrud.add_argument(" --enable" , metavar=" PLUGIN" , nargs=1, help=" Enable plugin" )
plugincrud.add_argument(" --disable" , metavar=" PLUGIN" , nargs=1, help=" Disable plugin" )
plugincrud.add_argument(" --list" , dest=" list" , action=" store_true" , help=" Disable plugin" )
pluginparser.set_defaults(func=self._func_plugin)
2022-05-19 16:11:41 -03:00
#CONFIGPARSER
2023-12-15 12:27:51 -03:00
configparser = subparsers.add_parser(" config" , description=" Manage app config" )
2022-05-19 16:11:41 -03:00
configcrud = configparser.add_mutually_exclusive_group(required=True)
configcrud.add_argument(" --allow-uppercase" , dest=" case" , nargs=1, action=self._store_type, help=" Allow case sensitive names" , choices=[" true" ," false" ])
configcrud.add_argument(" --fzf" , dest=" fzf" , nargs=1, action=self._store_type, help=" Use fzf for lists" , choices=[" true" ," false" ])
configcrud.add_argument(" --keepalive" , dest=" idletime" , nargs=1, action=self._store_type, help=" Set keepalive time in seconds, 0 to disable" , type=int, metavar=" INT" )
configcrud.add_argument(" --completion" , dest=" completion" , nargs=1, choices=[" bash" ," zsh" ], action=self._store_type, help=" Get terminal completion configuration for conn" )
2023-04-14 11:44:56 -03:00
configcrud.add_argument(" --configfolder" , dest=" configfolder" , nargs=1, action=self._store_type, help=" Set the default location for config file" , metavar=" FOLDER" )
2023-07-11 19:33:21 -03:00
configcrud.add_argument(" --openai-org" , dest=" organization" , nargs=1, action=self._store_type, help=" Set openai organization" , metavar=" ORGANIZATION" )
configcrud.add_argument(" --openai-api-key" , dest=" api_key" , nargs=1, action=self._store_type, help=" Set openai api_key" , metavar=" API_KEY" )
configcrud.add_argument(" --openai-model" , dest=" model" , nargs=1, action=self._store_type, help=" Set openai model" , metavar=" MODEL" )
2022-05-19 16:11:41 -03:00
configparser.set_defaults(func=self._func_others)
2023-12-15 12:27:51 -03:00
#Add plugins
file_path = self.config.defaultdir + " /plugins"
self.plugins = Plugins()
self.plugins._import_plugins_to_argparse(file_path, subparsers)
#Generate helps
nodeparser.usage = self._help(" usage" , subparsers)
nodeparser.epilog = self._help(" end" , subparsers)
nodeparser.help = self._help(" node" )
2022-05-19 16:11:41 -03:00
#Manage sys arguments
2023-12-15 12:27:51 -03:00
self.commands = list(subparsers.choices.keys())
profilecmds = []
for action in profileparser._actions:
profilecmds.extend(action.option_strings)
2022-05-19 16:11:41 -03:00
if len(argv) > = 2 and argv[1] == " profile" and argv[0] in profilecmds:
argv[1] = argv[0]
argv[0] = " profile"
2023-12-15 12:27:51 -03:00
if len(argv) < 1 or argv[0] not in self.commands:
2022-05-19 16:11:41 -03:00
argv.insert(0," node" )
args = defaultparser.parse_args(argv)
2023-12-15 12:27:51 -03:00
if args.subcommand in self.plugins.plugins:
self.plugins.plugins[args.subcommand].Entrypoint(args, self.plugins.plugin_parsers[args.subcommand].parser, self)
else:
return args.func(args)< / code > < / pre >
2022-05-19 16:11:41 -03:00
< / details >
< / dd >
2022-04-03 09:38:00 -03:00
< / dl >
< / dd >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.node" > < code class = "flex name class" >
2022-04-03 09:38:00 -03:00
< span > class < span class = "ident" > node< / span > < / span >
2023-12-06 10:53:42 -03:00
< span > (< / span > < span > unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags='', jumphost='')< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < p > This class generates a node object. Containts all the information and methods to connect and interact with a device using ssh or telnet.< / p >
2022-04-03 10:26:08 -03:00
< h3 id = "attributes" > Attributes:< / h3 >
< pre > < code > - output (str): Output of the commands you ran with run or test
method.
- result(bool): True if expected value is found after running
the commands using test method.
2022-04-28 12:49:31 -03:00
- status (int): 0 if the method run or test run succesfully.
1 if connection failed.
2 if expect timeouts without prompt or EOF.
2022-04-03 09:38:00 -03:00
< / code > < / pre >
2022-04-03 10:26:08 -03:00
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - unique (str): Unique name to assign to the node.
- host (str): IP address or hostname of the node.
2022-04-03 09:38:00 -03:00
< / code > < / pre >
2022-04-03 10:26:08 -03:00
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - options (str): Additional options to pass the ssh/telnet for
connection.
- logs (str): Path/file for storing the logs. You can use
${unique},${host}, ${port}, ${user}, ${protocol}
as variables.
- password (str): Encrypted or plaintext password.
- port (str): Port to connect to node, default 22 for ssh and 23
for telnet.
- protocol (str): Select ssh or telnet. Default is ssh.
- user (str): Username to of the node.
- config (obj): Pass the object created with class configfile with
key for decryption and extra configuration if you
are using connection manager.
2023-05-05 13:41:32 -03:00
- tags (dict) : Tags useful for automation and personal porpuse
like "os", "prompt" and "screenleght_command"
2023-12-06 10:53:42 -03:00
- jumphost (str): Reference another node to be used as a jumphost
2022-04-03 09:38:00 -03:00
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > class node:
' ' ' This class generates a node object. Containts all the information and methods to connect and interact with a device using ssh or telnet.
2022-04-03 10:26:08 -03:00
### Attributes:
- output (str): Output of the commands you ran with run or test
method.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- result(bool): True if expected value is found after running
the commands using test method.
2022-04-28 12:49:31 -03:00
- status (int): 0 if the method run or test run succesfully.
1 if connection failed.
2 if expect timeouts without prompt or EOF.
2022-04-03 09:38:00 -03:00
' ' '
2023-12-06 10:53:42 -03:00
def __init__(self, unique, host, options=' ' , logs=' ' , password=' ' , port=' ' , protocol=' ' , user=' ' , config=' ' , tags=' ' , jumphost=' ' ):
2022-04-03 09:38:00 -03:00
' ' '
2022-04-03 10:26:08 -03:00
### Parameters:
- unique (str): Unique name to assign to the node.
- host (str): IP address or hostname of the node.
### Optional Parameters:
- options (str): Additional options to pass the ssh/telnet for
connection.
- logs (str): Path/file for storing the logs. You can use
${unique},${host}, ${port}, ${user}, ${protocol}
as variables.
- password (str): Encrypted or plaintext password.
- port (str): Port to connect to node, default 22 for ssh and 23
for telnet.
- protocol (str): Select ssh or telnet. Default is ssh.
- user (str): Username to of the node.
- config (obj): Pass the object created with class configfile with
key for decryption and extra configuration if you
are using connection manager.
2023-05-05 13:41:32 -03:00
- tags (dict) : Tags useful for automation and personal porpuse
like " os" , " prompt" and " screenleght_command"
2023-12-06 10:53:42 -03:00
- jumphost (str): Reference another node to be used as a jumphost
2022-04-03 09:38:00 -03:00
' ' '
if config == ' ' :
self.idletime = 0
self.key = None
else:
self.idletime = config.config[" idletime" ]
self.key = config.key
self.unique = unique
2023-12-06 10:53:42 -03:00
attr = {" host" : host, " logs" : logs, " options" :options, " port" : port, " protocol" : protocol, " user" : user, " tags" : tags, " jumphost" : jumphost}
2022-04-03 09:38:00 -03:00
for key in attr:
2023-05-05 13:41:32 -03:00
profile = re.search(" ^@(.*)" , str(attr[key]))
2022-04-03 09:38:00 -03:00
if profile and config != ' ' :
2023-05-05 13:41:32 -03:00
try:
setattr(self,key,config.profiles[profile.group(1)][key])
except:
setattr(self,key," " )
2022-04-03 09:38:00 -03:00
elif attr[key] == ' ' and key == " protocol" :
try:
setattr(self,key,config.profiles[" default" ][key])
except:
setattr(self,key," ssh" )
else:
setattr(self,key,attr[key])
if isinstance(password,list):
self.password = []
for i, s in enumerate(password):
profile = re.search(" ^@(.*)" , password[i])
if profile and config != ' ' :
self.password.append(config.profiles[profile.group(1)][" password" ])
else:
self.password = [password]
2023-12-06 10:53:42 -03:00
if self.jumphost != " " and config != ' ' :
self.jumphost = config.getitem(self.jumphost)
for key in self.jumphost:
profile = re.search(" ^@(.*)" , str(self.jumphost[key]))
if profile:
try:
self.jumphost[key] = config.profiles[profile.group(1)][key]
except:
self.jumphost[key] = " "
elif self.jumphost[key] == ' ' and key == " protocol" :
try:
self.jumphost[key] = config.profiles[" default" ][key]
except:
self.jumphost[key] = " ssh"
if isinstance(self.jumphost[" password" ],list):
jumphost_password = []
for i, s in enumerate(self.jumphost[" password" ]):
profile = re.search(" ^@(.*)" , self.jumphost[" password" ][i])
if profile:
jumphost_password.append(config.profiles[profile.group(1)][" password" ])
self.jumphost[" password" ] = jumphost_password
else:
self.jumphost[" password" ] = [self.jumphost[" password" ]]
if self.jumphost[" password" ] != [" " ]:
self.password = self.jumphost[" password" ] + self.password
if self.jumphost[" protocol" ] == " ssh" :
jumphost_cmd = self.jumphost[" protocol" ] + " -W %h:%p"
if self.jumphost[" port" ] != ' ' :
jumphost_cmd = jumphost_cmd + " -p " + self.jumphost[" port" ]
if self.jumphost[" options" ] != ' ' :
jumphost_cmd = jumphost_cmd + " " + self.jumphost[" options" ]
if self.jumphost[" user" ] == ' ' :
jumphost_cmd = jumphost_cmd + " {}" .format(self.jumphost[" host" ])
else:
jumphost_cmd = jumphost_cmd + " {}" .format(" @" .join([self.jumphost[" user" ],self.jumphost[" host" ]]))
self.jumphost = f" -o ProxyCommand=\" {jumphost_cmd}\" "
else:
self.jumphost = " "
2022-04-03 09:38:00 -03:00
2022-06-10 13:24:26 -03:00
def _passtx(self, passwords, *, keyfile=None):
2022-04-03 09:38:00 -03:00
# decrypts passwords, used by other methdos.
dpass = []
if keyfile is None:
keyfile = self.key
if keyfile is not None:
2022-06-10 13:24:26 -03:00
with open(keyfile) as f:
key = RSA.import_key(f.read())
2022-04-03 09:38:00 -03:00
decryptor = PKCS1_OAEP.new(key)
for passwd in passwords:
if not re.match(' ^b[\" \' ].+[\" \' ]$' , passwd):
dpass.append(passwd)
else:
try:
decrypted = decryptor.decrypt(ast.literal_eval(passwd)).decode(" utf-8" )
dpass.append(decrypted)
except:
raise ValueError(" Missing or corrupted key" )
return dpass
def _logfile(self, logfile = None):
# translate logs variables and generate logs path.
if logfile == None:
logfile = self.logs
logfile = logfile.replace(" ${unique}" , self.unique)
logfile = logfile.replace(" ${host}" , self.host)
logfile = logfile.replace(" ${port}" , self.port)
logfile = logfile.replace(" ${user}" , self.user)
logfile = logfile.replace(" ${protocol}" , self.protocol)
now = datetime.datetime.now()
dateconf = re.search(r' \$\{date \' (.*)\' }' , logfile)
if dateconf:
logfile = re.sub(r' \$\{date (.*)}' ,now.strftime(dateconf.group(1)), logfile)
return logfile
def _logclean(self, logfile, var = False):
#Remove special ascii characters and other stuff from logfile.
if var == False:
t = open(logfile, " r" ).read()
else:
t = logfile
2023-07-11 19:33:21 -03:00
while t.find(" \b" ) != -1:
t = re.sub(' [^\b]\b' , ' ' , t)
2022-05-19 17:30:05 -03:00
t = t.replace(" \n" ," " ,1)
t = t.replace(" \a" ," " )
2022-04-03 09:38:00 -03:00
t = t.replace(' \n\n' , ' \n' )
t = re.sub(r' .\[K' , ' ' , t)
ansi_escape = re.compile(r' \x1B(?:[@-Z\\-_]|\[[0-?]*[ -/ ]*[@-~])' )
t = ansi_escape.sub(' ' , t)
2022-05-19 16:11:41 -03:00
t = t.lstrip(" \n\r" )
2022-05-19 17:30:05 -03:00
t = t.replace(" \r" ," " )
2022-06-10 13:24:26 -03:00
t = t.replace(" \x0E" ," " )
t = t.replace(" \x0F" ," " )
2022-04-03 09:38:00 -03:00
if var == False:
d = open(logfile, " w" )
d.write(t)
d.close()
return
else:
return t
2023-10-05 15:21:17 -03:00
def _savelog(self):
' ' ' Save the log buffer to the file at regular intervals if there are changes.' ' '
t = threading.current_thread()
prev_size = 0 # Store the previous size of the buffer
while getattr(t, " do_run" , True): # Check if thread is signaled to stop
current_size = self.mylog.tell() # Current size of the buffer
# Only save if the buffer size has changed
if current_size != prev_size:
with open(self.logfile, " w" ) as f: # Use " w" to overwrite the file
f.write(self._logclean(self.mylog.getvalue().decode(), True))
prev_size = current_size # Update the previous size
sleep(5)
2022-05-25 17:25:02 -03:00
def _filter(self, a):
#Set time for last input when using interact
self.lastinput = time()
return a
def _keepalive(self):
#Send keepalive ctrl+e when idletime passed without new inputs on interact
self.lastinput = time()
2022-06-10 13:24:26 -03:00
t = threading.current_thread()
2022-05-25 17:25:02 -03:00
while True:
if time() - self.lastinput > = self.idletime:
self.child.sendcontrol(" e" )
self.lastinput = time()
sleep(1)
2022-04-03 09:38:00 -03:00
def interact(self, debug = False):
' ' '
Allow user to interact with the node directly, mostly used by connection manager.
2022-04-03 10:26:08 -03:00
### Optional Parameters:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- debug (bool): If True, display all the connecting information
before interact. Default False.
2022-04-03 09:38:00 -03:00
' ' '
connect = self._connect(debug = debug)
if connect == True:
size = re.search(' columns=([0-9]+).*lines=([0-9]+)' ,str(os.get_terminal_size()))
self.child.setwinsize(int(size.group(2)),int(size.group(1)))
print(" Connected to " + self.unique + " at " + self.host + (" :" if self.port != ' ' else ' ' ) + self.port + " via: " + self.protocol)
if ' logfile' in dir(self):
2023-10-05 15:21:17 -03:00
# Initialize self.mylog
2022-05-25 17:25:02 -03:00
if not ' mylog' in dir(self):
self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog
2023-10-05 15:21:17 -03:00
# Start the _savelog thread
log_thread = threading.Thread(target=self._savelog)
log_thread.daemon = True
log_thread.start()
2022-04-03 09:38:00 -03:00
if ' missingtext' in dir(self):
print(self.child.after.decode(), end=' ' )
2022-05-25 17:25:02 -03:00
if self.idletime > 0:
x = threading.Thread(target=self._keepalive)
x.daemon = True
x.start()
if debug:
print(self.mylog.getvalue().decode())
self.child.interact(input_filter=self._filter)
2023-10-05 15:21:17 -03:00
if ' logfile' in dir(self):
2022-05-25 17:25:02 -03:00
with open(self.logfile, " w" ) as f:
2023-10-05 15:21:17 -03:00
f.write(self._logclean(self.mylog.getvalue().decode(), True))
2022-05-25 17:25:02 -03:00
2022-04-03 09:38:00 -03:00
else:
print(connect)
exit(1)
2023-01-05 16:39:22 -03:00
def run(self, commands, vars = None,*, folder = ' ' , prompt = r' > $|#$|\$$|> .$|#.$|\$.$' , stdout = False, timeout = 10):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on the node and return the output.
2022-04-03 10:26:08 -03:00
### Parameters:
- commands (str/list): Commands to run on the node. Should be
2022-04-28 12:49:31 -03:00
str or a list of str. You can use variables
as {varname} and defining them in optional
parameter vars.
### Optional Parameters:
- vars (dict): Dictionary containing the definition of variables
used in commands parameter.
Keys: Variable names.
Values: strings.
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- folder (str): Path where output log should be stored, leave
empty to disable logging.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while
routers use " > " or " #" . The default value should
work for most nodes. Change it if your connection
need some special symbol.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- stdout (bool):Set True to send the command output to stdout.
default False.
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-28 12:49:31 -03:00
2022-04-03 10:26:08 -03:00
### Returns:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
str: Output of the commands you ran on the node.
2022-04-03 09:38:00 -03:00
' ' '
2022-04-28 12:49:31 -03:00
connect = self._connect(timeout = timeout)
2022-05-19 16:11:41 -03:00
now = datetime.datetime.now().strftime(' %Y-%m-%d_%H%M%S' )
2022-04-03 09:38:00 -03:00
if connect == True:
2023-05-05 13:41:32 -03:00
if " prompt" in self.tags:
prompt = self.tags[" prompt" ]
2022-04-28 12:49:31 -03:00
expects = [prompt, pexpect.EOF, pexpect.TIMEOUT]
2022-04-03 09:38:00 -03:00
output = ' '
2022-05-19 16:11:41 -03:00
status = ' '
2022-04-28 12:49:31 -03:00
if not isinstance(commands, list):
commands = [commands]
2023-05-05 13:41:32 -03:00
if " screen_length_command" in self.tags:
commands.insert(0, self.tags[" screen_length_command" ])
2022-05-25 17:25:02 -03:00
self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog
2022-04-28 12:49:31 -03:00
for c in commands:
if vars is not None:
c = c.format(**vars)
result = self.child.expect(expects, timeout = timeout)
self.child.sendline(c)
if result == 2:
2022-05-19 16:11:41 -03:00
break
2022-05-25 17:25:02 -03:00
if not result == 2:
2022-05-19 16:11:41 -03:00
result = self.child.expect(expects, timeout = timeout)
2022-04-03 09:38:00 -03:00
self.child.close()
2022-05-25 17:25:02 -03:00
output = self._logclean(self.mylog.getvalue().decode(), True)
2022-04-03 09:38:00 -03:00
if stdout == True:
print(output)
if folder != ' ' :
2022-05-19 16:11:41 -03:00
with open(folder + " /" + self.unique + " _" + now + " .txt" , " w" ) as f:
2022-04-03 09:38:00 -03:00
f.write(output)
f.close()
self.output = output
2022-05-25 17:25:02 -03:00
if result == 2:
2022-05-19 16:11:41 -03:00
self.status = 2
else:
self.status = 0
2022-04-03 09:38:00 -03:00
return output
else:
self.output = connect
2022-04-28 12:49:31 -03:00
self.status = 1
2022-05-19 16:11:41 -03:00
if stdout == True:
print(connect)
if folder != ' ' :
with open(folder + " /" + self.unique + " _" + now + " .txt" , " w" ) as f:
f.write(connect)
f.close()
2022-04-03 09:38:00 -03:00
return connect
2023-01-05 16:39:22 -03:00
def test(self, commands, expected, vars = None,*, prompt = r' > $|#$|\$$|> .$|#.$|\$.$' , timeout = 10):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on the node, then check if expected value appears on the output after the last command.
2022-04-03 10:26:08 -03:00
### Parameters:
- commands (str/list): Commands to run on the node. Should be
2022-04-28 12:49:31 -03:00
str or a list of str. You can use variables
as {varname} and defining them in optional
parameter vars.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- expected (str) : Expected text to appear after running
2022-04-28 12:49:31 -03:00
all the commands on the node.You can use
variables as {varname} and defining them
in optional parameter vars.
### Optional Parameters:
- vars (dict): Dictionary containing the definition of variables
used in commands and expected parameters.
Keys: Variable names.
Values: strings.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while
routers use " > " or " #" . The default value should
work for most nodes. Change it if your connection
need some special symbol.
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-28 12:49:31 -03:00
2022-04-03 09:38:00 -03:00
### Returns:
2022-04-03 10:26:08 -03:00
bool: true if expected value is found after running the commands
false if prompt is found before.
2022-04-03 09:38:00 -03:00
' ' '
2022-04-28 12:49:31 -03:00
connect = self._connect(timeout = timeout)
2022-04-03 09:38:00 -03:00
if connect == True:
2023-05-05 13:41:32 -03:00
if " prompt" in self.tags:
prompt = self.tags[" prompt" ]
2022-04-28 12:49:31 -03:00
expects = [prompt, pexpect.EOF, pexpect.TIMEOUT]
2022-04-03 09:38:00 -03:00
output = ' '
2022-04-28 12:49:31 -03:00
if not isinstance(commands, list):
commands = [commands]
2023-07-11 19:33:21 -03:00
if not isinstance(expected, list):
expected = [expected]
2023-05-05 13:41:32 -03:00
if " screen_length_command" in self.tags:
commands.insert(0, self.tags[" screen_length_command" ])
2022-05-25 17:25:02 -03:00
self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog
2022-04-28 12:49:31 -03:00
for c in commands:
if vars is not None:
c = c.format(**vars)
result = self.child.expect(expects, timeout = timeout)
self.child.sendline(c)
if result == 2:
2022-05-25 17:25:02 -03:00
break
2023-05-05 13:41:32 -03:00
if not result == 2:
2022-05-25 17:25:02 -03:00
result = self.child.expect(expects, timeout = timeout)
2022-04-28 12:49:31 -03:00
self.child.close()
2022-05-25 17:25:02 -03:00
output = self._logclean(self.mylog.getvalue().decode(), True)
self.output = output
2023-05-05 13:41:32 -03:00
if result in [0, 1]:
2023-07-11 19:33:21 -03:00
# lastcommand = commands[-1]
# if vars is not None:
# lastcommand = lastcommand.format(**vars)
# last_command_index = output.rfind(lastcommand)
# cleaned_output = output[last_command_index + len(lastcommand):].strip()
self.result = {}
for e in expected:
if vars is not None:
e = e.format(**vars)
updatedprompt = re.sub(r' (?< !\\)\$' , ' ' , prompt)
newpattern = f" .*({updatedprompt}).*{e}.*"
cleaned_output = output
cleaned_output = re.sub(newpattern, ' ' , cleaned_output)
if e in cleaned_output:
self.result[e] = True
else:
self.result[e]= False
2022-04-28 12:49:31 -03:00
self.status = 0
2023-07-11 19:33:21 -03:00
return self.result
2023-05-05 13:41:32 -03:00
if result == 2:
2022-04-28 12:49:31 -03:00
self.result = None
self.status = 2
return output
2022-04-03 09:38:00 -03:00
else:
self.result = None
self.output = connect
2022-04-28 12:49:31 -03:00
self.status = 1
2022-04-03 09:38:00 -03:00
return connect
2023-01-05 16:39:22 -03:00
def _connect(self, debug = False, timeout = 10, max_attempts = 3):
2022-04-03 09:38:00 -03:00
# Method to connect to the node, it parse all the information, create the ssh/telnet command and login to the node.
2023-11-03 11:59:00 -03:00
if self.protocol in [" ssh" , " sftp" ]:
cmd = self.protocol
2022-04-03 09:38:00 -03:00
if self.idletime > 0:
cmd = cmd + " -o ServerAliveInterval=" + str(self.idletime)
if self.port != ' ' :
2023-11-03 11:59:00 -03:00
if self.protocol == " ssh" :
cmd = cmd + " -p " + self.port
elif self.protocol == " sftp" :
cmd = cmd + " -P " + self.port
2022-04-03 09:38:00 -03:00
if self.options != ' ' :
cmd = cmd + " " + self.options
if self.logs != ' ' :
self.logfile = self._logfile()
2023-12-06 10:53:42 -03:00
if self.jumphost != ' ' :
cmd = cmd + " " + self.jumphost
2022-04-03 09:38:00 -03:00
if self.password[0] != ' ' :
2022-06-10 13:24:26 -03:00
passwords = self._passtx(self.password)
2022-04-03 09:38:00 -03:00
else:
passwords = []
2023-11-03 11:59:00 -03:00
if self.user == ' ' :
cmd = cmd + " {}" .format(self.host)
else:
cmd = cmd + " {}" .format(" @" .join([self.user,self.host]))
2023-12-06 17:46:39 -03:00
expects = [' yes/no' , ' refused' , ' supported' , ' Invalid|[u|U]sage: (ssh|sftp)' , ' ssh-keygen.*\" ' , ' timeout|timed.out' , ' unavailable' , ' closed' , ' [p|P]assword:|[u|U]sername:' , r' > $|#$|\$$|> .$|#.$|\$.$' , ' suspend' , pexpect.EOF, pexpect.TIMEOUT, " No route to host" , " resolve hostname" , " no matching" , " [b|B]ad (owner|permissions)" ]
2022-04-03 09:38:00 -03:00
elif self.protocol == " telnet" :
cmd = " telnet " + self.host
if self.port != ' ' :
cmd = cmd + " " + self.port
if self.options != ' ' :
cmd = cmd + " " + self.options
if self.logs != ' ' :
self.logfile = self._logfile()
if self.password[0] != ' ' :
2022-06-10 13:24:26 -03:00
passwords = self._passtx(self.password)
2022-04-03 09:38:00 -03:00
else:
passwords = []
2023-12-06 17:46:39 -03:00
expects = [' [u|U]sername:' , ' refused' , ' supported' , ' invalid option' , ' ssh-keygen.*\" ' , ' timeout|timed.out' , ' unavailable' , ' closed' , ' [p|P]assword:' , r' > $|#$|\$$|> .$|#.$|\$.$' , ' suspend' , pexpect.EOF, pexpect.TIMEOUT, " No route to host" , " resolve hostname" , " no matching" , " [b|B]ad (owner|permissions)" ]
2022-04-03 09:38:00 -03:00
else:
raise ValueError(" Invalid protocol: " + self.protocol)
2023-01-05 16:39:22 -03:00
attempts = 1
while attempts < = max_attempts:
child = pexpect.spawn(cmd)
if debug:
print(cmd)
self.mylog = io.BytesIO()
child.logfile_read = self.mylog
if len(passwords) > 0:
loops = len(passwords)
else:
loops = 1
endloop = False
for i in range(0, loops):
while True:
results = child.expect(expects, timeout=timeout)
if results == 0:
2023-11-03 11:59:00 -03:00
if self.protocol in [" ssh" , " sftp" ]:
2023-01-05 16:39:22 -03:00
child.sendline(' yes' )
elif self.protocol == " telnet" :
if self.user != ' ' :
child.sendline(self.user)
else:
self.missingtext = True
break
2023-12-01 15:31:17 -03:00
if results in [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16]:
2023-01-05 16:39:22 -03:00
child.terminate()
if results == 12 and attempts != max_attempts:
attempts += 1
endloop = True
break
else:
2023-10-26 17:33:44 -03:00
if results == 12:
after = " Connection timeout"
else:
after = child.after.decode()
2023-11-03 11:59:00 -03:00
return (" Connection failed code:" + str(results) + " \n" + child.before.decode().lstrip() + after + child.readline().decode()).rstrip()
2023-01-05 16:39:22 -03:00
if results == 8:
if len(passwords) > 0:
child.sendline(passwords[i])
2022-04-03 09:38:00 -03:00
else:
self.missingtext = True
2023-01-05 16:39:22 -03:00
break
if results in [9, 11]:
endloop = True
child.sendline()
break
if results == 10:
child.sendline(" \r" )
sleep(2)
if endloop:
2022-04-03 09:38:00 -03:00
break
2023-01-05 16:39:22 -03:00
if results == 12:
continue
else:
2022-04-03 09:38:00 -03:00
break
child.readline(0)
self.child = child
return True< / code > < / pre >
< / details >
< h3 > Methods< / h3 >
< dl >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.node.interact" > < code class = "name flex" >
2022-04-03 09:38:00 -03:00
< span > def < span class = "ident" > interact< / span > < / span > (< span > self, debug=False)< / span >
< / code > < / dt >
< dd >
< div class = "desc" > < p > Allow user to interact with the node directly, mostly used by connection manager.< / p >
2022-04-03 10:26:08 -03:00
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - debug (bool): If True, display all the connecting information
before interact. Default False.
2022-04-03 09:38:00 -03:00
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > def interact(self, debug = False):
' ' '
Allow user to interact with the node directly, mostly used by connection manager.
2022-04-03 10:26:08 -03:00
### Optional Parameters:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- debug (bool): If True, display all the connecting information
before interact. Default False.
2022-04-03 09:38:00 -03:00
' ' '
connect = self._connect(debug = debug)
if connect == True:
size = re.search(' columns=([0-9]+).*lines=([0-9]+)' ,str(os.get_terminal_size()))
self.child.setwinsize(int(size.group(2)),int(size.group(1)))
print(" Connected to " + self.unique + " at " + self.host + (" :" if self.port != ' ' else ' ' ) + self.port + " via: " + self.protocol)
if ' logfile' in dir(self):
2023-10-05 15:21:17 -03:00
# Initialize self.mylog
2022-05-25 17:25:02 -03:00
if not ' mylog' in dir(self):
self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog
2023-10-05 15:21:17 -03:00
# Start the _savelog thread
log_thread = threading.Thread(target=self._savelog)
log_thread.daemon = True
log_thread.start()
2022-04-03 09:38:00 -03:00
if ' missingtext' in dir(self):
print(self.child.after.decode(), end=' ' )
2022-05-25 17:25:02 -03:00
if self.idletime > 0:
x = threading.Thread(target=self._keepalive)
x.daemon = True
x.start()
if debug:
print(self.mylog.getvalue().decode())
self.child.interact(input_filter=self._filter)
2023-10-05 15:21:17 -03:00
if ' logfile' in dir(self):
2022-05-25 17:25:02 -03:00
with open(self.logfile, " w" ) as f:
2023-10-05 15:21:17 -03:00
f.write(self._logclean(self.mylog.getvalue().decode(), True))
2022-05-25 17:25:02 -03:00
2022-04-03 09:38:00 -03:00
else:
print(connect)
exit(1)< / code > < / pre >
< / details >
< / dd >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.node.run" > < code class = "name flex" >
2023-01-05 16:39:22 -03:00
< span > def < span class = "ident" > run< / span > < / span > (< span > self, commands, vars=None, *, folder='', prompt=' > $|#$|\\$$|> .$|#.$|\\$.$' , stdout=False, timeout=10)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < p > Run a command or list of commands on the node and return the output.< / p >
2022-04-03 10:26:08 -03:00
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - commands (str/list): Commands to run on the node. Should be
2022-04-28 12:49:31 -03:00
str or a list of str. You can use variables
as {varname} and defining them in optional
parameter vars.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - vars (dict): Dictionary containing the definition of variables
used in commands parameter.
Keys: Variable names.
Values: strings.
2022-04-03 09:38:00 -03:00
< / code > < / pre >
2022-04-03 10:26:08 -03:00
< h3 id = "optional-named-parameters" > Optional Named Parameters:< / h3 >
< pre > < code > - folder (str): Path where output log should be stored, leave
empty to disable logging.
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses "> " or EOF while
routers use "> " or "#". The default value should
work for most nodes. Change it if your connection
need some special symbol.
- stdout (bool):Set True to send the command output to stdout.
default False.
2022-04-28 12:49:31 -03:00
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 09:38:00 -03:00
< / code > < / pre >
2022-04-03 10:26:08 -03:00
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > str: Output of the commands you ran on the node.
2022-04-03 09:38:00 -03:00
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2023-01-05 16:39:22 -03:00
< pre > < code class = "python" > def run(self, commands, vars = None,*, folder = ' ' , prompt = r' > $|#$|\$$|> .$|#.$|\$.$' , stdout = False, timeout = 10):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on the node and return the output.
2022-04-03 10:26:08 -03:00
### Parameters:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- commands (str/list): Commands to run on the node. Should be
2022-04-28 12:49:31 -03:00
str or a list of str. You can use variables
as {varname} and defining them in optional
parameter vars.
### Optional Parameters:
- vars (dict): Dictionary containing the definition of variables
used in commands parameter.
Keys: Variable names.
Values: strings.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- folder (str): Path where output log should be stored, leave
empty to disable logging.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while
routers use " > " or " #" . The default value should
work for most nodes. Change it if your connection
need some special symbol.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- stdout (bool):Set True to send the command output to stdout.
default False.
2022-04-28 12:49:31 -03:00
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-28 12:49:31 -03:00
2022-04-03 10:26:08 -03:00
### Returns:
str: Output of the commands you ran on the node.
2022-04-03 09:38:00 -03:00
' ' '
2022-04-28 12:49:31 -03:00
connect = self._connect(timeout = timeout)
2022-05-19 16:11:41 -03:00
now = datetime.datetime.now().strftime(' %Y-%m-%d_%H%M%S' )
2022-04-03 09:38:00 -03:00
if connect == True:
2023-05-05 13:41:32 -03:00
if " prompt" in self.tags:
prompt = self.tags[" prompt" ]
2022-04-28 12:49:31 -03:00
expects = [prompt, pexpect.EOF, pexpect.TIMEOUT]
2022-04-03 09:38:00 -03:00
output = ' '
2022-05-19 16:11:41 -03:00
status = ' '
2022-04-28 12:49:31 -03:00
if not isinstance(commands, list):
commands = [commands]
2023-05-05 13:41:32 -03:00
if " screen_length_command" in self.tags:
commands.insert(0, self.tags[" screen_length_command" ])
2022-05-25 17:25:02 -03:00
self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog
2022-04-28 12:49:31 -03:00
for c in commands:
if vars is not None:
c = c.format(**vars)
result = self.child.expect(expects, timeout = timeout)
self.child.sendline(c)
if result == 2:
2022-05-19 16:11:41 -03:00
break
2022-05-25 17:25:02 -03:00
if not result == 2:
2022-05-19 16:11:41 -03:00
result = self.child.expect(expects, timeout = timeout)
2022-04-03 09:38:00 -03:00
self.child.close()
2022-05-25 17:25:02 -03:00
output = self._logclean(self.mylog.getvalue().decode(), True)
2022-04-03 09:38:00 -03:00
if stdout == True:
print(output)
if folder != ' ' :
2022-05-19 16:11:41 -03:00
with open(folder + " /" + self.unique + " _" + now + " .txt" , " w" ) as f:
2022-04-03 09:38:00 -03:00
f.write(output)
f.close()
self.output = output
2022-05-25 17:25:02 -03:00
if result == 2:
2022-05-19 16:11:41 -03:00
self.status = 2
else:
self.status = 0
2022-04-03 09:38:00 -03:00
return output
else:
self.output = connect
2022-04-28 12:49:31 -03:00
self.status = 1
2022-05-19 16:11:41 -03:00
if stdout == True:
print(connect)
if folder != ' ' :
with open(folder + " /" + self.unique + " _" + now + " .txt" , " w" ) as f:
f.write(connect)
f.close()
2022-04-03 09:38:00 -03:00
return connect< / code > < / pre >
< / details >
< / dd >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.node.test" > < code class = "name flex" >
2023-01-05 16:39:22 -03:00
< span > def < span class = "ident" > test< / span > < / span > (< span > self, commands, expected, vars=None, *, prompt=' > $|#$|\\$$|> .$|#.$|\\$.$' , timeout=10)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < p > Run a command or list of commands on the node, then check if expected value appears on the output after the last command.< / p >
2022-04-03 10:26:08 -03:00
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - commands (str/list): Commands to run on the node. Should be
2022-04-28 12:49:31 -03:00
str or a list of str. You can use variables
as {varname} and defining them in optional
parameter vars.
2022-04-03 10:26:08 -03:00
- expected (str) : Expected text to appear after running
2022-04-28 12:49:31 -03:00
all the commands on the node.You can use
variables as {varname} and defining them
in optional parameter vars.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - vars (dict): Dictionary containing the definition of variables
used in commands and expected parameters.
Keys: Variable names.
Values: strings.
2022-04-03 09:38:00 -03:00
< / code > < / pre >
2022-04-03 10:26:08 -03:00
< h3 id = "optional-named-parameters" > Optional Named Parameters:< / h3 >
< pre > < code > - prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses "> " or EOF while
routers use "> " or "#". The default value should
work for most nodes. Change it if your connection
need some special symbol.
2022-04-28 12:49:31 -03:00
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 09:38:00 -03:00
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
2022-04-03 10:26:08 -03:00
< pre > < code > bool: true if expected value is found after running the commands
false if prompt is found before.
2022-04-03 09:38:00 -03:00
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2023-01-05 16:39:22 -03:00
< pre > < code class = "python" > def test(self, commands, expected, vars = None,*, prompt = r' > $|#$|\$$|> .$|#.$|\$.$' , timeout = 10):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on the node, then check if expected value appears on the output after the last command.
2022-04-03 10:26:08 -03:00
### Parameters:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- commands (str/list): Commands to run on the node. Should be
2022-04-28 12:49:31 -03:00
str or a list of str. You can use variables
as {varname} and defining them in optional
parameter vars.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
- expected (str) : Expected text to appear after running
2022-04-28 12:49:31 -03:00
all the commands on the node.You can use
variables as {varname} and defining them
in optional parameter vars.
### Optional Parameters:
- vars (dict): Dictionary containing the definition of variables
used in commands and expected parameters.
Keys: Variable names.
Values: strings.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while
routers use " > " or " #" . The default value should
work for most nodes. Change it if your connection
need some special symbol.
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- timeout (int):Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-28 12:49:31 -03:00
2022-04-03 09:38:00 -03:00
### Returns:
2022-04-03 10:26:08 -03:00
bool: true if expected value is found after running the commands
false if prompt is found before.
2022-04-03 09:38:00 -03:00
' ' '
2022-04-28 12:49:31 -03:00
connect = self._connect(timeout = timeout)
2022-04-03 09:38:00 -03:00
if connect == True:
2023-05-05 13:41:32 -03:00
if " prompt" in self.tags:
prompt = self.tags[" prompt" ]
2022-04-28 12:49:31 -03:00
expects = [prompt, pexpect.EOF, pexpect.TIMEOUT]
2022-04-03 09:38:00 -03:00
output = ' '
2022-04-28 12:49:31 -03:00
if not isinstance(commands, list):
commands = [commands]
2023-07-11 19:33:21 -03:00
if not isinstance(expected, list):
expected = [expected]
2023-05-05 13:41:32 -03:00
if " screen_length_command" in self.tags:
commands.insert(0, self.tags[" screen_length_command" ])
2022-05-25 17:25:02 -03:00
self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog
2022-04-28 12:49:31 -03:00
for c in commands:
if vars is not None:
c = c.format(**vars)
result = self.child.expect(expects, timeout = timeout)
self.child.sendline(c)
if result == 2:
2022-05-25 17:25:02 -03:00
break
2023-05-05 13:41:32 -03:00
if not result == 2:
2022-05-25 17:25:02 -03:00
result = self.child.expect(expects, timeout = timeout)
2022-04-28 12:49:31 -03:00
self.child.close()
2022-05-25 17:25:02 -03:00
output = self._logclean(self.mylog.getvalue().decode(), True)
self.output = output
2023-05-05 13:41:32 -03:00
if result in [0, 1]:
2023-07-11 19:33:21 -03:00
# lastcommand = commands[-1]
# if vars is not None:
# lastcommand = lastcommand.format(**vars)
# last_command_index = output.rfind(lastcommand)
# cleaned_output = output[last_command_index + len(lastcommand):].strip()
self.result = {}
for e in expected:
if vars is not None:
e = e.format(**vars)
updatedprompt = re.sub(r' (?< !\\)\$' , ' ' , prompt)
newpattern = f" .*({updatedprompt}).*{e}.*"
cleaned_output = output
cleaned_output = re.sub(newpattern, ' ' , cleaned_output)
if e in cleaned_output:
self.result[e] = True
else:
self.result[e]= False
2022-04-28 12:49:31 -03:00
self.status = 0
2023-07-11 19:33:21 -03:00
return self.result
2023-05-05 13:41:32 -03:00
if result == 2:
2022-04-28 12:49:31 -03:00
self.result = None
self.status = 2
return output
2022-04-03 09:38:00 -03:00
else:
self.result = None
self.output = connect
2022-04-28 12:49:31 -03:00
self.status = 1
2022-04-03 09:38:00 -03:00
return connect< / code > < / pre >
< / details >
< / dd >
< / dl >
< / dd >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.nodes" > < code class = "flex name class" >
2022-04-03 09:38:00 -03:00
< span > class < span class = "ident" > nodes< / span > < / span >
< span > (< / span > < span > nodes: dict, config='')< / span >
< / code > < / dt >
< dd >
< div class = "desc" > < p > This class generates a nodes object. Contains a list of node class objects and methods to run multiple tasks on nodes simultaneously.< / p >
< h3 id = "attributes" > Attributes:< / h3 >
< pre > < code > - nodelist (list): List of node class objects passed to the init
function.
- output (dict): Dictionary formed by nodes unique as keys,
output of the commands you ran on the node as
value. Created after running methods run or test.
- result (dict): Dictionary formed by nodes unique as keys, value
is True if expected value is found after running
the commands, False if prompt is found before.
Created after running method test.
2022-04-28 12:49:31 -03:00
- status (dict): Dictionary formed by nodes unique as keys, value:
0 if method run or test ended succesfully.
1 if connection failed.
2 if expect timeouts without prompt or EOF.
2022-04-03 09:38:00 -03:00
- < unique> (obj): For each item in nodelist, there is an attribute
generated with the node unique.
< / code > < / pre >
< h3 id = "parameters" > Parameters:< / h3 >
< pre > < code > - nodes (dict): Dictionary formed by node information:
Keys: Unique name for each node.
Mandatory Subkeys: host(str).
Optional Subkeys: options(str), logs(str), password(str),
port(str), protocol(str), user(str).
For reference on subkeys check node class.
< / code > < / pre >
2022-04-03 10:26:08 -03:00
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
2022-04-03 09:38:00 -03:00
< pre > < code > - config (obj): Pass the object created with class configfile with key
for decryption and extra configuration if you are using
connection manager.
< / code > < / pre > < / div >
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
< pre > < code class = "python" > class nodes:
' ' ' This class generates a nodes object. Contains a list of node class objects and methods to run multiple tasks on nodes simultaneously.
### Attributes:
- nodelist (list): List of node class objects passed to the init
function.
- output (dict): Dictionary formed by nodes unique as keys,
output of the commands you ran on the node as
value. Created after running methods run or test.
- result (dict): Dictionary formed by nodes unique as keys, value
is True if expected value is found after running
the commands, False if prompt is found before.
Created after running method test.
2022-04-28 12:49:31 -03:00
- status (dict): Dictionary formed by nodes unique as keys, value:
0 if method run or test ended succesfully.
1 if connection failed.
2 if expect timeouts without prompt or EOF.
2022-04-03 09:38:00 -03:00
- < unique> (obj): For each item in nodelist, there is an attribute
generated with the node unique.
' ' '
def __init__(self, nodes: dict, config = ' ' ):
' ' '
### Parameters:
- nodes (dict): Dictionary formed by node information:
Keys: Unique name for each node.
Mandatory Subkeys: host(str).
Optional Subkeys: options(str), logs(str), password(str),
port(str), protocol(str), user(str).
For reference on subkeys check node class.
2022-04-03 10:26:08 -03:00
### Optional Parameters:
2022-04-03 09:38:00 -03:00
- config (obj): Pass the object created with class configfile with key
for decryption and extra configuration if you are using
connection manager.
' ' '
self.nodelist = []
self.config = config
for n in nodes:
this = node(n, **nodes[n], config = config)
self.nodelist.append(this)
setattr(self,n,this)
def _splitlist(self, lst, n):
#split a list in lists of n members.
for i in range(0, len(lst), n):
yield lst[i:i + n]
2022-04-28 12:49:31 -03:00
def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on all the nodes in nodelist.
2022-04-03 10:26:08 -03:00
### Parameters:
2022-04-28 12:49:31 -03:00
- commands (str/list): Commands to run on the nodes. Should be str or
list of str. You can use variables as {varname}
and defining them in optional parameter vars.
### Optional Parameters:
- vars (dict): Dictionary containing the definition of variables for
each node, used in commands parameter.
Keys should be formed by nodes unique names. Use
special key name __global__ for global variables.
Subkeys: Variable names.
Values: strings.
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
2022-04-28 12:49:31 -03:00
- folder (str): Path where output log should be stored, leave empty
to disable logging.
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while routers
use " > " or " #" . The default value should work for
most nodes. Change it if your connection need some
special symbol.
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- stdout (bool): Set True to send the command output to stdout.
Default False.
2022-04-03 10:26:08 -03:00
2022-04-28 12:49:31 -03:00
- parallel (int): Number of nodes to run the commands simultaneously.
Default is 10, if there are more nodes that this
value, nodes are groups in groups with max this
number of members.
- timeout (int): Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 10:26:08 -03:00
###Returns:
dict: Dictionary formed by nodes unique as keys, Output of the
commands you ran on the node as value.
2022-04-03 09:38:00 -03:00
' ' '
args = {}
2022-04-28 12:49:31 -03:00
nodesargs = {}
2022-04-03 09:38:00 -03:00
args[" commands" ] = commands
if folder != None:
args[" folder" ] = folder
2022-05-19 16:11:41 -03:00
Path(folder).mkdir(parents=True, exist_ok=True)
2022-04-03 09:38:00 -03:00
if prompt != None:
args[" prompt" ] = prompt
if stdout != None:
args[" stdout" ] = stdout
2022-04-28 12:49:31 -03:00
if timeout != None:
args[" timeout" ] = timeout
2022-04-03 09:38:00 -03:00
output = {}
2022-04-28 12:49:31 -03:00
status = {}
2022-04-03 09:38:00 -03:00
tasks = []
for n in self.nodelist:
2022-05-11 14:25:43 -03:00
nodesargs[n.unique] = deepcopy(args)
2022-04-28 12:49:31 -03:00
if vars != None:
nodesargs[n.unique][" vars" ] = {}
if " __global__" in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[" __global__" ])
if n.unique in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[n.unique])
tasks.append(threading.Thread(target=n.run, kwargs=nodesargs[n.unique]))
2022-04-03 09:38:00 -03:00
taskslist = list(self._splitlist(tasks, parallel))
for t in taskslist:
for i in t:
i.start()
for i in t:
i.join()
for i in self.nodelist:
output[i.unique] = i.output
2022-04-28 12:49:31 -03:00
status[i.unique] = i.status
2022-04-03 09:38:00 -03:00
self.output = output
2022-04-28 12:49:31 -03:00
self.status = status
2022-04-03 09:38:00 -03:00
return output
2022-04-28 12:49:31 -03:00
def test(self, commands, expected, vars = None,*, prompt = None, parallel = 10, timeout = None):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.
2022-04-03 10:26:08 -03:00
### Parameters:
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- commands (str/list): Commands to run on the node. Should be str or
list of str.
- expected (str) : Expected text to appear after running all the
commands on the node.
### Optional Parameters:
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- vars (dict): Dictionary containing the definition of variables for
each node, used in commands and expected parameters.
Keys should be formed by nodes unique names. Use
special key name __global__ for global variables.
Subkeys: Variable names.
Values: strings.
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
2022-04-28 12:49:31 -03:00
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while
routers use " > " or " #" . The default value should
work for most nodes. Change it if your connection
need some special symbol.
- parallel (int): Number of nodes to run the commands simultaneously.
Default is 10, if there are more nodes that this
value, nodes are groups in groups with max this
number of members.
- timeout (int): Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 10:26:08 -03:00
### Returns:
dict: Dictionary formed by nodes unique as keys, value is True if
expected value is found after running the commands, False
if prompt is found before.
2022-04-03 09:38:00 -03:00
' ' '
args = {}
2022-04-28 12:49:31 -03:00
nodesargs = {}
2022-04-03 09:38:00 -03:00
args[" commands" ] = commands
args[" expected" ] = expected
if prompt != None:
args[" prompt" ] = prompt
2022-04-28 12:49:31 -03:00
if timeout != None:
args[" timeout" ] = timeout
2022-04-03 09:38:00 -03:00
output = {}
result = {}
2022-04-28 12:49:31 -03:00
status = {}
2022-04-03 09:38:00 -03:00
tasks = []
for n in self.nodelist:
2022-05-11 14:25:43 -03:00
nodesargs[n.unique] = deepcopy(args)
2022-04-28 12:49:31 -03:00
if vars != None:
nodesargs[n.unique][" vars" ] = {}
if " __global__" in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[" __global__" ])
if n.unique in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[n.unique])
tasks.append(threading.Thread(target=n.test, kwargs=nodesargs[n.unique]))
2022-04-03 09:38:00 -03:00
taskslist = list(self._splitlist(tasks, parallel))
for t in taskslist:
for i in t:
i.start()
for i in t:
i.join()
for i in self.nodelist:
result[i.unique] = i.result
output[i.unique] = i.output
2022-04-28 12:49:31 -03:00
status[i.unique] = i.status
2022-04-03 09:38:00 -03:00
self.output = output
self.result = result
2022-04-28 12:49:31 -03:00
self.status = status
2022-04-03 09:38:00 -03:00
return result< / code > < / pre >
< / details >
< h3 > Methods< / h3 >
< dl >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.nodes.run" > < code class = "name flex" >
2022-04-28 12:49:31 -03:00
< span > def < span class = "ident" > run< / span > < / span > (< span > self, commands, vars=None, *, folder=None, prompt=None, stdout=None, parallel=10, timeout=None)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < p > Run a command or list of commands on all the nodes in nodelist.< / p >
2022-04-03 10:26:08 -03:00
< h3 id = "parameters" > Parameters:< / h3 >
2022-04-28 12:49:31 -03:00
< pre > < code > - commands (str/list): Commands to run on the nodes. Should be str or
list of str. You can use variables as {varname}
and defining them in optional parameter vars.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - vars (dict): Dictionary containing the definition of variables for
each node, used in commands parameter.
Keys should be formed by nodes unique names. Use
special key name __global__ for global variables.
Subkeys: Variable names.
Values: strings.
2022-04-03 10:26:08 -03:00
< / code > < / pre >
< h3 id = "optional-named-parameters" > Optional Named Parameters:< / h3 >
2022-04-28 12:49:31 -03:00
< pre > < code > - folder (str): Path where output log should be stored, leave empty
to disable logging.
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses "> " or EOF while routers
use "> " or "#". The default value should work for
most nodes. Change it if your connection need some
special symbol.
- stdout (bool): Set True to send the command output to stdout.
Default False.
- parallel (int): Number of nodes to run the commands simultaneously.
Default is 10, if there are more nodes that this
value, nodes are groups in groups with max this
number of members.
- timeout (int): Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 10:26:08 -03:00
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > dict: Dictionary formed by nodes unique as keys, Output of the
commands you ran on the node as value.
< / code > < / pre > < / div >
2022-04-03 09:38:00 -03:00
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2022-04-28 12:49:31 -03:00
< pre > < code class = "python" > def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on all the nodes in nodelist.
2022-04-03 10:26:08 -03:00
### Parameters:
2022-04-28 12:49:31 -03:00
- commands (str/list): Commands to run on the nodes. Should be str or
list of str. You can use variables as {varname}
and defining them in optional parameter vars.
### Optional Parameters:
- vars (dict): Dictionary containing the definition of variables for
each node, used in commands parameter.
Keys should be formed by nodes unique names. Use
special key name __global__ for global variables.
Subkeys: Variable names.
Values: strings.
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
2022-04-28 12:49:31 -03:00
- folder (str): Path where output log should be stored, leave empty
to disable logging.
2022-04-03 10:26:08 -03:00
2022-04-28 12:49:31 -03:00
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while routers
use " > " or " #" . The default value should work for
most nodes. Change it if your connection need some
special symbol.
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- stdout (bool): Set True to send the command output to stdout.
Default False.
2022-04-03 09:38:00 -03:00
2022-04-28 12:49:31 -03:00
- parallel (int): Number of nodes to run the commands simultaneously.
Default is 10, if there are more nodes that this
value, nodes are groups in groups with max this
number of members.
- timeout (int): Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 10:26:08 -03:00
###Returns:
dict: Dictionary formed by nodes unique as keys, Output of the
commands you ran on the node as value.
2022-04-03 09:38:00 -03:00
' ' '
args = {}
2022-04-28 12:49:31 -03:00
nodesargs = {}
2022-04-03 09:38:00 -03:00
args[" commands" ] = commands
if folder != None:
args[" folder" ] = folder
2022-05-19 16:11:41 -03:00
Path(folder).mkdir(parents=True, exist_ok=True)
2022-04-03 09:38:00 -03:00
if prompt != None:
args[" prompt" ] = prompt
if stdout != None:
args[" stdout" ] = stdout
2022-04-28 12:49:31 -03:00
if timeout != None:
args[" timeout" ] = timeout
2022-04-03 09:38:00 -03:00
output = {}
2022-04-28 12:49:31 -03:00
status = {}
2022-04-03 09:38:00 -03:00
tasks = []
for n in self.nodelist:
2022-05-11 14:25:43 -03:00
nodesargs[n.unique] = deepcopy(args)
2022-04-28 12:49:31 -03:00
if vars != None:
nodesargs[n.unique][" vars" ] = {}
if " __global__" in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[" __global__" ])
if n.unique in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[n.unique])
tasks.append(threading.Thread(target=n.run, kwargs=nodesargs[n.unique]))
2022-04-03 09:38:00 -03:00
taskslist = list(self._splitlist(tasks, parallel))
for t in taskslist:
for i in t:
i.start()
for i in t:
i.join()
for i in self.nodelist:
output[i.unique] = i.output
2022-04-28 12:49:31 -03:00
status[i.unique] = i.status
2022-04-03 09:38:00 -03:00
self.output = output
2022-04-28 12:49:31 -03:00
self.status = status
2022-04-03 09:38:00 -03:00
return output< / code > < / pre >
< / details >
< / dd >
2022-04-04 11:40:51 -03:00
< dt id = "connpy.nodes.test" > < code class = "name flex" >
2022-04-28 12:49:31 -03:00
< span > def < span class = "ident" > test< / span > < / span > (< span > self, commands, expected, vars=None, *, prompt=None, parallel=10, timeout=None)< / span >
2022-04-03 09:38:00 -03:00
< / code > < / dt >
< dd >
< div class = "desc" > < p > Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.< / p >
2022-04-03 10:26:08 -03:00
< h3 id = "parameters" > Parameters:< / h3 >
2022-04-28 12:49:31 -03:00
< pre > < code > - commands (str/list): Commands to run on the node. Should be str or
list of str.
2022-04-03 10:26:08 -03:00
2022-04-28 12:49:31 -03:00
- expected (str) : Expected text to appear after running all the
commands on the node.
< / code > < / pre >
< h3 id = "optional-parameters" > Optional Parameters:< / h3 >
< pre > < code > - vars (dict): Dictionary containing the definition of variables for
each node, used in commands and expected parameters.
Keys should be formed by nodes unique names. Use
special key name __global__ for global variables.
Subkeys: Variable names.
Values: strings.
2022-04-03 10:26:08 -03:00
< / code > < / pre >
< h3 id = "optional-named-parameters" > Optional Named Parameters:< / h3 >
2022-04-28 12:49:31 -03:00
< pre > < code > - prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses "> " or EOF while
routers use "> " or "#". The default value should
work for most nodes. Change it if your connection
need some special symbol.
- parallel (int): Number of nodes to run the commands simultaneously.
Default is 10, if there are more nodes that this
value, nodes are groups in groups with max this
number of members.
- timeout (int): Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 10:26:08 -03:00
< / code > < / pre >
< h3 id = "returns" > Returns:< / h3 >
< pre > < code > dict: Dictionary formed by nodes unique as keys, value is True if
expected value is found after running the commands, False
if prompt is found before.
< / code > < / pre > < / div >
2022-04-03 09:38:00 -03:00
< details class = "source" >
< summary >
< span > Expand source code< / span >
< / summary >
2022-04-28 12:49:31 -03:00
< pre > < code class = "python" > def test(self, commands, expected, vars = None,*, prompt = None, parallel = 10, timeout = None):
2022-04-03 09:38:00 -03:00
' ' '
Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.
2022-04-03 10:26:08 -03:00
### Parameters:
2022-04-28 12:49:31 -03:00
- commands (str/list): Commands to run on the node. Should be str or
list of str.
- expected (str) : Expected text to appear after running all the
commands on the node.
### Optional Parameters:
2022-04-03 10:26:08 -03:00
2022-04-28 12:49:31 -03:00
- vars (dict): Dictionary containing the definition of variables for
each node, used in commands and expected parameters.
Keys should be formed by nodes unique names. Use
special key name __global__ for global variables.
Subkeys: Variable names.
Values: strings.
2022-04-03 10:26:08 -03:00
### Optional Named Parameters:
2022-04-28 12:49:31 -03:00
- prompt (str): Prompt to be expected after a command is finished
running. Usually linux uses " > " or EOF while
routers use " > " or " #" . The default value should
work for most nodes. Change it if your connection
need some special symbol.
- parallel (int): Number of nodes to run the commands simultaneously.
Default is 10, if there are more nodes that this
value, nodes are groups in groups with max this
number of members.
- timeout (int): Time in seconds for expect to wait for prompt/EOF.
2023-03-21 18:23:29 -03:00
default 10.
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
### Returns:
2022-04-03 09:38:00 -03:00
2022-04-03 10:26:08 -03:00
dict: Dictionary formed by nodes unique as keys, value is True if
expected value is found after running the commands, False
if prompt is found before.
2022-04-03 09:38:00 -03:00
' ' '
args = {}
2022-04-28 12:49:31 -03:00
nodesargs = {}
2022-04-03 09:38:00 -03:00
args[" commands" ] = commands
args[" expected" ] = expected
if prompt != None:
args[" prompt" ] = prompt
2022-04-28 12:49:31 -03:00
if timeout != None:
args[" timeout" ] = timeout
2022-04-03 09:38:00 -03:00
output = {}
result = {}
2022-04-28 12:49:31 -03:00
status = {}
2022-04-03 09:38:00 -03:00
tasks = []
for n in self.nodelist:
2022-05-11 14:25:43 -03:00
nodesargs[n.unique] = deepcopy(args)
2022-04-28 12:49:31 -03:00
if vars != None:
nodesargs[n.unique][" vars" ] = {}
if " __global__" in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[" __global__" ])
if n.unique in vars.keys():
nodesargs[n.unique][" vars" ].update(vars[n.unique])
tasks.append(threading.Thread(target=n.test, kwargs=nodesargs[n.unique]))
2022-04-03 09:38:00 -03:00
taskslist = list(self._splitlist(tasks, parallel))
for t in taskslist:
for i in t:
i.start()
for i in t:
i.join()
for i in self.nodelist:
result[i.unique] = i.result
output[i.unique] = i.output
2022-04-28 12:49:31 -03:00
status[i.unique] = i.status
2022-04-03 09:38:00 -03:00
self.output = output
self.result = result
2022-04-28 12:49:31 -03:00
self.status = status
2022-04-03 09:38:00 -03:00
return result< / code > < / pre >
< / details >
< / dd >
< / dl >
< / dd >
< / dl >
< / section >
< / article >
< nav id = "sidebar" >
< h1 > Index< / h1 >
< div class = "toc" >
2022-04-03 12:00:35 -03:00
< ul >
< li > < a href = "#connection-manager" > Connection manager< / a > < ul >
2022-04-03 18:25:58 -03:00
< li > < a href = "#features" > Features< / a > < / li >
< li > < a href = "#usage" > Usage< / a > < / li >
2022-04-03 12:00:35 -03:00
< li > < a href = "#manage-profiles" > Manage profiles< / a > < / li >
< li > < a href = "#examples" > Examples< / a > < / li >
< / ul >
< / li >
2023-12-15 12:27:51 -03:00
< li > < a href = "#plugin-requirements-for-connpy" > Plugin Requirements for Connpy< / a > < ul >
< li > < a href = "#general-structure" > General Structure< / a > < / li >
< li > < a href = "#specific-class-requirements" > Specific Class Requirements< / a > < / li >
< li > < a href = "#executable-block" > Executable Block< / a > < / li >
< li > < a href = "#script-verification" > Script Verification< / a > < / li >
< li > < a href = "#example-script" > Example Script< / a > < / li >
< / ul >
< / li >
2023-04-14 11:44:56 -03:00
< li > < a href = "#http-api" > http API< / a > < ul >
< li > < a href = "#1-list-nodes" > 1. List Nodes< / a > < ul >
< li > < a href = "#request-body" > Request Body:< / a > < / li >
< li > < a href = "#response" > Response:< / a > < / li >
< / ul >
< / li >
2023-05-05 13:41:32 -03:00
< li > < a href = "#2-get-nodes" > 2. Get Nodes< / a > < ul >
2023-04-14 11:44:56 -03:00
< li > < a href = "#request-body_1" > Request Body:< / a > < / li >
< li > < a href = "#response_1" > Response:< / a > < / li >
< / ul >
< / li >
2023-05-05 13:41:32 -03:00
< li > < a href = "#3-run-commands" > 3. Run Commands< / a > < ul >
< li > < a href = "#request-body_2" > Request Body:< / a > < / li >
< li > < a href = "#response_2" > Response:< / a > < / li >
< / ul >
< / li >
< li > < a href = "#4-ask-ai" > 4. Ask AI< / a > < ul >
< li > < a href = "#request-body_3" > Request Body:< / a > < / li >
< li > < a href = "#response_3" > Response:< / a > < / li >
< / ul >
< / li >
2023-04-14 11:44:56 -03:00
< / ul >
< / li >
2022-04-03 18:25:58 -03:00
< li > < a href = "#automation-module" > Automation module< / a > < ul >
< li > < a href = "#standalone-module" > Standalone module< / a > < / li >
< li > < a href = "#using-manager-configuration" > Using manager configuration< / a > < / li >
< li > < a href = "#running-parallel-tasks" > Running parallel tasks< / a > < / li >
2022-04-28 12:49:31 -03:00
< li > < a href = "#using-variables" > Using variables< / a > < / li >
2023-05-05 13:41:32 -03:00
< li > < a href = "#using-ai" > Using AI< / a > < / li >
2022-04-03 18:25:58 -03:00
< / ul >
< / li >
2022-04-03 12:00:35 -03:00
< / ul >
2022-04-03 09:38:00 -03:00
< / div >
< ul id = "index" >
< li > < h3 > < a href = "#header-classes" > Classes< / a > < / h3 >
< ul >
< li >
2023-12-15 12:27:51 -03:00
< h4 > < code > < a title = "connpy.Plugins" href = "#connpy.Plugins" > Plugins< / a > < / code > < / h4 >
< ul class = "" >
< li > < code > < a title = "connpy.Plugins.verify_script" href = "#connpy.Plugins.verify_script" > verify_script< / a > < / code > < / li >
< / ul >
< / li >
< li >
2023-05-05 13:41:32 -03:00
< h4 > < code > < a title = "connpy.ai" href = "#connpy.ai" > ai< / a > < / code > < / h4 >
< ul class = "" >
< li > < code > < a title = "connpy.ai.ask" href = "#connpy.ai.ask" > ask< / a > < / code > < / li >
2023-05-12 12:05:25 -03:00
< li > < code > < a title = "connpy.ai.confirm" href = "#connpy.ai.confirm" > confirm< / a > < / code > < / li >
2023-05-23 16:15:03 -03:00
< li > < code > < a title = "connpy.ai.process_string" href = "#connpy.ai.process_string" > process_string< / a > < / code > < / li >
2023-05-05 13:41:32 -03:00
< / ul >
< / li >
< li >
2022-04-04 11:40:51 -03:00
< h4 > < code > < a title = "connpy.configfile" href = "#connpy.configfile" > configfile< / a > < / code > < / h4 >
2022-04-03 09:38:00 -03:00
< ul class = "" >
2022-04-04 11:40:51 -03:00
< li > < code > < a title = "connpy.configfile.getitem" href = "#connpy.configfile.getitem" > getitem< / a > < / code > < / li >
2023-05-05 13:41:32 -03:00
< li > < code > < a title = "connpy.configfile.getitems" href = "#connpy.configfile.getitems" > getitems< / a > < / code > < / li >
2022-04-03 18:25:58 -03:00
< / ul >
< / li >
< li >
2022-04-04 11:40:51 -03:00
< h4 > < code > < a title = "connpy.connapp" href = "#connpy.connapp" > connapp< / a > < / code > < / h4 >
2022-04-03 18:25:58 -03:00
< ul class = "" >
2022-04-04 11:40:51 -03:00
< li > < code > < a title = "connpy.connapp.encrypt" href = "#connpy.connapp.encrypt" > encrypt< / a > < / code > < / li >
2022-05-19 16:11:41 -03:00
< li > < code > < a title = "connpy.connapp.start" href = "#connpy.connapp.start" > start< / a > < / code > < / li >
2022-04-03 09:38:00 -03:00
< / ul >
< / li >
< li >
2022-04-04 11:40:51 -03:00
< h4 > < code > < a title = "connpy.node" href = "#connpy.node" > node< / a > < / code > < / h4 >
2022-04-03 09:38:00 -03:00
< ul class = "" >
2022-04-04 11:40:51 -03:00
< li > < code > < a title = "connpy.node.interact" href = "#connpy.node.interact" > interact< / a > < / code > < / li >
< li > < code > < a title = "connpy.node.run" href = "#connpy.node.run" > run< / a > < / code > < / li >
< li > < code > < a title = "connpy.node.test" href = "#connpy.node.test" > test< / a > < / code > < / li >
2022-04-03 09:38:00 -03:00
< / ul >
< / li >
< li >
2022-04-04 11:40:51 -03:00
< h4 > < code > < a title = "connpy.nodes" href = "#connpy.nodes" > nodes< / a > < / code > < / h4 >
2022-04-03 09:38:00 -03:00
< ul class = "" >
2022-04-04 11:40:51 -03:00
< li > < code > < a title = "connpy.nodes.run" href = "#connpy.nodes.run" > run< / a > < / code > < / li >
< li > < code > < a title = "connpy.nodes.test" href = "#connpy.nodes.test" > test< / a > < / code > < / li >
2022-04-03 09:38:00 -03:00
< / ul >
< / li >
< / ul >
< / li >
< / ul >
< / nav >
< / main >
< footer id = "footer" >
< p > Generated by < a href = "https://pdoc3.github.io/pdoc" title = "pdoc: Python API documentation generator" > < cite > pdoc< / cite > 0.10.0< / a > .< / p >
< / footer >
< / body >
< / html >