How to set LTE SIM PIN on RouterOS using Python RouterOS-api

How to unconditionally set the SIM PIN

This will set the SIM pin - if the SIM pin is already set, it will be updated to the new value.

# Ask user for PIN
pin = "1234" # Ensure to replace this with your actual PIN
# Check if its a 4-digit number
if not pin.isdigit() or len(pin) != 4:
    print("Invalid PIN code")
    exit(1)
# Send the PIN code
# /interface/lte/set pin=1234 [find name=lte1]
lte_interface = api.get_resource('/interface/lte').get()[0]
lte_interface_id = lte_interface['id']
print(f"Sending PIN code {pin} to LTE interface {lte_interface['name']}")
api.get_resource('/interface/lte').set(id=lte_interface_id, pin=pin)

For convenience, here’s this code as a function:

def set_lte_interface_pin(api, pin):
    # Set the PIN code
    lte_interface = api.get_resource('/interface/lte').get()[0]
    lte_interface_id = lte_interface['id']
    # Check if the PIN code is already set
    # Check if its a 4-digit number
    if not pin.isdigit() or len(pin) != 4:
        print("Invalid PIN code")
        exit(1)
    print(f"Sending PIN code {pin} to LTE interface {lte_interface['name']}")
    api.get_resource('/interface/lte').set(id=lte_interface_id, pin=pin)

# Example usage
set_lte_interface_pin(api, "1234")

Full example


#!/usr/bin/env python3
import routeros_api

connection = routeros_api.RouterOsApiPool(
    '192.168.88.1', username='admin', password='L9TYAUV7R8',
    plaintext_login=True
)
api = connection.get_api()

# Set the PIN code
lte_interface = api.get_resource('/interface/lte').get()[0]
lte_interface_id = lte_interface['id']
# Check if the PIN code is already set
if lte_interface['pin']:
    print("PIN code is already set")
else:
    # Ask user for PIN
    pin = input("Enter the PIN code: ")
    # Check if its a 4-digit number
    if not pin.isdigit() or len(pin) != 4:
        print("Invalid PIN code")
        exit(1)
    print(f"Sending PIN code {pin} to LTE interface {lte_interface['name']}")
    api.get_resource('/interface/lte').set(id=lte_interface_id, pin=pin)