Skip to main content

Fork of adb-shell with ADB Wi-Fi (TLS) pairing, TLS transport, and mDNS discovery added.

Project description

This package is a fork of adb_shell that adds support for modern ADB Wi-Fi pairing — the 6-digit-code TLS pairing flow introduced in Android 11’s “Wireless debugging” feature, plus the post-pairing TLS data channel and mDNS-based service discovery.

It exists primarily as a testbed for verifying that Wi-Fi pairing support can be added to Home Assistant’s Android TV integration. If you do not need Wi-Fi pairing, install upstream adb-shell instead — the upstream package is the canonical home of all non-Wi-Fi functionality.

Original adb_shell documentation: https://adb-shell.readthedocs.io/

Origin

This Python package implements ADB shell and FileSync functionality. It originated from python-adb and was substantially rewritten by Jeff Irion in adb_shell; this fork adds Wi-Fi pairing on top.

Installation

pip install adb-shell-wifi

Async

To utilize the async version of this code, you must install into a Python 3.7+ environment via:

pip install adb-shell-wifi[async]

USB Support (Experimental)

To connect to a device via USB, install this package via:

pip install adb-shell-wifi[usb]

Wi-Fi Pairing Support (Experimental)

To pair with a modern Android device using a 6-digit pairing code (Android 11+ “Wireless debugging”), install this package via:

pip install adb-shell-wifi[wifi]

This pulls in spake2-cffi and pyOpenSSL. Note: spake2-cffi does not publish Windows wheels, so Windows users will need a C toolchain available at install time.

On the device, open Settings > System > Developer options > Wireless debugging > Pair device with pairing code. Note the IP address, port, and 6-digit code shown by the device, then:

from adb_shell_wifi.pairing import pair

adbkey = "path/to/adbkey"
with open(adbkey, "rb") as f:
    priv = f.read()
with open(adbkey + ".pub", "rb") as f:
    pub = f.read()

peer_info = pair(
    host="192.168.1.42",   # from device UI
    port=43811,            # from device UI
    pairing_code="515109", # from device UI
    private_key_pem=priv,
    public_key=pub,
)
print("device peer info:", peer_info.type, peer_info.data)

After pairing succeeds, the device records the host’s public key in its keystore. To actually run shell commands, push, pull, etc. over the post-pairing TLS channel, use AdbDeviceTls and pass the same adbkey PEM as tls_priv_pem:

from adb_shell_wifi.adb_device import AdbDeviceTls
from adb_shell_wifi.mdns import discover_connect_services

# Find the random port the device chose for its TLS data socket.
services = discover_connect_services(timeout_s=4.0)
svc = services[0]   # or pick the right device by name/host

device = AdbDeviceTls(svc.host, svc.port, default_transport_timeout_s=10.0)
device.connect(rsa_keys=[], tls_priv_pem=priv, auth_timeout_s=10.0)
print(device.shell("echo hello from adb_shell_wifi"))
device.close()

The mdns module also exposes discover_pairing_services() for finding devices that currently have a pairing dialog open, plus async equivalents (discover_connect_services_async, discover_pairing_services_async).

Example Usage

(Based on androidtv/adb_manager.py)

from adb_shell_wifi.adb_device import AdbDeviceTcp, AdbDeviceUsb
from adb_shell_wifi.auth.sign_pythonrsa import PythonRSASigner

# Load the public and private keys
adbkey = 'path/to/adbkey'
with open(adbkey) as f:
    priv = f.read()
with open(adbkey + '.pub') as f:
     pub = f.read()
signer = PythonRSASigner(pub, priv)

# Connect
device1 = AdbDeviceTcp('192.168.0.222', 5555, default_transport_timeout_s=9.)
device1.connect(rsa_keys=[signer], auth_timeout_s=0.1)

# Connect via USB (package must be installed via `pip install adb-shell-wifi[usb])`
device2 = AdbDeviceUsb()
device2.connect(rsa_keys=[signer], auth_timeout_s=0.1)

# Send a shell command
response1 = device1.shell('echo TEST1')
response2 = device2.shell('echo TEST2')

Generate ADB Key Files

If you need to generate a key, you can do so as follows.

from adb_shell_wifi.auth.keygen import keygen

keygen('path/to/adbkey')

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

adb_shell_wifi-0.5.0.tar.gz (86.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

adb_shell_wifi-0.5.0-py3-none-any.whl (78.9 kB view details)

Uploaded Python 3

File details

Details for the file adb_shell_wifi-0.5.0.tar.gz.

File metadata

  • Download URL: adb_shell_wifi-0.5.0.tar.gz
  • Upload date:
  • Size: 86.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for adb_shell_wifi-0.5.0.tar.gz
Algorithm Hash digest
SHA256 077ddccc63a1ba7350af9858dddd97b1f6e3e6ecf5030e0d4e7dc052b2561afe
MD5 21327df90322d254380a222f3ce19e93
BLAKE2b-256 d3a7e53a81641c1ee5a5f4a512127a16b2f568400e65c0bad14322d3c40e5edd

See more details on using hashes here.

File details

Details for the file adb_shell_wifi-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: adb_shell_wifi-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 78.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for adb_shell_wifi-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 231d4c608f2631ddb2df55bdc7ea111c43275277d04fcbc791f8b613dfaa391a
MD5 53aeac3ef8b95582c620e3bed7fdfd04
BLAKE2b-256 b2396685726ea605ed2d6d73d198973cf97a40e9d401b6d9eec77e3120498430

See more details on using hashes here.

Supported by

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