[asterisk-dev] C Code to connect to Asterisk Manager Interface

Wolfgang Pichler wpichler at yosd.at
Thu Feb 28 01:26:49 CST 2008


Hi,

please don't cross post to the dev and to the users list...

Take a look at 
http://sourceforge.net/project/showfiles.php?group_id=171337 - there you 
will find a C lib (libami) to connect to the asteirsk manager interface 
and execute commands...

Also the asterisk manager proxy there is really nice...

regards,
Wolfgang

Michael Henderson schrieb:
> Hi,
>
> I have written a C code which would let me connect to the Asterisk 
> Manager Interface. The code compiles successfully but on running the 
> code I get unauthorized login shown in the Asterisk command line console.
>
> Here is my C code:
>
> #include<stdio.h>
> #include<netdb.h>
> #include<unistd.h>
> #include<string.h>
> #include<arpa/inet.h>
> #include<sys/types.h>
> #include<sys/socket.h>
> #include<netinet/in.h>
>
> #define MAX_MSG_SIZE 512
> #define SERVER_ADDRESS "192.168.0.150 <http://192.168.0.150>"
> #define CLIENT_ADDRESS "192.168.0.150 <http://192.168.0.150>"
> #define SERVER_PORT 5038
> #define    CLIENT_PORT 5100
>
> int main()
> {
>     int sd;
>     struct sockaddr_in serveraddr, clientaddr;
>     char msg[MAX_MSG_SIZE];
>
>     bzero((char *) &serveraddr, sizeof(serveraddr));
>     serveraddr.sin_family = AF_INET;
>     serveraddr.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
>     serveraddr.sin_port = htons(SERVER_PORT);
>
>     bzero((char *) &clientaddr, sizeof(clientaddr));
>     clientaddr.sin_family = AF_INET;
>     clientaddr.sin_addr.s_addr = INADDR_ANY;
>     clientaddr.sin_port = htons(CLIENT_PORT);
>
>     sd = socket(AF_INET, SOCK_STREAM, 0);
>     printf("\nCreated socket ...");
>
>     bind(sd,(struct sockaddr *) &clientaddr, sizeof(clientaddr));
>     printf("\nBinding successful ...");
>
>     connect(sd,(struct sockaddr *) &serveraddr, sizeof(serveraddr));
>     printf("\nConnected ...");
>
>     *msg=(char)"Action: Login\r\nUsername: admin\r\nSecret: 
> admin\r\nActionID: 1\r\n\r\n";
>     send(sd,msg,strlen(msg)+1,0);
>     close(sd);
>
>     return(1);
> }
>
>
>
> Please correct me where I am going wrong. In manager.conf the username 
> and secret has been defined.
> Thank you.
> ------------------------------------------------------------------------
>
> _______________________________________________
> --Bandwidth and Colocation Provided by http://www.api-digital.com--
>
> asterisk-dev mailing list
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-dev




More information about the asterisk-dev mailing list