connpy


Nameconnpy JSON
Version 4.1.1 PyPI version JSON
download
home_pagehttps://github.com/fluzzi/connpy
SummaryConnpy is a SSH/Telnet connection manager and automation module
upload_time2024-07-21 21:41:10
maintainerNone
docs_urlNone
authorFederico Luzzi
requires_pythonNone
licenseCustom Software License
keywords networking automation docker kubernetes ssh telnet connection manager
VCS
bugtrack_url
requirements Flask Flask_Cors google_api_python_client google_auth_oauthlib inquirer openai pexpect protobuf pycryptodome pyfzf PyYAML rich waitress
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
  <img src="https://nginx.gederico.dynu.net/images/CONNPY-resized.png" alt="App Logo">
</p>


# Connpy
[![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
[![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)
[![](https://img.shields.io/pypi/l/connpy.svg?style=flat-square)](https://github.com/fluzzi/connpy/blob/main/LICENSE)
[![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)

Connpy is a SSH, SFTP, Telnet, kubectl, and Docker pod connection manager and automation module for Linux, Mac, and Docker.


## Installation

pip install connpy

### Run it in Windows using docker
```
git clone https://github.com/fluzzi/connpy
docker compose -f path/to/folder/docker-compose.yml build
docker compose -f path/to/folder/docker-compose.yml run -it connpy-app
```

## Connection manager 
### Privacy Policy

Connpy is committed to protecting your privacy. Our privacy policy explains how we handle user data:

- **Data Access**: Connpy accesses data necessary for managing remote host connections, including server addresses, usernames, and passwords. This data is stored locally on your machine and is not transmitted or shared with any third parties.
- **Data Usage**: User data is used solely for the purpose of managing and automating SSH and Telnet connections.
- **Data Storage**: All connection details are stored locally and securely on your device. We do not store or process this data on our servers.
- **Data Sharing**: We do not share any user data with third parties.

### Google Integration

Connpy integrates with Google services for backup purposes:

- **Configuration Backup**: The app allows users to store their device information in the app configuration. This configuration can be synced with Google services to create backups.
- **Data Access**: Connpy only accesses its own files and does not access any other files on your Google account.
- **Data Usage**: The data is used solely for backup and restore purposes, ensuring that your device information and configurations are safe and recoverable.
- **Data Sharing**: Connpy does not share any user data with third parties, including Google. The backup data is only accessible by the user.

For more detailed information, please read our [Privacy Policy](https://connpy.gederico.dynu.net/fluzzi32/connpy/src/branch/main/PRIVATE_POLICY.md).


### Features
    - Manage connections using SSH, SFTP, Telnet, kubectl, and Docker exec.
    - Set contexts to manage specific nodes from specific contexts (work/home/clients/etc).
    - You can generate profiles and reference them from nodes using @profilename so you don't
      need to edit multiple nodes when changing passwords or other information.
    - Nodes can be stored on @folder or @subfolder@folder to organize your devices. They can
      be referenced using node@subfolder@folder or node@folder.
    - If you have too many nodes, get a completion script using: conn config --completion.
      Or use fzf by installing pyfzf and running conn config --fzf true.
    - Create in bulk, copy, move, export, and import nodes for easy management.
    - Run automation scripts on network devices.
    - Use GPT AI to help you manage your devices.
    - Add plugins with your own scripts.
    - Much more!

### Usage:
```
usage: conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]
       conn {profile,move,mv,copy,cp,list,ls,bulk,export,import,ai,run,api,plugin,config,sync,context} ...

positional arguments:
  node|folder        node[@subfolder][@folder]
                     Connect to specific node or show all matching nodes
                     [@subfolder][@folder]
                     Show all available connections globally or in specified path

options:
  -h, --help         show this help message and exit
  -v, --version      Show version
  -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
  -t, --sftp         Connects using sftp instead of ssh

Commands:
  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
  sync            Sync config with Google
  context         Manage contexts with regex matching
```

### Manage profiles:
```
usage: conn profile [-h] (--add | --del | --mod | --show) profile

positional arguments:
  profile        Name of profile to manage

options:
  -h, --help         show this help message and exit
  -a, --add          Add new profile
  -r, --del, --rm    Delete profile
  -e, --mod, --edit  Modify profile
  -s, --show         Show profile

```

### Examples:
```
   #Add new profile
   conn profile --add office-user
   #Add new folder
   conn --add @office
   #Add new subfolder
   conn --add @datacenter@office
   #Add node to subfolder
   conn --add server@datacenter@office
   #Add node to folder
   conn --add pc@office
   #Show node information
   conn --show server@datacenter@office
   #Connect to nodes
   conn pc@office
   conn server
   #Create and set new context
   conn context -a office .*@office
   conn context --set office
   #Run a command in a node
   conn run server ls -la
``` 
## 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 specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture:
  1. **Class `Parser`**:
     - **Purpose**: Handles parsing of command-line arguments.
     - **Requirements**:
       - 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`**:
     - **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application.
     - **Requirements**:
       - 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.
  3. **Class `Preload`**:
     - **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic.
   - **Requirements**:
     - Contains at least an `__init__` method that accepts parameter connapp besides `self`.

### Class Dependencies and Combinations
- **Dependencies**:
  - `Parser` and `Entrypoint` are interdependent and must both be present if one is included.
  - `Preload` is independent and may exist alone or alongside the other classes.
- **Valid Combinations**:
  - `Parser` and `Entrypoint` together.
  - `Preload` alone.
  - All three classes (`Parser`, `Entrypoint`, `Preload`).

### Preload Modifications and Hooks

In the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs.

#### Modifying Classes with `modify`
The `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions.

- **Usage**: Modify a class to include additional configurations or changes
- **Modify Method Signature**:
  - `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance.
- **Modification Method Signature**:
  - **Arguments**:
    - `cls`:  This function accepts a single argument, the class instance, which it then modifies.
  - **Modifiable Classes**:
    - `connapp.config`
    - `connapp.node`
    - `connapp.nodes`
    - `connapp.ai`
  - ```python
    def modify_config(cls):
        # Example modification: adding a new attribute or modifying an existing one
        cls.new_attribute = 'New Value'

    class Preload:
        def __init__(self, connapp):
            # Applying modification to the config class instance
            connapp.config.modify(modify_config)
    ```

#### Implementing Method Hooks
There are 2 methods that allows you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities.

  - **Usage**: Register hooks to methods to execute additional logic before or after the main method execution.
- **Registration Methods Signature**:
  - `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments.
  - `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs.
- **Method Signatures for Pre-Hooks**
  - `pre_hook_method(*args, **kwargs)`
  - **Arguments**:
    - `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method.
  - **Return**:
    - Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received.
- **Method Signatures for Post-Hooks**:
  - `post_hook_method(*args, **kwargs)`
  - **Arguments**:
    - `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method.
      - `kwargs["result"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller.
  - **Return**:
    - Can return a modified result, which will replace the original result of the main method, or simply return `kwargs["result"]` to return the original method result.    
  - ```python
    def pre_processing_hook(*args, **kwargs):
        print("Pre-processing logic here")
        # Modify arguments or perform any checks
        return args, kwargs  # Return modified or unmodified args and kwargs

    def post_processing_hook(*args, **kwargs):
        print("Post-processing logic here")
        # Modify the result or perform any final logging or cleanup
        return kwargs["result"]  # Return the modified or unmodified result

    class Preload:
        def __init__(self, connapp):
            # Registering a pre-hook
            connapp.ai.some_method.register_pre_hook(pre_processing_hook)

            # Registering a post-hook
            connapp.node.another_method.register_post_hook(post_processing_hook)
    ```
  

### 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.

## Automation module usage
### Standalone module
```
import connpy
router = connpy.node("uniqueName","ip/host", user="username", password="password")
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:
    print("router does not have ip 1.1.1.1")
```

### Using manager configuration
```
import connpy
conf = connpy.configfile()
device = conf.getitem("router@office")
router = connpy.node("unique name", **device, config=conf)
result = router.run("show ip int brief")
print(result)
```
### Running parallel tasks on multiple devices 
```
import connpy
conf = connpy.configfile()
#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 = {}
nodes["router1"] = {"host": "1.1.1.1", "user": "user", "password": "password1"}
nodes["router2"] = {"host": "1.1.1.2", "user": "user", "password": "password2"}
nodes["router3"] = {"host": "1.1.1.2", "user": "user", "password": "password3"}
#Finally you run some tasks on the nodes
mynodes = connpy.nodes(nodes, config = conf)
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")
```
### 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 = {}
variables["router1@office"] = {"ip": "10.57.57.1"}
variables["router2@office"] = {"ip": "10.57.57.2"}
variables["router3@office"] = {"ip": "10.57.57.3"}
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"))
```
### Using AI
```
import connpy
conf = connpy.configfile()
organization = 'openai-org'
api_key = "openai-key"
myia = connpy.ai(conf, organization, api_key)
input = "go to router 1 and get me the full configuration"
result = myia.ask(input, dryrun = False)
print(result)
```
## 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.

---

### 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

**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.

---

### 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.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/fluzzi/connpy",
    "name": "connpy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "networking, automation, docker, kubernetes, ssh, telnet, connection manager",
    "author": "Federico Luzzi",
    "author_email": "fluzzi@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/76/37/e2f55832907ec266259edcb470aa965263bce2e7fcb2d5b1ac36398eb432/connpy-4.1.1.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n  <img src=\"https://nginx.gederico.dynu.net/images/CONNPY-resized.png\" alt=\"App Logo\">\n</p>\n\n\n# Connpy\n[![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)\n[![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)\n[![](https://img.shields.io/pypi/l/connpy.svg?style=flat-square)](https://github.com/fluzzi/connpy/blob/main/LICENSE)\n[![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/)\n\nConnpy is a SSH, SFTP, Telnet, kubectl, and Docker pod connection manager and automation module for Linux, Mac, and Docker.\n\n\n## Installation\n\npip install connpy\n\n### Run it in Windows using docker\n```\ngit clone https://github.com/fluzzi/connpy\ndocker compose -f path/to/folder/docker-compose.yml build\ndocker compose -f path/to/folder/docker-compose.yml run -it connpy-app\n```\n\n## Connection manager \n### Privacy Policy\n\nConnpy is committed to protecting your privacy. Our privacy policy explains how we handle user data:\n\n- **Data Access**: Connpy accesses data necessary for managing remote host connections, including server addresses, usernames, and passwords. This data is stored locally on your machine and is not transmitted or shared with any third parties.\n- **Data Usage**: User data is used solely for the purpose of managing and automating SSH and Telnet connections.\n- **Data Storage**: All connection details are stored locally and securely on your device. We do not store or process this data on our servers.\n- **Data Sharing**: We do not share any user data with third parties.\n\n### Google Integration\n\nConnpy integrates with Google services for backup purposes:\n\n- **Configuration Backup**: The app allows users to store their device information in the app configuration. This configuration can be synced with Google services to create backups.\n- **Data Access**: Connpy only accesses its own files and does not access any other files on your Google account.\n- **Data Usage**: The data is used solely for backup and restore purposes, ensuring that your device information and configurations are safe and recoverable.\n- **Data Sharing**: Connpy does not share any user data with third parties, including Google. The backup data is only accessible by the user.\n\nFor more detailed information, please read our [Privacy Policy](https://connpy.gederico.dynu.net/fluzzi32/connpy/src/branch/main/PRIVATE_POLICY.md).\n\n\n### Features\n    - Manage connections using SSH, SFTP, Telnet, kubectl, and Docker exec.\n    - Set contexts to manage specific nodes from specific contexts (work/home/clients/etc).\n    - You can generate profiles and reference them from nodes using @profilename so you don't\n      need to edit multiple nodes when changing passwords or other information.\n    - Nodes can be stored on @folder or @subfolder@folder to organize your devices. They can\n      be referenced using node@subfolder@folder or node@folder.\n    - If you have too many nodes, get a completion script using: conn config --completion.\n      Or use fzf by installing pyfzf and running conn config --fzf true.\n    - Create in bulk, copy, move, export, and import nodes for easy management.\n    - Run automation scripts on network devices.\n    - Use GPT AI to help you manage your devices.\n    - Add plugins with your own scripts.\n    - Much more!\n\n### Usage:\n```\nusage: conn [-h] [--add | --del | --mod | --show | --debug] [node|folder] [--sftp]\n       conn {profile,move,mv,copy,cp,list,ls,bulk,export,import,ai,run,api,plugin,config,sync,context} ...\n\npositional arguments:\n  node|folder        node[@subfolder][@folder]\n                     Connect to specific node or show all matching nodes\n                     [@subfolder][@folder]\n                     Show all available connections globally or in specified path\n\noptions:\n  -h, --help         show this help message and exit\n  -v, --version      Show version\n  -a, --add          Add new node[@subfolder][@folder] or [@subfolder]@folder\n  -r, --del, --rm    Delete node[@subfolder][@folder] or [@subfolder]@folder\n  -e, --mod, --edit  Modify node[@subfolder][@folder]\n  -s, --show         Show node[@subfolder][@folder]\n  -d, --debug        Display all conections steps\n  -t, --sftp         Connects using sftp instead of ssh\n\nCommands:\n  profile         Manage profiles\n  move(mv)        Move node\n  copy(cp)        Copy node\n  list(ls)        List profiles, nodes or folders\n  bulk            Add nodes in bulk\n  export          Export connection folder to Yaml file\n  import          Import connection folder to config from Yaml file\n  ai              Make request to an AI\n  run             Run scripts or commands on nodes\n  api             Start and stop connpy api\n  plugin          Manage plugins\n  config          Manage app config\n  sync            Sync config with Google\n  context         Manage contexts with regex matching\n```\n\n### Manage profiles:\n```\nusage: conn profile [-h] (--add | --del | --mod | --show) profile\n\npositional arguments:\n  profile        Name of profile to manage\n\noptions:\n  -h, --help         show this help message and exit\n  -a, --add          Add new profile\n  -r, --del, --rm    Delete profile\n  -e, --mod, --edit  Modify profile\n  -s, --show         Show profile\n\n```\n\n### Examples:\n```\n   #Add new profile\n   conn profile --add office-user\n   #Add new folder\n   conn --add @office\n   #Add new subfolder\n   conn --add @datacenter@office\n   #Add node to subfolder\n   conn --add server@datacenter@office\n   #Add node to folder\n   conn --add pc@office\n   #Show node information\n   conn --show server@datacenter@office\n   #Connect to nodes\n   conn pc@office\n   conn server\n   #Create and set new context\n   conn context -a office .*@office\n   conn context --set office\n   #Run a command in a node\n   conn run server ls -la\n``` \n## Plugin Requirements for Connpy\n\n### General Structure\n- The plugin script must be a Python file.\n- Only the following top-level elements are allowed in the plugin script:\n  - Class definitions\n  - Function definitions\n  - Import statements\n  - The `if __name__ == \"__main__\":` block for standalone execution\n  - Pass statements\n\n### Specific Class Requirements\n- The plugin script must define specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture:\n  1. **Class `Parser`**:\n     - **Purpose**: Handles parsing of command-line arguments.\n     - **Requirements**:\n       - Must contain only one method: `__init__`.\n       - The `__init__` method must initialize at least two attributes:\n         - `self.parser`: An instance of `argparse.ArgumentParser`.\n         - `self.description`: A string containing the description of the parser.\n  2. **Class `Entrypoint`**:\n     - **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application.\n     - **Requirements**:\n       - Must have an `__init__` method that accepts exactly three parameters besides `self`:\n         - `args`: Arguments passed to the plugin.\n         - The parser instance (typically `self.parser` from the `Parser` class).\n         - The Connapp instance to interact with the Connpy app.\n  3. **Class `Preload`**:\n     - **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic.\n   - **Requirements**:\n     - Contains at least an `__init__` method that accepts parameter connapp besides `self`.\n\n### Class Dependencies and Combinations\n- **Dependencies**:\n  - `Parser` and `Entrypoint` are interdependent and must both be present if one is included.\n  - `Preload` is independent and may exist alone or alongside the other classes.\n- **Valid Combinations**:\n  - `Parser` and `Entrypoint` together.\n  - `Preload` alone.\n  - All three classes (`Parser`, `Entrypoint`, `Preload`).\n\n### Preload Modifications and Hooks\n\nIn the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs.\n\n#### Modifying Classes with `modify`\nThe `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions.\n\n- **Usage**: Modify a class to include additional configurations or changes\n- **Modify Method Signature**:\n  - `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance.\n- **Modification Method Signature**:\n  - **Arguments**:\n    - `cls`:  This function accepts a single argument, the class instance, which it then modifies.\n  - **Modifiable Classes**:\n    - `connapp.config`\n    - `connapp.node`\n    - `connapp.nodes`\n    - `connapp.ai`\n  - ```python\n    def modify_config(cls):\n        # Example modification: adding a new attribute or modifying an existing one\n        cls.new_attribute = 'New Value'\n\n    class Preload:\n        def __init__(self, connapp):\n            # Applying modification to the config class instance\n            connapp.config.modify(modify_config)\n    ```\n\n#### Implementing Method Hooks\nThere are 2 methods that allows you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities.\n\n  - **Usage**: Register hooks to methods to execute additional logic before or after the main method execution.\n- **Registration Methods Signature**:\n  - `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments.\n  - `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs.\n- **Method Signatures for Pre-Hooks**\n  - `pre_hook_method(*args, **kwargs)`\n  - **Arguments**:\n    - `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method.\n  - **Return**:\n    - Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received.\n- **Method Signatures for Post-Hooks**:\n  - `post_hook_method(*args, **kwargs)`\n  - **Arguments**:\n    - `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method.\n      - `kwargs[\"result\"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller.\n  - **Return**:\n    - Can return a modified result, which will replace the original result of the main method, or simply return `kwargs[\"result\"]` to return the original method result.    \n  - ```python\n    def pre_processing_hook(*args, **kwargs):\n        print(\"Pre-processing logic here\")\n        # Modify arguments or perform any checks\n        return args, kwargs  # Return modified or unmodified args and kwargs\n\n    def post_processing_hook(*args, **kwargs):\n        print(\"Post-processing logic here\")\n        # Modify the result or perform any final logging or cleanup\n        return kwargs[\"result\"]  # Return the modified or unmodified result\n\n    class Preload:\n        def __init__(self, connapp):\n            # Registering a pre-hook\n            connapp.ai.some_method.register_pre_hook(pre_processing_hook)\n\n            # Registering a post-hook\n            connapp.node.another_method.register_post_hook(post_processing_hook)\n    ```\n  \n\n### Executable Block\n- The plugin script can include an executable block:\n  - `if __name__ == \"__main__\":`\n  - This block allows the plugin to be run as a standalone script for testing or independent use.\n\n### Script Verification\n- The `verify_script` method in `plugins.py` is used to check the plugin script's compliance with these standards.\n- Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system.\n \n### Example Script\n\nFor a practical example of how to write a compatible plugin script, please refer to the following example:\n\n[Example Plugin Script](https://github.com/fluzzi/awspy)\n\nThis script demonstrates the required structure and implementation details according to the plugin system's standards.\n\n## Automation module usage\n### Standalone module\n```\nimport connpy\nrouter = connpy.node(\"uniqueName\",\"ip/host\", user=\"username\", password=\"password\")\nrouter.run([\"term len 0\",\"show run\"])\nprint(router.output)\nhasip = router.test(\"show ip int brief\",\"1.1.1.1\")\nif hasip:\n    print(\"Router has ip 1.1.1.1\")\nelse:\n    print(\"router does not have ip 1.1.1.1\")\n```\n\n### Using manager configuration\n```\nimport connpy\nconf = connpy.configfile()\ndevice = conf.getitem(\"router@office\")\nrouter = connpy.node(\"unique name\", **device, config=conf)\nresult = router.run(\"show ip int brief\")\nprint(result)\n```\n### Running parallel tasks on multiple devices \n```\nimport connpy\nconf = connpy.configfile()\n#You can get the nodes from the config from a folder and fitlering in it\nnodes = conf.getitem(\"@office\", [\"router1\", \"router2\", \"router3\"])\n#You can also get each node individually:\nnodes = {}\nnodes[\"router1\"] = conf.getitem(\"router1@office\")\nnodes[\"router2\"] = conf.getitem(\"router2@office\")\nnodes[\"router10\"] = conf.getitem(\"router10@datacenter\")\n#Also, you can create the nodes manually:\nnodes = {}\nnodes[\"router1\"] = {\"host\": \"1.1.1.1\", \"user\": \"user\", \"password\": \"password1\"}\nnodes[\"router2\"] = {\"host\": \"1.1.1.2\", \"user\": \"user\", \"password\": \"password2\"}\nnodes[\"router3\"] = {\"host\": \"1.1.1.2\", \"user\": \"user\", \"password\": \"password3\"}\n#Finally you run some tasks on the nodes\nmynodes = connpy.nodes(nodes, config = conf)\nresult = mynodes.test([\"show ip int br\"], \"1.1.1.2\")\nfor i in result:\n    print(\"---\" + i + \"---\")\n    print(result[i])\n    print()\n# Or for one specific node\nmynodes.router1.run([\"term len 0\". \"show run\"], folder = \"/home/user/logs\")\n```\n### Using variables\n```\nimport connpy\nconfig = connpy.configfile()\nnodes = config.getitem(\"@office\", [\"router1\", \"router2\", \"router3\"])\ncommands = []\ncommands.append(\"config t\")\ncommands.append(\"interface lo {id}\")\ncommands.append(\"ip add {ip} {mask}\")\ncommands.append(\"end\")\nvariables = {}\nvariables[\"router1@office\"] = {\"ip\": \"10.57.57.1\"}\nvariables[\"router2@office\"] = {\"ip\": \"10.57.57.2\"}\nvariables[\"router3@office\"] = {\"ip\": \"10.57.57.3\"}\nvariables[\"__global__\"] = {\"id\": \"57\"}\nvariables[\"__global__\"][\"mask\"] =  \"255.255.255.255\"\nexpected = \"!\"\nrouters = connpy.nodes(nodes, config = config)\nrouters.run(commands, variables)\nrouters.test(\"ping {ip}\", expected, variables)\nfor key in routers.result:\n    print(key, ' ---> ', (\"pass\" if routers.result[key] else \"fail\"))\n```\n### Using AI\n```\nimport connpy\nconf = connpy.configfile()\norganization = 'openai-org'\napi_key = \"openai-key\"\nmyia = connpy.ai(conf, organization, api_key)\ninput = \"go to router 1 and get me the full configuration\"\nresult = myia.ask(input, dryrun = False)\nprint(result)\n```\n## http API\nWith the Connpy API you can run commands on devices using http requests\n\n### 1. List Nodes\n\n**Endpoint**: `/list_nodes`\n\n**Method**: `POST`\n\n**Description**: This route returns a list of nodes. It can also filter the list based on a given keyword.\n\n#### Request Body:\n\n```json\n{\n  \"filter\": \"<keyword>\"\n}\n```\n\n* `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.\n\n#### Response:\n\n- A JSON array containing the filtered list of nodes.\n\n---\n\n### 2. Get Nodes\n\n**Endpoint**: `/get_nodes`\n\n**Method**: `POST`\n\n**Description**: This route returns a dictionary of nodes with all their attributes. It can also filter the nodes based on a given keyword.\n\n#### Request Body:\n\n```json\n{\n  \"filter\": \"<keyword>\"\n}\n```\n\n* `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.\n\n#### Response:\n\n- A JSON array containing the filtered nodes.\n\n---\n\n### 3. Run Commands\n\n**Endpoint**: `/run_commands`\n\n**Method**: `POST`\n\n**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.\n\n#### Request Body:\n\n```json\n{\n  \"action\": \"<action>\",\n  \"nodes\": \"<nodes>\",\n  \"commands\": \"<commands>\",\n  \"expected\": \"<expected>\",\n  \"options\": \"<options>\"\n}\n```\n\n* `action` (required): The action to be performed. Possible values: `run` or `test`.\n* `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.\n* `commands` (required): A list of commands to be executed on the specified nodes.\n* `expected` (optional, only used when the action is `test`): A single expected result for the test.\n* `options` (optional): Array to pass options to the run command, options are: `prompt`, `parallel`, `timeout`  \n\n#### Response:\n\n- A JSON object with the results of the executed commands on the nodes.\n\n---\n\n### 4. Ask AI\n\n**Endpoint**: `/ask_ai`\n\n**Method**: `POST`\n\n**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.\n\n#### Request Body:\n\n```json\n{\n  \"input\": \"<user input request>\",\n  \"dryrun\": true or false\n}\n```\n\n* `input` (required): The user input requesting the AI to perform an action on some devices or get the devices list.\n* `dryrun` (optional): If set to true, it will return the parameters to run the request but it won't run it. default is false.\n\n#### Response:\n\n- A JSON array containing the action to run and the parameters and the result of the action.\n\n\n",
    "bugtrack_url": null,
    "license": "Custom Software License",
    "summary": "Connpy is a SSH/Telnet connection manager and automation module",
    "version": "4.1.1",
    "project_urls": {
        "Bug Tracker": "https://github.com/fluzzi/connpy/issues",
        "Documentation": "https://fluzzi.github.io/connpy/",
        "Homepage": "https://github.com/fluzzi/connpy"
    },
    "split_keywords": [
        "networking",
        " automation",
        " docker",
        " kubernetes",
        " ssh",
        " telnet",
        " connection manager"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ee3d4e9246651d38a75225e167375bb8991d860a367d8088cdde3a1bdc9cda31",
                "md5": "1d76270da64cced41354e3a1415a952a",
                "sha256": "5b2b40ac5f931ecc4ca430fe104bbff7b808274560bf14394688e0510588bdfe"
            },
            "downloads": -1,
            "filename": "connpy-4.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1d76270da64cced41354e3a1415a952a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 59597,
            "upload_time": "2024-07-21T21:41:08",
            "upload_time_iso_8601": "2024-07-21T21:41:08.317641Z",
            "url": "https://files.pythonhosted.org/packages/ee/3d/4e9246651d38a75225e167375bb8991d860a367d8088cdde3a1bdc9cda31/connpy-4.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7637e2f55832907ec266259edcb470aa965263bce2e7fcb2d5b1ac36398eb432",
                "md5": "274ce6a78d60e2eaaa606e8aadb52cc1",
                "sha256": "5e9ca2e14ff717fa2c52cc8105eb7d4f3755f95e4e4a9e2f43399ea86dc9d856"
            },
            "downloads": -1,
            "filename": "connpy-4.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "274ce6a78d60e2eaaa606e8aadb52cc1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 56126,
            "upload_time": "2024-07-21T21:41:10",
            "upload_time_iso_8601": "2024-07-21T21:41:10.063173Z",
            "url": "https://files.pythonhosted.org/packages/76/37/e2f55832907ec266259edcb470aa965263bce2e7fcb2d5b1ac36398eb432/connpy-4.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-21 21:41:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fluzzi",
    "github_project": "connpy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "Flask",
            "specs": [
                [
                    ">=",
                    "2.3.2"
                ]
            ]
        },
        {
            "name": "Flask_Cors",
            "specs": [
                [
                    ">=",
                    "4.0.1"
                ]
            ]
        },
        {
            "name": "google_api_python_client",
            "specs": [
                [
                    ">=",
                    "2.125.0"
                ]
            ]
        },
        {
            "name": "google_auth_oauthlib",
            "specs": [
                [
                    ">=",
                    "1.2.0"
                ]
            ]
        },
        {
            "name": "inquirer",
            "specs": [
                [
                    ">=",
                    "3.3.0"
                ]
            ]
        },
        {
            "name": "openai",
            "specs": [
                [
                    ">=",
                    "0.27.8"
                ]
            ]
        },
        {
            "name": "pexpect",
            "specs": [
                [
                    ">=",
                    "4.8.0"
                ]
            ]
        },
        {
            "name": "protobuf",
            "specs": [
                [
                    ">=",
                    "5.27.2"
                ]
            ]
        },
        {
            "name": "pycryptodome",
            "specs": [
                [
                    ">=",
                    "3.18.0"
                ]
            ]
        },
        {
            "name": "pyfzf",
            "specs": [
                [
                    ">=",
                    "0.3.1"
                ]
            ]
        },
        {
            "name": "PyYAML",
            "specs": [
                [
                    ">=",
                    "6.0.1"
                ]
            ]
        },
        {
            "name": "rich",
            "specs": [
                [
                    ">=",
                    "13.7.1"
                ]
            ]
        },
        {
            "name": "waitress",
            "specs": [
                [
                    ">=",
                    "2.1.2"
                ]
            ]
        }
    ],
    "lcname": "connpy"
}
        
Elapsed time: 1.23365s