Read and write sip.conf from C++ application

Hi,

I want to read sip.conf from asterisk server path, modify the file from a C++ application.
eg., ofstream("/usr/local/data/etc/asterisk,sip.conf,XXX) … Like this.
May I know is it possible ? Any other ways are highly appreciated
Thanks in Advance

Related to read and modify a file with C++ I assume is possible, because I have done it with Python and PHP so don’t see the reason why it can’t be done with C++. If I want to modify programmatically Asterisk configuration, I prefer to use Real-time architecture. Also be aware chan_sip is community supported now

What means XXX here ?

File sip.conf is normal text file.
You can edit it by C++ as you want, i don’t see any limit.

You can re-write any file in any language.

After you change the file, you need to ask Asterisk to reload the file.

include, tryinclude, and exec may also be of interest to you.

Thanks all
ofstream myfile;

myfile.open("/usr/local/data/etc/asterisk/sip.conf");
if ( myfile.is_open() ) { // always check whether the file is open

This is my code snippet but the file is not opened

read or write permission of filestream

Could you please provide me an example for #exec - modify the sip.conf from the path /usr/local/data/etc/asterisk/ ? The exampe provided in the link
#exec /opt/bin/build-extra-contexts.sh --foo=“bar”
not understandable

This is kind of an uncommon path for Asterisk configuration files.

  1. What output do you get with:
ls -l /usr/local/data/etc/asterisk/sip.conf
  1. What user does your code execute as?
  2. What user does Asterisk execute as?
  3. Does prefixing the path to your executable with ‘sudo’ allow the file to be accessed?
  1. -rw-r–r–
  2. Windows user
  3. Asterisk is from Linux server (root user)
  4. Yes

You really should be using pjsip instead of sip.

The first thing is to understand that this is not intended to modify the configuration file that contains the #exec. It is to execute some script or program, capture the output (STDOUT) from the script/program, and include it in place of the ‘#exec’ line when the configuration file is reloaded.

For example, if you wanted to create a custom endpoint based on the host name and the current date (a contrived, useless example), you could add to pjsip.conf:

#exec add-this-host-as-endpoint.sh

Where add-this-host-as-endpoint.sh could be something like:

#!/bin/bash

# add this host as a custom endpoint

# define variables
	custom_hostname=${HOSTNAME%%.*}-$(date +%F)

# output the endpoint definition
	(
	printf '[%s]\n' ${custom_hostname}
	printf '\t  type\t\t\t\t= endpoint\n'
	printf '\tallow\t\t\t\t= !all,ulaw\n'
	printf '\taors\t\t\t\t= %s\n' ${custom_hostname}
	printf '\tcontext\t\t\t\t= start\n'
	printf '\toutbound_auth\t\t\t= %s\n' ${custom_hostname}
	)

# (end of add-this-host-as-endpoint.sh)

And the ‘net effect’ would be that:

#exec add-this-host-as-endpoint.sh

would be parsed as:

[pbx10-2021-07-25]
	  type				= endpoint
	allow				= !all,ulaw
	aors				= pbx10-2021-07-25
	context				= start
	outbound_auth		= pbx10-2021-07-25
  1. ‘ls -l’ should look something more like:
-rw-r----- 1 sedwards sedwards 6984 Jul 22 13:08 /etc/asterisk/sip.conf

The owner, group, and permissions were the bits I was interested in, but based on your answers to 3 & 4 it may be moot.

  1. I’m confused. Are you trying to modify a file on a Linux host from a Windows host? Is there a shared filesystem between the 2 hosts?

  2. If you are going to execute your program as root, is there still a problem? If you are going to execute your program as another user, it’s a matter of owner/group/permissions that you need to work out.

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