duanfeng7756 2015-01-21 15:55
浏览 15

CakePHP生成IP范围

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.

  • 写回答

1条回答 默认 最新

  • douxue4395 2015-01-25 22:53
    关注

    Maybe split netmask, check it and depending on its value set number of iterations for adding IPs?

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入