zebra命令行编写(二)

本文介绍如何在项目中配置cmd_node结构体及其关联函数,包括定义节点、设置提示符函数、安装节点及命令等步骤。通过具体示例说明如何实现VLAN接口配置功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中cmd_node的形式如下

/* Node which has some commands and prompt string and configuration
   function pointer . */
struct cmd_node
{
  /* Node index. */
  enum node_type node;

  /* Prompt character at vty interface. */
  char *prompt;
  /* Prompt function if prompt is NULL */
  char *(*prompt_func)(struct vty *);

  /* Is this node's configuration goes to vtysh ? */
  int vtysh;

  /* Node's configuration write function */
  int (*func) (struct vty *);

  /* Vector of this node's command list. */
  vector cmd_vector;
};


在这里总结用函数指针char *(*prompt_func)(struct vty *);来定义cmd_node。以下以interface_node为例进行讲解,可供参考

1. 在cmd_interface.c 的2355行定义

static struct cmd_node vlan_interface_node = {VLAN_INTERFACE_NODE, NULL, NULL, 1 };

2. 在command.h的node_type中加入VLAN_INTERFACE_NODE

3. 在cmd_interface.c中的2373行定义指针函数

static char *cmd_vlan_interface_node_prompt(struct vty *vty)
{
    static char prompt[40];
    sprintf(prompt, "%%s(Vlan-interface%d)# ", vty->para.para_integer);
    return prompt;
}
4. 在cmd_interface.c 的7806行的void cmd_interface_init(void)中添加
vlan_interface_node.prompt_func = cmd_vlan_interface_node_prompt;
    install_node(&vlan_interface_node, NULL);


5.在cmd_interface.c的2380行中添加触发命令

DEFUN (vlan_interface,
        access_vlan_interface_cmd,
        "interface vlan-interface <1-4094>",
        INTERFACE_DESC_STR
        "VLAN interface\n"
        "VLAN interface\n")
{
	char szifname[64];//added by wanghuanyu for 129

    #if 0 //add by zhouguanhua 2013/5/31
    sys_network_t info;
    int iRet = IPC_STATUS_OK;

    memset(&info, 0x0, sizeof(sys_network_t));
    iRet = ipc_get_sys_networking(&info);
    if(IPC_STATUS_OK != iRet)
    {
        vty_out(vty, " ERROR: get system networking failed!%s", VTY_NEWLINE);
        return CMD_WARNING;
    }

    if(info.m_vlan != strtoul(argv[0], NULL, 0))
    {
        vty_out(vty, " ERROR: Interface-vlan should be same with management-vlan!%s", VTY_NEWLINE);
        return CMD_WARNING;
    }
    vty->para.para_integer = strtoul(argv[0], NULL, 0);
    vty->node = VLAN_INTERFACE_NODE;
    return CMD_SUCCESS;
   #endif //add by zhouguanhua 2013/5/31
   sys_mvlan_t info;
   int iRet = IPC_STATUS_OK;
   memset(&info, 0x0, sizeof(sys_mvlan_t));
   iRet = get_sys_mvlan_value(&info);
   if(IPC_STATUS_OK != iRet)
   {
       vty_out(vty, " ERROR: get system mvlan failed!%s", VTY_NEWLINE);
       return CMD_WARNING;
   }
   if(info.m_vlan != strtoul(argv[0], NULL, 0))
   {
       vty_out(vty, " ERROR: Interface-vlan should be same with management-vlan!%s", VTY_NEWLINE);
       return CMD_WARNING;
   }
   /*begin modified by wanghuanyu */
	vty->para.para_integer = strtoul(argv[0], NULL, 0);
	sprintf(szifname,"%s%d",IF_L3VLAN_NAMEPREFIX,vty->para.para_integer);
	IF_GetByIFName(szifname,&(vty->ifindex));
	vty->node = VLAN_INTERFACE_NODE;
	/*end modified by wanghuanyu */
   return CMD_SUCCESS;
}


6. 在cmd_interface_init(void)的7813行安装触发命令

 install_element(CONFIG_NODE, &access_vlan_interface_cmd);


至此安装节点完毕,以下是在该节点下添加命令

7. 在void cmd_interface_init(void)中添加命令(以ip_dynamic_cmd 命令为例)

install_element(VLAN_INTERFACE_NODE, &ip_dynamic_cmd);

至此可正常使用
(注:在source insight 的install_element(CONFIG_NODE, &access_vlan_interface_cmd);中,选中access_vlan_interface_cmd按下shift+F3可跳转到DEFUN(触发命令))
 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值