I am fairly new to CakePHP and have been struggling to figure out how to do this. I need to store IP Addresses in a table with descriptions of what they are assigned to etc. I first create the IP Range e.g. 192.168.0.1/255.255.255.0 this is not a problem, however I want the IP Range to create all the IP Addresses in another table.
Ip_Ranges (stored as ip2long):
Id, Network, Subnet, Description
1 , 192.168.0.0, 255.255.255.0, Internal Range
Ip_Addresses(stored as ip2long):
Address, Subnet, Description
192.168.0.1, 255.255.255.0,
192.168.0.2, 255.255.255.0,
192.168.0.3, 255.255.255.0,
192.168.0.4, 255.255.255.0,
192.168.0.., 255...
You get the picture!
Code so far:
public function add() {
$this->IpRange->create();
if ($this->IpRange->save($this->request->data)) {
$ip = ip2long($this->request->data['IpRange']['Network']);
$nm = ip2long($this->request->data['IpRange']['Subnet']);
$nw = ($ip & $nm);
$bc = $nw | (~$nm);
while($ip <= $bc ){
//What should go here to save the $ip???
$ip++;
}
$this->Session->setFlash(__('The IP Range has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The IP Range could not be saved. Please, try again.'));
}
}
So as you can see creating the IP range is just a simple form save, but when this saves I need it to populate the ip_addresses table which is where I'm stuck.
How do I do this? I assume with a while look and just ++ the network address until reaching the broadcast address but I'm not familiar with cakephp and while loops saving to an alternative table.
Any assistance would be great.