Skip to main content

Asynchronous SSH handler based on Netmiko for managing network devices.

Project description

netmikio

A lightweight package for asynchronous SSH-handling based on Netmiko for managing network devices.
Built on top of netmiko by Kirk Byers.

Important Note

While netmikio provides an asynchronous interface for interacting with network devices, it is important to understand that it is not fully asynchronous. The underlying netmiko library is synchronous and blocking by design. This package uses asyncio to offload blocking operations to a thread pool, allowing you to manage multiple SSH connections concurrently, but the operations themselves are still blocking.

However, this package is particularly useful when you have other asynchronous tasks running in parallel, such as handling I/O operations, making API calls, or managing multiple coroutines. By integrating SSH operations within an asyncio event loop, you can still benefit from the concurrency provided by Python’s async ecosystem.

If you require true non-blocking, asynchronous SSH communication, consider using a library like asyncssh, which is built specifically for fully async SSH connections.

Features

  • Asynchronous interface for SSH operations using Python's asyncio with blocking operations offloaded to a thread pool.
  • Short and simplified method names similar to Netmiko.
  • Handles connection management, timeouts, and authentication errors in an asynchronous context.

How Does It Work?

netmikio follows a similar approach to netmiko but introduces shorter, simplified method names:

  • send_com(): Send a command and retrieve the output.
  • send_com_timing(): Send a command with timing support.
  • send_conf_set(): Send a configuration command set.

Example: Sending the command show power inline to multiple devices asynchronously

from netmikio import aioConnectHandler
import asyncio

async def my_func(dev):
    """
    Send a command and gather the output.
    """
    async with aioConnectHandler(**dev) as ssh:  # Connect asynchronously like in Netmiko but with 'async'.
        output = await ssh.send_com('show power inline')  # Send command and retrieve output asynchronously.
    print(f"{dev['host']}:\n{output}\n")

async def main():
    """
    Main function where tasks are created and executed.
    """
    devices = [
        {
            'device_type': 'cisco_ios',
            'host': 'sw1.test.local',
            'username': 'admin',
            'password': '12345678'
        },
        {
            'device_type': 'cisco_ios',
            'host': 'sw2.test.local',
            'username': 'admin',
            'password': '12345678'
        }
    ]
    tasks = [my_func(dev) for dev in devices]  # Create tasks for each device.
    await asyncio.gather(*tasks)  # Run the tasks asynchronously.

if __name__ == '__main__':
    asyncio.run(main())  # Execute the main function.

Output:

sw1.test.local:
Available:240.0(w)  Used:137.8(w)  Remaining:102.2(w)

Interface Admin  Oper       Power   Device              Class Max
                            (Watts)
--------- ------ ---------- ------- ------------------- ----- ----
Gi0/1     auto   on         13.9    AIR-AP1542D-B-K9    3     30.0
Gi0/2     auto   on         13.9    AIR-AP1542D-B-K9    3     30.0
Gi0/3     auto   on         26.6    AIR-AP2802E-B-K9    4     30.0
Gi0/4     auto   on         13.9    AIR-AP1542D-B-K9    3     30.0
Gi0/5     auto   off        0.0     n/a                 n/a   30.0
Gi0/6     auto   on         13.9    Ieee PD             4     30.0
Gi0/7     auto   on         13.9    AIR-AP1542I-B-K9    3     30.0
Gi0/8     auto   off        0.0     n/a                 n/a   30.0
Gi0/9     auto   on         13.9    AIR-AP1542D-B-K9    3     30.0
Gi0/10    auto   on         13.9    AIR-AP1542I-B-K9    3     30.0
Gi0/11    auto   on         13.9    AIR-AP1542I-B-K9    3     30.0
Gi0/12    auto   off        0.0     n/a                 n/a   30.0

sw2.test.local:
Available:240.0(w)  Used:99.3(w)  Remaining:140.7(w)

Interface Admin  Oper       Power   Device              Class Max
                            (Watts)
--------- ------ ---------- ------- ------------------- ----- ----
Gi0/1     auto   on         13.9    AIR-AP1542I-B-K9    4     30.0
Gi0/2     auto   on         13.9    AIR-AP1542I-B-K9    3     30.0
Gi0/3     auto   on         29.8    AIR-AP1562D-B-K9    4     30.0
Gi0/4     auto   on         13.9    AIR-AP1542I-E-K9    3     30.0
Gi0/5     auto   on         13.9    AIR-AP1542I-B-K9    3     30.0
Gi0/6     auto   on         13.9    AIR-AP1542D-B-K9    3     30.0
Gi0/7     auto   off        0.0     n/a                 n/a   30.0
Gi0/8     auto   off        0.0     n/a                 n/a   30.0
Gi0/9     auto   off        0.0     n/a                 n/a   30.0
Gi0/10    auto   off        0.0     n/a                 n/a   30.0
Gi0/11    auto   off        0.0     n/a                 n/a   30.0
Gi0/12    auto   off        0.0     n/a                 n/a   30.0

thamuppet - Anton Hultberg
antonhultberg@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

netmikio-1.0.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

netmikio-1.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file netmikio-1.0.1.tar.gz.

File metadata

  • Download URL: netmikio-1.0.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.4

File hashes

Hashes for netmikio-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4fcc039352ec23465812609432699644b77bd1f06618d24cbfa438dea3c3b9b6
MD5 160bd38cf5190a8af332dea6c026f04b
BLAKE2b-256 7b2e5a99bf7df68f9d711cbb3f26141f5cadf4115196c7051af4d94a456d124a

See more details on using hashes here.

File details

Details for the file netmikio-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: netmikio-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.4

File hashes

Hashes for netmikio-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ddb510bdb2170a5b8895093042bf524918fb812e6f54e7c7a1a3eac050190c35
MD5 3e268c523f0dd22a2160d4fd663f4cf6
BLAKE2b-256 4cb2a30ddc136896bd609e6d83fceba56042c2449c5698391432ab8fdc34a757

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page