[asterisk-commits] tilghman: trunk r113559 - /trunk/contrib/scripts/astcli

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 8 16:33:11 CDT 2008


Author: tilghman
Date: Tue Apr  8 16:33:11 2008
New Revision: 113559

URL: http://svn.digium.com/view/asterisk?view=rev&rev=113559
Log:
Add commandline tool for doing CLI commands through AMI (instead of using asterisk -rx)
(closes issue #12389)
 Reported by: davevg
 Patches: 
       astcli uploaded by davevg (license 209)

Added:
    trunk/contrib/scripts/astcli   (with props)

Added: trunk/contrib/scripts/astcli
URL: http://svn.digium.com/view/asterisk/trunk/contrib/scripts/astcli?view=auto&rev=113559
==============================================================================
--- trunk/contrib/scripts/astcli (added)
+++ trunk/contrib/scripts/astcli Tue Apr  8 16:33:11 2008
@@ -1,0 +1,67 @@
+#!/usr/bin/perl
+
+use strict;
+use Net::Telnet;
+use Getopt::Long;
+
+# Created by: David Van Ginneken
+# Bird's the Word Technologies
+# davevg at btwtech.com
+my ($user, $pw);
+GetOptions("username=s" => \$user, "password=s" => \$pw);
+if (undef ne $user) {
+	# Using CLI-specified options
+} elsif (-e "$ENV{HOME}/.astcli") {
+	process_credentials("$ENV{HOME}/.astcli");
+} elsif (-e '/etc/asterisk/.astcli') {
+	process_credentials('/etc/asterisk/.astcli');
+} else {
+	print "User Credentials File Not Found\n";
+}
+my $action = join(" ", @ARGV);
+
+&usage if (!defined $user || !defined $pw);
+
+my $tc = new Net::Telnet (Timeout => 10,
+    Errmode => "die",
+    Host    => "localhost",
+    Port    => 5038);
+# Login with our username and secret.
+$tc->open  ();
+$tc->print ("Action: Login");
+$tc->print ("Username: $user");
+$tc->print ("Secret: $pw");
+$tc->print ("Events: off");
+$tc->print (""); 
+# Check for login success.
+my ($pre, $match) = $tc->waitfor ("/Message: .*/");
+unless (($pre =~ m/Success/) && ($match =~ m/Authentication/)) {
+  print "Server Authentication failed.\n";
+  exit;
+}
+$tc->print ("Action: Command");
+$tc->print ("Command: $action");
+$tc->print ("");
+($pre, undef) = $tc->waitfor ("/--END COMMAND--.*/");
+$pre =~ s/^\n\n//g;
+$pre =~ s/Privilege: Command\n//;
+$pre =~ s/Response: Follows\n//;
+print $pre;
+
+
+sub process_credentials {
+	# Process the credentials found..
+	my $file = shift;
+	open (my $fh, "<$file") or die "Unable to open $file\n";
+	while (<$fh>) {
+		chomp;
+		(undef,$user) = split(/[,=]/, $_) if $_ =~ /user(name)?[,=]/i;
+		(undef,$pw) = split(/[,=]/, $_) if $_ =~ /(secret|passw(or)?d)[,=]/i;
+	}
+}
+
+sub usage {
+	print "astcli [-u <username> -p <passwd>] <cli-command>\n";
+	exit;
+}
+

Propchange: trunk/contrib/scripts/astcli
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/contrib/scripts/astcli
------------------------------------------------------------------------------
    svn:executable = *

Propchange: trunk/contrib/scripts/astcli
------------------------------------------------------------------------------
    svn:keywords = Id Author Revision Date

Propchange: trunk/contrib/scripts/astcli
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list