Convert Figma logo to code with AI

ronf logoasyncssh

AsyncSSH is a Python package which provides an asynchronous client and server implementation of the SSHv2 protocol on top of the Python asyncio framework.

1,633
163
1,633
20

Top Related Projects

The leading native Python SSHv2 protocol library.

12,440

Transparent proxy server that works as a poor man's VPN. Forwards over ssh. Doesn't require admin. Works with Linux and MacOS. Supports DNS tunneling.

65,475

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.

15,158

Simple, Pythonic remote execution and deployment.

14,544

Powerline is a statusline plugin for vim, and provides statuslines and prompts for several other applications, including zsh, bash, tmux, IPython, Awesome and Qtile.

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.

Quick Overview

AsyncSSH is a Python library that provides an asynchronous SSH client and server implementation. It is built on top of Python's asyncio framework and offers a high-performance, easy-to-use interface for SSH operations.

Pros

  • Fully asynchronous, allowing for efficient handling of multiple SSH connections
  • Supports both client and server implementations
  • Comprehensive feature set, including SFTP, SCP, and port forwarding
  • Well-documented with extensive examples

Cons

  • Requires Python 3.6 or later, which may not be suitable for older systems
  • Learning curve for developers not familiar with asyncio
  • Limited support for some advanced SSH features compared to OpenSSH

Code Examples

  1. Connecting to an SSH server and running a command:
import asyncio
import asyncssh

async def run_client():
    async with asyncssh.connect('example.com', username='user') as conn:
        result = await conn.run('ls -l')
        print(result.stdout, end='')

asyncio.run(run_client())
  1. Implementing a simple SSH server:
import asyncio
import asyncssh

class MySSHServer(asyncssh.SSHServer):
    def connection_made(self, conn):
        print('SSH connection received from', conn.get_extra_info('peername')[0])

    async def password_auth_supported(self):
        return True

    async def validate_password(self, username, password):
        return username == 'user' and password == 'password'

async def start_server():
    await asyncssh.create_server(MySSHServer, '', 8022,
                                 server_host_keys=['ssh_host_key'])

asyncio.run(start_server())
  1. Performing SFTP operations:
import asyncio
import asyncssh

async def sftp_operations():
    async with asyncssh.connect('example.com', username='user') as conn:
        async with conn.start_sftp_client() as sftp:
            await sftp.put('local_file.txt', 'remote_file.txt')
            await sftp.get('remote_file.txt', 'downloaded_file.txt')

asyncio.run(sftp_operations())

Getting Started

To get started with AsyncSSH, first install it using pip:

pip install asyncssh

Then, import the library and use it in your Python code:

import asyncio
import asyncssh

async def main():
    async with asyncssh.connect('example.com', username='user', password='password') as conn:
        result = await conn.run('echo "Hello, AsyncSSH!"')
        print(result.stdout)

asyncio.run(main())

This example connects to an SSH server, runs a simple command, and prints the output.

Competitor Comparisons

The leading native Python SSHv2 protocol library.

Pros of Paramiko

  • Mature and widely adopted library with extensive documentation
  • Supports both SSH client and server implementations
  • Offers a synchronous API, which can be easier for beginners

Cons of Paramiko

  • Lacks native support for asynchronous operations
  • May have performance limitations for high-concurrency scenarios
  • Less actively maintained compared to AsyncSSH

Code Comparison

Paramiko (synchronous):

import paramiko

client = paramiko.SSHClient()
client.connect('hostname', username='user', password='pass')
stdin, stdout, stderr = client.exec_command('ls -l')
print(stdout.read().decode())
client.close()

AsyncSSH (asynchronous):

import asyncio, asyncssh

async def run_client():
    async with asyncssh.connect('hostname', username='user', password='pass') as conn:
        result = await conn.run('ls -l')
        print(result.stdout)

asyncio.get_event_loop().run_until_complete(run_client())

AsyncSSH leverages Python's asyncio framework for non-blocking operations, while Paramiko uses a traditional synchronous approach. AsyncSSH's code structure reflects its asynchronous nature, using async/await syntax and context managers for connection handling.

12,440

Transparent proxy server that works as a poor man's VPN. Forwards over ssh. Doesn't require admin. Works with Linux and MacOS. Supports DNS tunneling.

Pros of sshuttle

  • Acts as a transparent proxy, allowing for easy tunneling of entire subnets
  • Supports DNS tunneling, enhancing privacy and security
  • Requires minimal setup on the remote side, often just SSH access

Cons of sshuttle

  • Limited to TCP traffic, doesn't support UDP or ICMP
  • May have higher latency compared to direct SSH connections
  • Requires root/admin privileges on the local machine for some features

Code Comparison

sshuttle:

def main():
    options, args = parse_args()
    if options.firewall:
        if options.subnets:
            print("Warning: subnets supplied with --firewall option are ignored.")
        return firewall(options)
    ...

asyncssh:

async def run_client():
    async with asyncssh.connect('localhost') as conn:
        result = await conn.run('ls')
        print(result.stdout, end='')

Key Differences

sshuttle focuses on creating a VPN-like tunnel over SSH, while asyncssh is a library for SSH and SFTP client and server implementations. sshuttle is more suited for network-level operations, whereas asyncssh is better for programmatic SSH interactions and file transfers.

65,475

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.

Pros of Ansible

  • Broader scope: Ansible is a complete IT automation platform, while AsyncSSH focuses solely on SSH functionality
  • Larger community and ecosystem: More plugins, modules, and third-party integrations available
  • Built-in inventory management and playbook system for orchestrating complex tasks

Cons of Ansible

  • Steeper learning curve due to its extensive feature set
  • Heavier resource usage, especially for small-scale operations
  • Less specialized for SSH operations compared to AsyncSSH's focused approach

Code Comparison

AsyncSSH:

async with asyncssh.connect('example.com') as conn:
    result = await conn.run('ls')
    print(result.stdout)

Ansible:

- name: List files
  hosts: example.com
  tasks:
    - name: Run ls command
      command: ls
      register: result
    - debug: var=result.stdout_lines

Summary

Ansible offers a comprehensive automation solution with a vast ecosystem, while AsyncSSH provides a more focused and lightweight approach to SSH operations. The choice between them depends on the specific needs of the project, with Ansible being better suited for large-scale infrastructure management and AsyncSSH excelling in scenarios requiring efficient, programmatic SSH interactions.

15,158

Simple, Pythonic remote execution and deployment.

Pros of Fabric

  • More comprehensive library for remote operations, including file transfers and system administration tasks
  • Extensive documentation and larger community support
  • Supports both SSH and local command execution

Cons of Fabric

  • Heavier and more complex than AsyncSSH
  • Slower performance for simple SSH operations
  • Less focus on asynchronous programming

Code Comparison

Fabric:

from fabric import Connection

with Connection('host') as c:
    result = c.run('uname -s')
    print(result.stdout.strip())

AsyncSSH:

import asyncio, asyncssh

async def run_client():
    async with asyncssh.connect('host') as conn:
        result = await conn.run('uname -s')
        print(result.stdout.strip())

asyncio.get_event_loop().run_until_complete(run_client())

Summary

Fabric is a more comprehensive tool for remote operations and system administration, with extensive documentation and community support. It's suitable for complex tasks involving multiple hosts and file transfers. However, it can be slower and more complex for simple SSH operations.

AsyncSSH, on the other hand, is lightweight and focused on asynchronous SSH operations. It offers better performance for simple SSH tasks and is more suitable for projects that require asynchronous programming. However, it has a smaller feature set compared to Fabric and may require additional libraries for more complex operations.

14,544

Powerline is a statusline plugin for vim, and provides statuslines and prompts for several other applications, including zsh, bash, tmux, IPython, Awesome and Qtile.

Pros of Powerline

  • Enhances the appearance of status lines in various applications (vim, tmux, shells)
  • Highly customizable with extensive theming options
  • Supports multiple programming languages and environments

Cons of Powerline

  • Can be complex to set up and configure
  • May require additional font installation for proper rendering
  • Performance impact on some systems, especially with complex configurations

Code Comparison

Powerline (Python):

from powerline import Powerline
powerline = Powerline()
powerline.add_segment('hostname', 'My Server')
print(powerline.render())

AsyncSSH (Python):

import asyncssh
import asyncio

async def run_client():
    async with asyncssh.connect('localhost') as conn:
        result = await conn.run('ls')
        print(result.stdout)

asyncio.get_event_loop().run_until_complete(run_client())

Summary

Powerline is a status line plugin that enhances the visual appearance of various applications, while AsyncSSH is a library for SSH and SFTP client and server operations. They serve different purposes, with Powerline focusing on UI enhancement and AsyncSSH on network communication. Powerline offers extensive customization but can be complex to set up, while AsyncSSH provides a straightforward way to implement SSH functionality in Python applications.

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.

Pros of cryptography

  • Broader scope: Provides a comprehensive set of cryptographic primitives and protocols
  • Larger community: More contributors and users, resulting in frequent updates and better support
  • Extensive documentation: Detailed API references and usage examples

Cons of cryptography

  • Steeper learning curve: More complex API due to its broader scope
  • Heavier dependency: Larger library size and potential for more dependencies

Code comparison

cryptography:

from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b"Secret message")

AsyncSSH:

import asyncssh
async def run_client():
    async with asyncssh.connect('localhost') as conn:
        result = await conn.run('echo "Hello, world!"')
        print(result.stdout, end='')

While cryptography offers a wide range of cryptographic operations, AsyncSSH focuses specifically on SSH functionality. cryptography provides low-level cryptographic primitives, while AsyncSSH offers high-level SSH client and server implementations. The choice between the two depends on the specific requirements of your project: use cryptography for general cryptographic needs, and AsyncSSH for SSH-specific tasks.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

.. image:: https://readthedocs.org/projects/asyncssh/badge/?version=latest :target: https://asyncssh.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status

.. image:: https://img.shields.io/pypi/v/asyncssh.svg :target: https://pypi.python.org/pypi/asyncssh/ :alt: AsyncSSH PyPI Project

AsyncSSH: Asynchronous SSH for Python

AsyncSSH is a Python package which provides an asynchronous client and server implementation of the SSHv2 protocol on top of the Python 3.6+ asyncio framework.

.. code:: python

import asyncio, asyncssh, sys

async def run_client(): async with asyncssh.connect('localhost') as conn: result = await conn.run('echo "Hello!"', check=True) print(result.stdout, end='')

try: asyncio.get_event_loop().run_until_complete(run_client()) except (OSError, asyncssh.Error) as exc: sys.exit('SSH connection failed: ' + str(exc))

Check out the examples__ to get started!

__ http://asyncssh.readthedocs.io/en/stable/#client-examples

Features

  • Full support for SSHv2, SFTP, and SCP client and server functions

    • Shell, command, and subsystem channels

    • Environment variables, terminal type, and window size

    • Direct and forwarded TCP/IP channels

    • OpenSSH-compatible direct and forwarded UNIX domain socket channels

    • OpenSSH-compatible TUN/TAP channels and packet forwarding

    • Local and remote TCP/IP port forwarding

    • Local and remote UNIX domain socket forwarding

    • Dynamic TCP/IP port forwarding via SOCKS

    • X11 forwarding support on both the client and the server

    • SFTP protocol version 3 with OpenSSH extensions

      • Experimental support for SFTP versions 4-6, when requested
    • SCP protocol support, including third-party remote to remote copies

  • Multiple simultaneous sessions on a single SSH connection

  • Multiple SSH connections in a single event loop

  • Byte and string based I/O with settable encoding

  • A variety of key exchange, encryption, and MAC__ algorithms

    • Including post-quantum kex algorithms ML-KEM and SNTRUP
  • Support for gzip compression__

    • Including OpenSSH variant to delay compression until after auth
  • User and host-based public key, password, and keyboard-interactive authentication methods

  • Many types and formats of public keys and certificates__

    • Including OpenSSH-compatible support for U2F and FIDO2 security keys
    • Including PKCS#11 support for accessing PIV security tokens
    • Including support for X.509 certificates as defined in RFC 6187
  • Support for accessing keys managed by ssh-agent__ on UNIX systems

    • Including agent forwarding support on both the client and the server
  • Support for accessing keys managed by PuTTY's Pageant agent on Windows

  • Support for accessing host keys via OpenSSH's ssh-keysign

  • OpenSSH-style known_hosts file__ support

  • OpenSSH-style authorized_keys file__ support

  • Partial support for OpenSSH-style configuration files__

  • Compatibility with OpenSSH "Encrypt then MAC" option for better security

  • Time and byte-count based session key renegotiation

  • Designed to be easy to extend to support new forms of key exchange, authentication, encryption, and compression algorithms

__ http://asyncssh.readthedocs.io/en/stable/api.html#key-exchange-algorithms __ http://asyncssh.readthedocs.io/en/stable/api.html#encryption-algorithms __ http://asyncssh.readthedocs.io/en/stable/api.html#mac-algorithms __ http://asyncssh.readthedocs.io/en/stable/api.html#compression-algorithms __ http://asyncssh.readthedocs.io/en/stable/api.html#public-key-support __ http://asyncssh.readthedocs.io/en/stable/api.html#ssh-agent-support __ http://asyncssh.readthedocs.io/en/stable/api.html#known-hosts __ http://asyncssh.readthedocs.io/en/stable/api.html#authorized-keys __ http://asyncssh.readthedocs.io/en/stable/api.html#config-file-support

License

This package is released under the following terms:

Copyright (c) 2013-2024 by Ron Frederick ronf@timeheart.net and others.

This program and the accompanying materials are made available under the terms of the Eclipse Public License v2.0 which accompanies this distribution and is available at:

http://www.eclipse.org/legal/epl-2.0/

This program may also be made available under the following secondary licenses when the conditions for such availability set forth in the Eclipse Public License v2.0 are satisfied:

 GNU General Public License, Version 2.0, or any later versions of
 that license

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later

For more information about this license, please see the Eclipse Public License FAQ <https://www.eclipse.org/legal/epl-2.0/faq.php>_.

Prerequisites

To use AsyncSSH 2.0 or later, you need the following:

  • Python 3.6 or later
  • cryptography (PyCA) 3.1 or later

Installation

Install AsyncSSH by running:

::

pip install asyncssh

Optional Extras ^^^^^^^^^^^^^^^

There are some optional modules you can install to enable additional functionality:

AsyncSSH defines the following optional PyPI extra packages to make it easy to install any or all of these dependencies:

| bcrypt | fido2 | gssapi | libnacl | pkcs11 | pyOpenSSL | pywin32

For example, to install bcrypt, fido2, gssapi, libnacl, pkcs11, and pyOpenSSL on UNIX, you can run:

::

pip install 'asyncssh[bcrypt,fido2,gssapi,libnacl,pkcs11,pyOpenSSL]'

To install bcrypt, fido2, libnacl, pkcs11, pyOpenSSL, and pywin32 on Windows, you can run:

::

pip install 'asyncssh[bcrypt,fido2,libnacl,pkcs11,pyOpenSSL,pywin32]'

Note that you will still need to manually install the libsodium library listed above for libnacl to work correctly and/or libnettle for UMAC support. Unfortunately, since liboqs, libsodium, and libnettle are not Python packages, they cannot be directly installed using pip.

Installing the development branch ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you would like to install the development branch of asyncssh directly from Github, you can use the following command to do this:

::

  pip install git+https://github.com/ronf/asyncssh@develop

Mailing Lists

Three mailing lists are available for AsyncSSH:

  • asyncssh-announce@googlegroups.com__: Project announcements
  • asyncssh-dev@googlegroups.com__: Development discussions
  • asyncssh-users@googlegroups.com__: End-user discussions

__ http://groups.google.com/d/forum/asyncssh-announce __ http://groups.google.com/d/forum/asyncssh-dev __ http://groups.google.com/d/forum/asyncssh-users