Asterisk命令行外呼脚本实现求助:Dial命令失效如何解决
Hey there! Let’s tackle this auto-outbound call script issue you’re having with Asterisk. Since the direct Dial command from the CLI didn’t pan out, that makes sense—Dial is meant to be used within an active call context (like in your dialplan) rather than directly initiating a new call from scratch. Here are a few reliable methods to automate outbound calls instead:
originate Command (CLI or Script) This is the most straightforward way to trigger outbound calls programmatically. The originate command lets you initiate a call from a channel to a target number, and you can run it directly from the CLI or wrap it in a shell script.
Example CLI Commands:
Originate from a specific extension to an external number via your trunk:
asterisk -rx "originate SIP/100 SIP/5551234@your_trunk_provider"Replace
SIP/100with your extension,5551234with the target number, andyour_trunk_providerwith your configured trunk context.Originate via your dialplan context (to leverage existing routing rules):
asterisk -rx "originate local/5551234@outbound_context extension s 1"Here,
local/5551234@outbound_contextuses your dialplan’soutbound_contextto handle routing, andextension s 1points to the starting priority in that context.
originate in a Shell Script For repeated use or batch calls, create a simple shell script to automate the process:
#!/bin/bash # Auto-outbound call script # Configure your variables TARGET_NUMBER="5551234" EXTENSION="100" TRUNK="your_trunk_provider" DIALPLAN_CONTEXT="outbound" # Option 1: Call directly from extension to target asterisk -rx "originate SIP/$EXTENSION SIP/$TARGET_NUMBER@$TRUNK" # Option 2: Use dialplan context (uncomment if needed) # asterisk -rx "originate local/$TARGET_NUMBER@$DIALPLAN_CONTEXT extension s 1"
Save this as auto_call.sh, make it executable with chmod +x auto_call.sh, then run it whenever you need to trigger a call.
If you need more control (like handling call status, retries, or dynamic number lists), use an AGI script. Here’s a quick Python example using the asterisk-agi library:
First, install the library:
pip install asterisk-agi
Then create the script (auto_call_agi.py):
#!/usr/bin/env python3 import asterisk.agi agi = asterisk.agi.AGI() # Trigger the outbound call # Format: Originate(channel, context, extension, priority) agi.exec_command('Originate', 'SIP/5551234@your_trunk', 'SIP/100', 'outbound', 's', '1') agi.verbose("Outbound call initiated successfully")
You can run this script directly via the CLI by triggering it through originate:
asterisk -rx "originate agi://localhost/auto_call_agi.py extension s 1"
If you need to trigger calls from another application or remotely, AMI is the way to go. Here’s a Python example using the pyst2 library:
First, install the library:
pip install pyst2
Then create the script (ami_auto_call.py):
#!/usr/bin/env python3 from asterisk.manager import Manager # Connect to AMI manager = Manager() try: manager.connect('localhost') manager.login('ami_username', 'ami_password') # Update with your AMI credentials # Send the originate action manager.send_action({ 'Action': 'Originate', 'Channel': 'SIP/100', 'Exten': '5551234', 'Context': 'outbound', 'Priority': '1', 'Async': 'true' # Run in background so the script doesn't wait for the call to end }) print("Outbound call initiated via AMI") finally: manager.close()
Don’t forget to enable AMI in your manager.conf file and set up valid credentials.
Quick Note on Why Dial Failed
The Dial command requires an active channel context (like when you’re already on a call via the CLI or a softphone). It’s used to connect an existing call to another endpoint, not initiate a new outbound call from nothing. That’s why originate and the other methods above are better suited for automation.
内容的提问来源于stack exchange,提问作者Javier Arenas Iglesias

