1. 大写字母
大写字母表示该命令的该部分必须输入一个字符串参数,例如命令:
epon loid LOID<1-24> [PASSWORD<1-12>]
unsigned char val[25] = {0};
if(strlen(argv[0]) > 24)
{
vty_out(vty, "Loid is too long, make sure that it is between 1-24 characters%s",VTY_NEWLINE);
return CMD_WARNING;
}
if(2 == argc)
{
if(strlen(argv[1]) > 12)
{
vty_out(vty, "Passwd is too long, make sure that it is between 1-12 characters%s",VTY_NEWLINE);
return CMD_WARNING;
}
}
sprintf(val, argv[0], strlen(argv[0]));
if(2 == argc)
{
memset(val, 0, sizeof(val));
sprintf(val, argv[1], strlen(argv[1]));
}
unsigned char name[64]="\0";
if(strlen(argv[0]) > 64 || strlen(argv[0])<1 )
{
vty_out(vty, "Name length error, make sure it is between 1-64 characters%s",VTY_NEWLINE);
return CMD_WARNING;
}
sprintf(name, argv[0], strlen(argv[0]))
2. A.B.C.D和A.B.C.D/M
A B C D 表示IP地址,M表示掩码;例如命令:
ip address A.B.C.D A.B.C.D [A.B.C.D]
struct in_addr ip, msk, gw;
int has_gw = 0, ret, i;
inet_aton(argv[0], &ip);
inet_aton(argv[1], &msk);
if (argc > 2)
{
has_gw = 1;
inet_aton(argv[2], &gw);
}
if ((inet_aton(argv[0], &ip) == 0) || !valid_ip(ip))
{
vty_out(vty, " ERROR: invalid IP address : %s%s", argv[0], VTY_NEWLINE);
return CMD_WARNING;
}
if ((inet_aton(argv[1], &msk) == 0) || !valid_subnet_mask(ip, msk))
{
vty_out(vty, " ERROR: invalid IP address : %s or invalid Subnet mask : %s%s", argv[0], argv[1], VTY_NEWLINE);
return CMD_WARNING;
}
if (has_gw)
{
if ((inet_aton(argv[2], &gw) == 0) || !valid_gateway(ip, msk, gw))
{
vty_out(vty, " ERROR: invalid gateway address : %s%s", argv[2], VTY_NEWLINE);
return CMD_WARNING;
}
}
if((ip.s_addr==info.netif.ip.s_addr)&&( msk.s_addr==info.netif.subnet.s_addr)&&(0==has_gw))
{
return CMD_SUCCESS; //殍???IP??Masko?-?IP??Mask?囗???2???DD殍??
}
if((ip.s_addr==info.netif.ip.s_addr)&&( msk.s_addr==info.netif.subnet.s_addr)
&&(1==has_gw)&&(gw.s_addr==info.netif.gateway.s_addr))
{
return CMD_SUCCESS; //殍???IP??Mask??DNSo?-?IP??Mask??DNS?囗???2???DD殍??
}
//end modify by zhouguanhua 2012-12-3 of EPN204QID0008
memset(&ipc_set, 0, sizeof(ipc_network_t));
safe_strncpy(ipc_set.hostname, info.hostname, sizeof(ipc_set.hostname));
safe_strncpy(ipc_set.interface_state, info.interface_state, sizeof(ipc_set.interface_state));
ipc_set.ip_proto = IP_PROTO_STATIC;
ipc_set.netif.ip = ip;
ipc_set.netif.subnet = msk;
if (has_gw)
{
ipc_set.netif.gateway = gw;
}
else
{
ipc_set.netif.gateway.s_addr = info.netif.gateway.s_addr;
}
3. 圆括号()和竖直线 |
圆括号一般和竖直线配合使用。圆括号括起来的部分表示这部分命令有几个用竖直线分隔开的可选项,您必须选择其中一项。
if(strstr(ENABLE_STR, (char *)argv[0]))
{
iLoopback = ENABLE;
}
else if(strstr(DISABLE_STR, (char *)argv[0]))
{
iLoopback = DISABLE;
}
else
{
vty_out(vty, " ERROR: set loopback paras error!%s", VTY_NEWLINE);
return CMD_WARNING;
}
注:在这种情况下,括号内的字符无论是大写或是小写,都是当做string处理的,并且还有一种处理方式如下
if (!strcmp(argv[0], "all"))
{
vg = NULL;
}
else
{}
不过这里的强制转换的具体作用还有待研究
4. 中括号[]
中括号中的参数表示可输入可不输入。
已在2中举例
5. 尖括号和数值范围
尖括号和数值范围表示输入的参数的取值范围在两个数值之间的某个数
DEFUN (mac_address_timer_aging,
mac_address_timer_aging_cmd,
"mac-address timer aging <45-458>",
"Configure MAC address\n"
"Configure timer\n"
"Global aging time\n"
"Global aging time (second)")
{
unsigned int aging_time = MAC_AGINGTIME_MIN;
mac_setting_t info;
if(strstr(self->string, "no"))
{
aging_time = MAC_AGINGTIME_DISABLE;
}
else
{
aging_time = strtoul(argv[0], NULL, 0);
}
memset(&info, 0, sizeof(info));
info.aging_time = aging_time;
}
6. 端口B:E
B表示开始端口,E表示结束端口
7. ICMP类型T.C
T表示协议,C表示code
8. MAC地址H.H.H
例如:1051.ED5A.034E
9. 小数点开头 .
表示后面可以输入多个参数
拓展
1. 有如mac-address timer aging <45-458>和mac-address timer no-aging这样成对出现的,可如下编写
if(strstr(self->string, "no"))
{
aging_time = MAC_AGINGTIME_DISABLE;
}
else
{
aging_time = strtoul(argv[0], NULL, 0);
}
no代码编写举例
DEFUN (logging_host,
logging_host_cmd,
"syslog host A.B.C.D level <0-7>",
"Commands of configure logging\n"
"Setting of syslog host configuration\n"
"IP Address format is A.B.C.D (where the log messages send)\n"
"Specify logging level\n"
"0--emergenchy,1--alert,2--critical,3--error,4--warning,5--notice,6--informational,7--debug\n")
{
int iRet = IPC_STATUS_OK;
sys_syslog_t info;
struct in_addr host_ip;
char defaultIp[]="0.0.0.0";
memset(&info, 0x0, sizeof(sys_syslog_t));
iRet = get_syslog_value(&info,0);
if(IPC_STATUS_OK != iRet)
{
vty_out(vty, " ERROR: get system log failed!%s", VTY_NEWLINE);
return CMD_WARNING;
}
if(strstr(self->string, "no"))
{
memset(info.syslog_ip,0,sizeof(info.syslog_ip));
strcpy(info.syslog_ip, defaultIp);
}
else
{
inet_aton(argv[0], &host_ip);
if ((inet_aton(argv[0], &host_ip) == 0) || !valid_ip(host_ip))
{
vty_out(vty, " ERROR: invalid IP address : %s%s", argv[0], VTY_NEWLINE);
return CMD_WARNING;
}
safe_strncpy(info.syslog_ip, argv[0], sizeof(info.syslog_ip));
if (argc > 1)
{
info.syslog_degree = strtoul(argv[1], NULL, 0);
}
}
iRet = ipc_set_syslog(&info);
if(IPC_STATUS_OK != iRet)
{
vty_out(vty, " ERROR: set system log failed!%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
ALIAS (logging_host,
no_logging_host_cmd,
"no syslog host",
NO_DESC_STR
"Commands of configure logging\n"
"No send logging messages to remote host\n");
snmp (enable|disable)添加no命令的这种形式,代码可如下编写
int tem_port_id=0;//cpu口的id为0
u_int32_t port_flow_control ;
switch_template_t stTemplateSet;
cvn_global_memset(&stTemplateSet,0,sizeof(stTemplateSet));
if(strstr(self->string, "no"))
{
port_flow_control=0;
}
else
{
if(strstr("enable",(char *)argv[0]))
{
port_flow_control=1;
}
else if(strstr("disable",(char *)argv[0]))
{
port_flow_control=0;
}
}
code