Zebra-VTYSH源码分析和改造(三):添加定制命令

2023-05-16

 

一 视图介绍

由上面几篇文章分析可见,所有的命令都是包含在node中的,根据Cisco或者H3常见路由器或者交换机的CLI格式可见,一个node就对应着一个视图(View)。常用的视图包括:普通视图,管理视图,文件系统视图,配置视图,以及接口配置视图和VLAN视图等。

在Zebra-VTYSH源码中,实现了的有Enable视图和配置视图。如下图所示:

/ # vtysh 

Copyright 2010-2011 IBM Co., Ltd.

CLI> enable 
CLI# 
  clear        Reset functions
  configure    Configuration from vty interface
  copy         Copy from one file to another
  debug        Debugging functions (see also 'undebug')
  disable      Turn off privileged mode command
  end          End current mode and down to previous mode
  exit         Exit current mode and down to previous mode
  list         Print command list
  no           Negate a command or set its defaults
  ping         send echo messages
  quit         Exit current mode and down to previous mode
  show         Show running system information
  start-shell  Start UNIX shell
  telnet       Open a telnet connection
  terminal     Set terminal line parameters
  traceroute   Trace route to destination
  undebug      Disable debugging functions (see also 'debug')
  write        Write running configuration to memory, network, or terminal
CLI# configure terminal 
CLI(config)# 
  access-list    Add an access list entry
  bgp            BGP information
  debug          Debugging functions (see also 'undebug')
  device-config  Device configuration
  dump           Dump packet
  enable         Modify enable password parameters
  end            End current mode and down to previous mode
  exit           Exit current mode and down to previous mode
  hostname       Set system's network name
  interface      Select an interface to configure
  ip             IP information
  ipv6           IPv6 information
  key            Authentication key management
  list           Print command list
  log            Logging control
  no             Negate a command or set its defaults
  password       Assign the terminal connection password
  route-map      Create route-map or enter route-map command mode
  router         Enable a routing process
  system-config  System and management configuration
  username
  write          Write running configuration to memory, network, or terminal
CLI(config)# system-config 
CLI(config-system)# 
  access                   Set CPE access ND flag
  admin-idle-time          Set system idle time
  admin-psw                Set system administrator password
  admin-username           Set system administrator username
  connection-mode          Set network connection mode : static and dynamic
  datetime                 Set date time (format:2000-01-01 00:00:00)
  default-gateway          Set system's network default gateway
  dns-server-1             Set system network DNS server 1
  dns-server-2             Set system network DNS server 2
  exit                     Exit current mode and down to previous mode
  factory-defaults         Restore ALL configure to factory default values( 0: reset all 1: reset with network parameters unchanged)
  hostname                 Set system's network name
  image-upgrade            Upgrade image via ftp method
  ip                       Set system ip address and netmask
  list                     Print command list
  managment-ip-range       Set management IP range and netmask
  managment-ip-range-flag  Set management IP range service flag
  mgr-vlan-id              Set management VLAN ID
  ntpserver                Set NTP server
  quit                     Exit current mode and down to previous mode
  reset                    Reset system
  snmp-refresh-time        Set SNMP service refresh time cycle
  snmp-rwcommunicty        Set SNMP read/write community
  snmp-service             Set SNMP service enable or disable
  snmp-trap-ip1            Set SNMP trap ip 1 address
  snmp-trap-ip2            Set SNMP trap ip 2 address
  snmp-trap1-ip-flag       Set SNMP trap ip 1 service flag(enable/disable)
  snmp-trap2-ip-flag       Set SNMP trap ip 2 service flag(enable/disable)
  ssh                      Set ssh service port and timeout values
  ssh-service              Set ssh service flag
  telnet                   Set telnet PORT
  telnet-service           Set telnet service flag
  timesync                 Set time sync service flag
  timezone                 Set time zone (0:ShangHai,1:ChongQing)
CLI(config-system)# quit
CLI(config)# device-config 
CLI(config-device)# 
  exit                       Exit current mode and down to previous mode
  list                       Print command list
  port-mirror-analysis-port  Device configuration: Set analysis port(1: eth1 2: eth2)
  port-mirror-flag           Device configuration: Enable or disable port mirror service(0:disable,1:enable)
  port-mirror-packet         Device configuration: Set packet type to be mirrored(1:Import & Export 2: Import 3: Export)
  port-mirror-port           Device configuration:Set port to be mirrored
  port1-rate                 Device configuration: set duplex mode and import/export/broadcast/unkown/multicast rate limit.
  port2-rate                 Device configuration: set duplex mode and import/export/broadcast/unkown/multicast rate limit.
  quit                       Exit current mode and down to previous mode
CLI(config-device)# 
CLI(config-device)#

如果想要添加自己的命令,可以在原有的视图上增加(也就是在原有的node中增加commands),或者新开自己的视图,然后在新视图中添加自己的commands。

二 添加命令

进入vtysh目录中,查看vtysh_main.c文件的main函数,也就是和vtysh初始化相关的一切都在这里,基本上在这里可以完成你需要的一些基本命令。

在函数vtysh_init_vty()中,有个

/* Initialize command interface. Install basic nodes and commands. */
Void cmd_init (int terminal)


 

的函数,就是负责初始化command接口,安装node和命令的。

比如你就可以添加自己的视图如下:

/*Added by xyang*/
	install_element (CONFIG_NODE, &vtysh_sysconfig_cmd);
	install_element (CONFIG_NODE, &vtysh_devconfig_cmd);


 

(其中,安装的system和device配置的视图)

/*Added by xyang
* system config node*
*/
DEFUN (system_config,
       vtysh_sysconfig_cmd,
       "system-config",
       SYS_CFG_STR
       "\n")
{  
  //vty_out (vty, "testing by xyang.%s", VTY_NEWLINE);

   vty->node = SYSCONFIG_NODE;

  return CMD_SUCCESS;
}
DEFUN (device_config,
       vtysh_devconfig_cmd,
       "device-config",
       DEV_CFG_STR
       "\n")
{  
  
  //vty_out (vty, "testing by xyang.%s", VTY_NEWLINE);

   vty->node = DEVCONFIG_NODE;

  return CMD_SUCCESS;
}
DEFUN定义为:
/* DEFUN for vty command interafce. Little bit hacky ;-). */
#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
  int funcname (struct cmd_element *, struct vty *, int, char **); \
  struct cmd_element cmdname = \
  { \
    cmdstr, \
    funcname, \
    helpstr \
  }; \
  int funcname \
  (struct cmd_element *self, struct vty *vty, int argc, char **argv)


 

SYSCONFIG_NODE和DEVCONFIG_NODE要添加进enum node_type{}中去。

最后就要在init_cmd的最后加进自己的command了

比如

/*add commands to system config node
  * added by xyang @ 2012-02-01*
  */
  	/*management network settings*/	
  
	install_element (SYSCONFIG_NODE, &vtysh_system_cfg_ip_cmd);//ip and subnet mask

其中,函数指针需要定义先:

DEFUN (vtysh_system_cfg_ip,
	 vtysh_system_cfg_ip_cmd,
	 "ip ADDRESS NETMASK",
	 "Set system ip address and netmask\n")
{
	applyCfg(argv[0],"IPADDR");
	applyCfg(argv[1],"NETMASK");
	system(NETWORK_SETTING_SCRIPT);	
	return CMD_SUCCESS;
}


 

这样,基本上完成了添加node和命令的任务了。

 

其他Zebra-VTYH自带的命令如果不想要的话删除掉就行了。

 

(总完)



 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Zebra-VTYSH源码分析和改造(三):添加定制命令 的相关文章

  • 架构师成长之路工具篇(1):markdown撰写文档

    今天笔者想说的工具就是markdown xff0c 正所谓工欲善其事必先利其器 xff0c 选择高效的工具自然能提升工作效率 笔者使用的markdown工具是 xff1a typora word太重 xff0c 太复杂 xff0c 在写文档
  • 如何优雅的退出qemu虚拟环境

    在console环境下 xff0c 先 按 ctrl 43 a xff0c 释放之后再按 x 键 既可terminate qemu 注 xff1a 1 a 和 x 均为小写 2 必须先释放ctrl 43 a 之后 再按x键
  • xmake经验总结1:解决c++ future/promise抛出std::system_error的问题

    1 背景 1 1 场景 编译器 xff1a gcc 9 4 运行系统 xff1a Ubuntu 20 04 4 LTS xmake v2 6 7 场景 xff1a 其大致场景是使用c 43 43 的future promise功能 xff0
  • 神经网络实现手写数字识别(MNIST)

    一 缘起 原本想沿着 传统递归算法实现迷宫游戏 gt 遗传算法实现迷宫游戏 gt 神经网络实现迷宫游戏的思路 xff0c 在本篇当中也写如何使用神经网络实现迷宫的 xff0c 但是研究了一下 xff0c 感觉有些麻烦不太好弄 xff0c 所
  • 【AI】Auto-GPT:当时代抛弃你的时候,不会和你说一声再见!

    当时代抛弃你的时候 xff0c 不会和你说一声再见 xff01 Auto GPT Code Auto GPT 一个全自动化的GPT 4 Collecting BOTAI Auto GPT 官网网站
  • 解决visio对象在word中显示不全的问题

    作为一个软件工程师 xff0c 编写技术文档是常有的事情 xff0c 使用visio绘制各种图形 如 xff0c 流程图 xff0c 结构图 xff0c 框架图 xff0c 状态图等等 也是再正常不过的事情 如果我们在word中撰写文档时
  • git submodule使用以及注意事项

    一 背景 在平时的软件开发过程中常常会有这样的场景 xff0c 自己负责的某个模块会依赖其他模块或者第三方的library 这时你自己的模块是一个独立的代码仓库 xff0c 你想要实现这样一种功能 xff0c 当你从你的模块的代码仓库里把代
  • Webpack5 - 基本使用

    一 webpack有何作用 webpack是一个Javascript应用程序的模块打包器 它可以递归地构建一个应用程序的模块依赖关系图 xff0c 然后将所有模块打包在一起 为什么需要模块打包器 xff1a 现在的应用程序模块文件很多 xf
  • Vue.js - VueRouter的Hash与History模式 / 手写VueRouter

    一 Hash与History模式 Hash模式History模式url地址外观http localhost 8081 abouthttp localhost 8080 about原理基于锚点 xff0c 监听锚点变化时触发的onhashch
  • Vue.js - Vue.js响应式原理(1/2)

    一 数据驱动 数据响应式 xff1a 数据改变 xff0c 则视图改变 xff0c 避免频繁的Dom操作 xff0c 提高运行效率 双向绑定 xff1a 数据改变 xff0c 则视图改变 xff1b 视图改变 xff0c 则数据也随之改变
  • Vue.js - 模拟Vue.js响应式原理(2/2)

    项目仓库 xff1a https gitee com big right right vue responsive tree master L8 一 类的说明 Vue类 xff1a 保存传入的选项数据 xff0c 把选项data中的成员注入
  • .NET用NCO连接SAP RFC---写数据到SAP

    1 环境 xff1a a win7 43 64位操作系统 b VS2012 c nco3 0 xff08 64bit 下载网址 xff1a http www dllbang com dll sapnco dll xff09 xff0c d
  • .NET Framwork,C#入门开发教程,零基础必看

    初识 NET Framwork和开发过程 一 什么是 NET Framework NETFramework是一个开发平台 xff0c 可以在其上使用多种语言开发程序 xff1a 如C xff0c VB xff0c C 43 43 xff08
  • 如何清除IIS缓存

    问题描述 xff1a 在IIS的默认网站下创建了一个虚拟目录名A xff0c 然后删除这个虚拟目录 后来需要重新创建一个同名虚拟目录 xff0c 映射的还是原来的项目文件 xff0c 只是项目文件的物理路径发生了变化 xff0c 这个新虚拟
  • jqGrid 多选复选框 编辑列

    1 首先看一下效果 2 html代码 lt table id 61 34 grid table 34 gt lt table gt 3 在 function 方法中 xff0c 写如下方法 xff0c 用json数据填充jqGrid xff

随机推荐