DBGet with Python?

Hello, is there a way to perform a DBGet query in the AstDB using a Python script?

On Thursday 08 August 2024 at 21:55:23, DHT768 via Asterisk Community wrote:

Hello, is there a way to perform a DBGet query in the AstDB using a Python
script?

The obvious starting point would be AMI:

https://docs.asterisk.org/Asterisk_16_Documentation/API_Documentation/AMI_Actions/DBGet

https://docs.asterisk.org/Latest_API/API_Documentation/AGI_Commands/database_get/

Alternatively you could just talk to SQLite, which is what the Asterisk
internal DB uses.

Antony.

–
Why are they called “The Rocky Mountains”?
What are other mountains made of?

                                               Please reply to the list;
                                                     please *don't* CC me.

Be careful about trying to directly access an SQLite database that Asterisk has open. SQLite is not designed for multi-user access.

1 Like

Thanks for your answers. I am using this Python script and I get the following message:

import telnetlib
import re
import time


HOST = "localhost"
PORT = 5038
USER = "admin"
SECRET = "XXX"

tn = telnetlib.Telnet(HOST, PORT)
tn.write(f"Action: Login\r\n".encode('ascii'))
tn.write(f"Username: {USER}\r\n".encode('ascii'))
tn.write(f"Secret: {SECRET}\r\n\r\n".encode('ascii'))

time.sleep(2)

# DBGet-Action

family = "locked"
key = "1181"
action_id = "12345"

tn.write(f"Action: DBGet\r\n".encode('ascii'))
tn.write(f"ActionID: {action_id}\r\n".encode('ascii'))
tn.write(f"Family: {family}\r\n".encode('ascii'))
tn.write(f"Key: {key}\r\n\r\n".encode('ascii'))

response = tn.read_until(b"\r\n\r\n", timeout=10).decode('ascii')
print("Raw response:\n", response)
match = re.search(r'Value: (.+?)\r', response)

if match:
value = match.group(1)
print("Database value:", value)

else:
print("No value found or the response does not contain the expected format")

tn.write(b"Action: Logoff\r\n\r\n")
tn.close()

Result in terminal:

$ python3 check_locked_1181.py

Raw response:

Asterisk Call Manager/9.0.0
Response: Success
Message: Authentication accepted

No value found or the response does not contain the expected format

Hi,
There are libraries to access AMI from Python.
I currently use Panoramisk.
Good luck,
Jean-Denis

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.