adb_shell Documentation

repository·master·Indexed 20 days ago

https://github.com/jefflirion/adb_shell

A Python package that implements ADB (Android Debug Bridge) shell and FileSync functionality. It provides transport mechanisms for TCP and USB connections, including synchronous and asynchronous operations, and tools for generating ADB key files and executing shell commands on Android devices.

Tokens
792
Snippets
5
Records
6
Agent score
20%

What's inside adb_shell

  1. Understand the adb_shell.transport package submodules

    master

    The adb_shell.transport package provides the underlying transport mechanisms for communicating with Android devices via ADB. Depending on your connection type (TCP or USB) and whether you require asynchronous operations, you should use one of the following submodules:

    • TCP Transport: Use adb_shell.transport.tcp_transport for synchronous TCP connections or adb_shell.transport.tcp_transport_async for asynchronous TCP connections.
    • USB Transport: Use adb_shell.transport.usb_transport for connections via a USB cable.
    • Base Transports: adb_shell.transport.base_transport and adb_shell.transport.base_transport_async define the abstract base classes for all transport implementations.
  2. Connect to devices via TCP or USB and run shell commands

    master

    This example demonstrates how to load RSA keys using PythonRSASigner, connect to a device over TCP (AdbDeviceTcp) and via USB (AdbDeviceUsb), and execute shell commands using the .shell() method.

    from adb_shell.adb_device import AdbDeviceTcp, AdbDeviceUsb
    from adb_shell.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 via TCP
    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[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')