Zebra-vtysh source code analysis and Transformation (3): Add custom commands

Source: Internet
Author: User
Tags service flag snmp

 

1. View Introduction

From the analysis in the previous articles, we can see that all commands are included in the node, which is visible in the CLI format of common routers or switches of Cisco or H3, A node corresponds to a view ). Common views include common views, management views, file system views, configuration views, interface configuration views, and VLAN views.

In the source code of zebra-vtysh, enable view and configuration view are implemented. As shown in:

/ # 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 terminalCLI# 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 terminalCLI(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)# quitCLI(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 modeCLI(config-device)# CLI(config-device)#

If you want to add your own commands, you can add commands to the original view (that is, add commands to the original node), or open your own view, and then add your own commands to the new view.

Add command

Go to the vtysh directory and check the main function of the vtysh_main.c file, that is, everything related to vtysh Initialization is here. Basically, you can complete some basic commands you need here.

In the vtysh_init_vty () function, there is

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

 

Is responsible for initializing the command interface and installing node and command.

For example, you can add your own view as follows:

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

 

(The installed system and Device Configuration view)

/* Added by xyang * system config node **/defun (system_config, vtysh_sysconfig_cmd, "system-config", sys_cmd_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_cmd_str" \ n ") {// vty_out (vty, "testing by xyang. % s ", vty_newline); vty-> node = devconfig_node; return cmd_success;} defun is defined as/* defun for vty command interafce. little bit hacky ;-). */# define defun (funcname, comment name, comment STR, helpstr) \ int funcname (struct comment _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 and devconfig_node should be added to Enum node_type.

Add your own command at the end of init_cmd.

For example

/*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

The function pointer must be defined first:

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;}

 

In this way, the task of adding node and command is basically completed.

 

Other commands that come with zebra-vtyh can be deleted if you do not want them.

 

(Total)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.