Quagga添加自己的命令

参考了王斌的文档:http://down.51cto.com/data/621454

TIP:笔者使用的版本是0.99.20

需求:在接口模式下添加一条"ip ospf enable"的命令,什么也不做,只输出一些字符;


Quagga内的视图是在vtysh.h定义的,位置:“vtysh/vtysh.c”,我常接触的主要有以下:

/* Vty node structures. */
static struct cmd_node bgp_node =
{
  BGP_NODE,
  "%s(config-router)# ",
};

static struct cmd_node interface_node =
{
  INTERFACE_NODE,
  "%s(config-if)# ",
};


static struct cmd_node zebra_node =
{
  ZEBRA_NODE,
  "%s(config-router)# "
};


static struct cmd_node bgp_ipv4_node =
{
  BGP_IPV4_NODE,
  "%s(config-router-af)# "
};

static struct cmd_node ospf_node =
{
  OSPF_NODE,
  "%s(config-router)# "
};


我主要用接口视图和OSPF视图,每个模式下会绑定该模式下可以使用的命令,比如,只有接口模式下才有“ip ospf hello-interval”,而ospf模式下没有,这就是视图和命令的关系。需要添加自己的视图可以在这里定义,这里了解下即可,无需修改;


定义“ip ospf enable”命令:

具体说明见王斌的笔记,这里我就直接加代码了,首先是在"ospfd/osfp_vty.c"加DEFUN,具体如下:

DEFUN (interface_ospf_enable,
		interface_ospf_enable_cmd,
		"ip ospf enable",
		"IP Information
"
		"OSPF interface commands
"
		"enable ip ospf router in this Interface
")
{
	vty_out(vty,"%% GO GO Sven%s", VTY_NEWLINE);
	return CMD_SUCCESS;
}

这里和王斌笔记有点不同,如果按照作者的来,可能会看不到最后的提示信息,这里需要加上另外两串字符串,ip和ospf的提示信息,如果只加上enable的提示信息的话,那么你看在敲上“ip ospf ?”的时候可能看到的就是空荡荡的,没提示信息。

将"ip ospf enable"和Interface视图关联起来:

需要在"ospf_vty.c"的ospf_vty_if_init函数内添加以下内容:

 /*My commands*/
  install_element(INTERFACE_NODE,&interface_ospf_enable_cmd);


表示该命令关联的是Interface视图,这样就修改完毕了,先不和ospf交互,该命令啥事也不干,只输出一个字符串




至此代码就修改完毕了,按照上一篇的提示编译安装,然后重启服务即可telnet 2609来查看效果了:

[root@sven siwen.xwd]# netstat -nta|grep 260.
tcp        0      0 0.0.0.0:2601                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:2609                0.0.0.0:*                   LISTEN      
[root@ siwen.xwd]# telnet localhost 2609
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Hello, this is Quagga (version 0.99.20).
Copyright 1996-2005 Kunihiro Ishiguro, et al.


User Access Verification

Password: 
ospfd> en
ospfd> enable 
ospfd# con
ospfd# configure 
% Command incomplete.
ospfd# configure te
ospfd# configure terminal 
ospfd(config)# int
ospfd(config)# interface eth1
ospfd(config-if)# ip os
ospfd(config-if)# ip ospf 
  authentication       Enable authentication on this interface
  authentication-key   Authentication password (key)
  cost                 Interface cost
  dead-interval        Interval after which a neighbor is declared dead
  enable               enable ip ospf router in this Interface
  hello-interval       Time between HELLO packets
  message-digest-key   Message digest authentication password (key)
  mtu-ignore           Disable mtu mismatch detection
  network              Network type
  priority             Router priority
  retransmit-interval  Time between retransmitting lost link state advertisements
  transmit-delay       Link state transmit delay
ospfd(config-if)# ip ospf en
ospfd(config-if)# ip ospf enable 
% GO GO Sven
ospfd(config-if)#


可以看到"ip ospf enable"命令已经加入进去,并且有提示信息,执行完毕后,会输出字符串。


原文地址:https://www.cnblogs.com/jiangu66/p/3192312.html