#!/usr/bin/perl -w # autodialer.pl # by NotTheory # http://www.oldskoolphreak.com # This autodials via the manager API on an Asterisk box. # VERY useful for fast and easy scanning with asterisk! -natas use Net::Telnet; # Here's all the necessary Asterisk stuff. $Username = "manager"; $Secret = "password"; $MyPhone = "Zap/2"; $MyPhoneEscaped = "Zap\\/2"; # Get the arguments (base number and count). $BaseNumber = $ARGV [0]; $Count = $ARGV [1]; if (!defined ($BaseNumber) || !defined ($Count)) { print "You must specify a base number and a count.\n"; print "(ie. perl autodialer.pl 9145550000 100)\n"; exit; } # Connect to localhost via telnet on the Asterisk manager port. $TelnetClient = new Net::Telnet (Timeout => 10, Errmode => "die", Host => "localhost", Port => 5038); # Login with our username and secret. $TelnetClient->open (); $TelnetClient->print ("Action: Login"); $TelnetClient->print ("Username: $Username"); $TelnetClient->print ("Secret: $Secret"); $TelnetClient->print (""); # Check for login success. ($Prematch, $Match) = $TelnetClient->waitfor ("/Message: .*/"); if (($Prematch =~ m/Success/) && ($Match =~ m/Authentication/)) { print "Good authentication.\n"; } else { print "Authentication failed.\n"; exit; } for ($Loop = 0; $Loop < $Count; $Loop++) { $CurrentNumber = $BaseNumber + $Loop; $Try = 0; $OriginateSuccess = 0; # Call origination fails sometimes (usually if you try to originate too # quickly after hanging up). This section makes sure we try to connect # a few times. # Make sure the "Exten:" line matches something in your dial plan # (/etc/asterisk/extensions.conf). Dialing 9 and the number takes me # out to the PSTN via my FXO card in my setup, YMMV. while (($Try < 5) && (!$OriginateSuccess)) { print "Dialing $CurrentNumber...\n"; $TelnetClient->print ("Action: Originate"); $TelnetClient->print ("Channel: $MyPhone"); $TelnetClient->print ("Exten: 9$CurrentNumber"); $TelnetClient->print ("Priority: 1"); $TelnetClient->print ("Callerid: \"Wardial\" <$CurrentNumber>"); $TelnetClient->print (""); ($Prematch, $Match) = $TelnetClient->waitfor ("/Message: .*/"); if (($Match =~ m/Originate/) && ($Prematch =~ m/Success/)) { $OriginateSuccess = 1; print "Origination success.\n"; } else { print "Origination failure.\n"; sleep (2); } $Try++; } # Now we've put the call through. We need to wait for them to hang up. print "Waiting...\n"; $OnThePhone = 1; while ($OnThePhone) { ($Prematch, $Match) = $TelnetClient->waitfor (match => "/Uniqueid: .*/", timeout => 60); print "Event detected.\n"; if (($Prematch =~ m/Hangup/) && ($Prematch =~ m/$MyPhoneEscaped/)) { $OnThePhone = 0; } } # They hung up. Move on. print "User hung up. Going to next number.\n"; } Ok, there are a few things you need to do to get this working. First, enable the manager. Something like this in your manager.conf file (/etc/asterisk) should work: ; ; Asterisk Call Management support ; [general] enabled = yes port = 5038 bindaddr = 127.0.0.1 [ntheory] secret = password ;deny=0.0.0.0/0.0.0.0 ;permit=127.0.0.1/255.255.255.255 read = system,call,log,verbose,command,agent,user write = system,call,log,verbose,command,agent,user This will enable the manager and only allow you to connect to it from your localhost. Don't forget to restart Asterisk. Try telnetting to 127.0.0.1:5038 to make sure it worked. If it doesn't connect you're doing something wrong. Second, modify the MyPhone and MyPhoneEscaped variables. I have a channel bank with a few phones on it. Zap/2 is my desk phone. You can make this any Zap channel you want just make sure they match and double up any backslashes as I did. It works for SIP and IAX channels as well. Third, modify the section of the script that refers to CurrentNumber. I need a 9 before outgoing calls so I put "9$CurrentNumber". Change it to reflect the way your dialplan takes calls from the interface you specified in MyPhone and MyPhoneEscaped. Finally, run it! When you do it successfully your phone should ring with the caller ID "WARDIAL " where number is the current number it is calling. When you pick up the phone it dials and puts you through. When you hang up it rings you back. The code is a bit of a hack. Sometimes it gets screwy but it has worked for me for lots of numbers so far. The only thing is I didn't include a delay between calls to let me write down results. This is mostly because when I run into a block of NIS I don't want to wait 10 seconds or so between calls. The only way to quit (other than finishing a block) is to CTRL-C out of the script. Enjoy and give me feedback! (This script requires the perl module Net::Telnet so if you dont have it installed you can get it by executing: perl -MCPAN -e "install Net::Telnet" -natas)