Configurar la red

Esta página se ha traducido automáticamente y aún no se ha revisado. En caso de discrepancia, prevalece el original en inglés. Leer el original en inglés

Lea y modifique la configuración de red a través de la API. En la WebGUI, consulte Configuración de red. Los ejemplos utilizan la biblioteca de Python e inician sesión con una clave de aplicación. Sustituya qb2-xxxxxxxxx por el nombre de host o la dirección IP de su sensor y application-key-for-qb2-xxxxxxxxx por el secreto de su clave.

Obtener la configuración de red

El método GetConfig devuelve la configuración de red establecida en el sensor.

import blickfeld_qb2

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

# Open a secure and authenticated connection to the sensor
with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    response = service.get_config()
    print(response)

Salida:

Network()

Un sensor en el que no se ha establecido ninguna configuración de red devuelve una configuración vacía y utiliza DHCP y el direccionamiento de enlace local. El método GetStatus devuelve en active_config la configuración en uso, con las direcciones.

Establecer una configuración de red estática

En una red sin servidor DHCP, ajuste la dirección IPv4 manualmente. El ejemplo ajusta dos direcciones, la puerta de enlace y dos servidores DNS.

Después del cambio, el sensor solo es accesible en la nueva dirección. Si ya no puede acceder a él, conéctese a través de la dirección IP de respaldo 192.168.26.26.

import blickfeld_qb2

# Two addresses, the gateway and two DNS servers
manual = blickfeld_qb2.system.config.NetworkStatic(
    ipv4=blickfeld_qb2.system.config.NetworkStaticIpv4(
        ip=["10.0.2.15/24", "10.0.2.43/24"],
        gateway="10.0.2.2",
        dns_server=["8.8.4.4", "8.8.8.8"],
    )
)

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

# Open a secure and authenticated connection to the sensor
with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    # Set the configuration
    service.set_config(manual=manual)

Establecer una configuración inalámbrica

Este ejemplo conecta el sensor a una red WiFi. Primero prueba la configuración con validate y solo la aplica cuando el sensor ha obtenido una dirección en la red WiFi.

validate aplica la configuración durante un momento, hasta aproximadamente un minuto, y después vuelve a la actual. Mientras tanto, el sensor abandona su red actual y no es accesible en su dirección actual.

import blickfeld_qb2
from grpclib import GRPCError

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    # Create a new configuration
    configuration = blickfeld_qb2.system.config.Network()

    # Activate DHCP
    configuration.dhcp = True

    # Configure the wifi settings
    configuration.wireless.ssid = "your_wifi_ssid"
    configuration.wireless.password = "your_wifi_password"

    # Validate the wireless configuration: the sensor applies it shortly and returns to its current configuration.
    # It raises an error with the reason when the sensor gets no address or no gateway in the WiFi network.
    try:
        response = service.validate(config=configuration)
    except GRPCError as error:
        raise SystemExit(f"Validation failed: {error.message}") from None
    print("Addresses in the WiFi network:", response.status.active_config.manual.ipv4.ip)

    # Apply the configuration
    service.set_config(dhcp=configuration.dhcp, wireless=configuration.wireless)

    # The sensor is now reachable under one of the addresses of the validation