Cmd_api_dns_control

scottnj

Verified User
Joined
Jul 9, 2005
Messages
6
Up to now, I have been successful working with the api with my php scripts. I would like to add some of the features of CMD_API_DNS_CONTROL, but this one doesn't work like the rest.
CMD_API_DNS_CONTROL

I would like to do something like
PHP:
		$sock = new HTTPSocket;
		$sock->connect($this->domain,2222);
		$sock->set_login($this->username,$this->password);
		$sock->query('CMD_API_DNS_CONTROL',
						array(
						'domain' => $this->domain,
						'action' => 'add',
						'type' => 'A',
						'name' => 'test',
						'value' => '192.168.0.1'
						)
					);
		$result = $sock->fetch_parsed_body();
		return($result);

I do not want to do it like
http://domain.com:2222/CMD_API_DNS_CONTROL?domain=domain.com&action=add&type=A&name=testing&value=192.168.0.2
because it requires someone to login.
 
I haven't tried it, but something like this should work I would think:

PHP:
$sock = new HTTPSocket;
$sock->connect('127.0.0.1',2222);
$sock->set_login($_SERVER['USER']);
$sock->query("/CMD_API_DNS_CONTROL?domain=domain.com&action=add&type=A&name=testing&value=192.168.0.2");
$result = $sock->fetch_parsed_body();
return($result);
 
Last edited:
ok, I got the add to work, I was forgetting the "/" before the "CMD_API_DNS_CONTROL". Here it is so far.
PHP:
/********************************************************/
function dns_add($sub, $address)
{
	$sock = new HTTPSocket;
	$sock->connect($this->domain,2222);
	$sock->set_login($this->username,$this->password);
	$sock->query("/CMD_API_DNS_CONTROL?domain=".$this->domain."&action=add&type=A&name=".$sub."&value=".$address);
	$result = $sock->fetch_parsed_body();
	return($result);
}
Now, is there a way to list the dns entries? If I don't add an action, it spits out all the info, but it is not parsed nicely!:mad:
PHP:
/********************************************************/
function dns_dump()
{
	$sock = new HTTPSocket;
	$sock->connect($this->domain,2222);
	$sock->set_login($this->username,$this->password);
	$sock->query("/CMD_API_DNS_CONTROL?domain=".$this->domain);
	$result = $sock->fetch_parsed_body();
	return($result);
}
I tried the "action=select" option, and IT IS NOT SELECT, IT IS DELETE!
PHP:
/********************************************************/
function dns_delete($sub, $address)
{
	$sock = new HTTPSocket;
	$sock->connect($this->domain,2222);
	$sock->set_login($this->username,$this->password);
	$sock->query('/CMD_API_DNS_CONTROL?domain='.$this->domain.'&action=select&arecs0='.urlencode('name='.$sub.'&value='.$address));
	$result = $sock->fetch_parsed_body();
	return($result);
}
I am not sure what the numbering in
arecs0, nsrecs0, mxrecs0, cnamerecs0, ptrrecs0 (0 and be any number, generally starting from zero, going up).
is all about?

Does anyone know how to parse the dns entries into a nice array?
 
jmstacey said:
http://www.directadmin.com/features.php?id=504

There is no parsing in this API command ;)
You'll need to setup your own routines if you really need to.
I was afraid you were going to say that.:(

I have never tried parsing a raw dump before, I am assuming it will require using regular expressions.
The output has a few line of random stuff, then the entries appear to be in a tab deliminated table, then a few more random lines I dont care about.

I do not know regex very well, can someone recomend any good resources? Or even better, some example code?:D
 
I decided not to parse the output, but to get the info some how else.
IF I had PHP5, I could use dns_get_record(), but that function is not in PHP4.
I found some functions using PEAR. There is a class, Net_DNS, that outputs the info in arrays.:cool:
 
The PEAR solution worked, but ran into problems with cashed data or propigation or something. I needed something more real time.
PHP:
/********************************************************/
function dns_dump2()
{
	require_once 'Net/DNS.php';
	$resolver = new Net_DNS_Resolver();
	$response = $resolver->axfr($this->domain);
	foreach($response as $temp1)
	{
		if($temp1->type=='A')
		{
				$result[$temp1->name] = $temp1->address;
		}
	}
	return($result);
}
I did a hack to get it working, but here it is. But I am sure there is a better way.
PHP:
/********************************************************/
function dns_dump()
{
	$sock = new HTTPSocket;
	$sock->connect($this->domain,2222);
	$sock->set_login($this->username,$this->password);
	$sock->query("/CMD_API_DNS_CONTROL?domain=".$this->domain);
	$result = $sock->fetch_parsed_body();
	foreach($result as $key=>$val) // extract key from the array
	{
		$out1=$key;
	}
	$out2=str_replace('_', '.', $out1); // replace '_' with '.'
	$lines=explode("\n",$out2); // break up into seperate lines
	foreach($lines as $key=>$line)
	{
		$cells[$key]=explode("\t",$line); // content was tab deliminated
		if(count($cells[$key])==5) // it seems all entries are 5 cells, junk is 1 cell
		{
			if($cells[$key][3]=='A') // only save the A records
			{
				$out[$cells[$key][0]]=$cells[$key][4];
			}
		}
	}
	$result=$out;
	return($result);
}
 
Back
Top