????????????libcli

ACSolo  发布于2个月前 69 阅读 评论(0)


???libcli

libcli????????????????????????????????????????????????

libcli??????

  1. ????????????????????????????????
  2. ??telnet??????vty cli??
  3. ??????
system>
system>sy
system>system-view
system]dis
system]display  ?
 device                   Device
 user-name                User Name
system]display  de
system]display  device
Run command display device.
system]display  device  ?
 slot                     Slot
 <cr>
system]display  device  s
system]display  device  slot  2
Run command display device slot 2.
system]dis
system]display  ?
 device                   Device
 user-name                User Name
system]display  us
system]display  user-name  ?
 STRING<1-32>             User Name, length range from 1 to 32.
system]display  user-name  jungle ?
 <cr>
system]display  user-name  jungle
Run command display user-name jungle.
system]dia
system-diagnose]?
 display                  Display
 quit                     Quit from current view
system-diagnose]q
system]q
system>

????

libcli???libcli.a?icli.h???????????????????????icli.h??????
libcli.a??lib32????
???demo??????windows?????????????????????????

??libcli?

????libcli???????icli.h??????????libcli.a???demo?command_main.cpp??

libcli???????

  1. ????cmd_init()???cli?.
  2. ????cli????cmd_main_entry().
int main()
{	
	/* ??? */ 	cmd_init();

	/* ????? */ 	reg_cmd();

	reg_cmd2();
	
	/* ????????? */ 	(void)cmd_regcallback(MID_TEST, cmd_callback);
	
	/* ????? */ 	_beginthreadex(NULL, 0, cmd_main_entry, NULL, NULL, NULL);

	for ( ; ; )
	{
		Sleep(1);
	}

	return 0;
}
  1. ??????cli???????????????????????
    ?????
#define MID_TEST 1 enum CMO_TBLID_EM {
	CMO_TBL_SHOW = 0,
	CMO_TBL_CFG,		
};

enum CMO_SHOW_ID_EM {
	CMO_SHOW_SHOW_DEVICE = CMD_ELEMID_DEF(MID_TEST, CMO_TBL_SHOW, 0),
	CMO_SHOW_SHOW_SLOTID,
	CMO_SHOW_SHOW_NAME,
};

ULONG reg_cmd()
{
	CMD_VECTOR_S * vec = NULL;
		
	/* ????????1: ??????? */
	CMD_VECTOR_NEW(vec);
	
	/* ????????2: ????? */
	/* 1 display */
	cmd_regelement_new(CMD_ELEMID_NULL, 			CMD_ELEM_TYPE_KEY,    "display",  		"Display", vec);
	/* 2 device */
	cmd_regelement_new(CMO_SHOW_SHOW_DEVICE, 		CMD_ELEM_TYPE_KEY,    "device",   		"Device",  vec);
	/* 3 slot */
	cmd_regelement_new(CMD_ELEMID_NULL, 			CMD_ELEM_TYPE_KEY,    "slot",   		"Slot",  vec);
	/* 4 slot-id */
	cmd_regelement_new(CMO_SHOW_SHOW_SLOTID, 		CMD_ELEM_TYPE_INTEGER,"INTEGER<0-8>",   "Slot ID",  vec);
	/* 5 user-name */
	cmd_regelement_new(CMD_ELEMID_NULL, 			CMD_ELEM_TYPE_KEY,    "user-name",   	"User Name",  vec);
	/* 6 user-name-value */
	cmd_regelement_new(CMO_SHOW_SHOW_NAME, 			CMD_ELEM_TYPE_STRING, "STRING<1-32>",   "User Name, length range from 1 to 32.",  vec);
	
		
	/* ????????3: ????? */
	cmd_install_command(MID_TEST, VIEW_GLOBAL, " 1 2 ", vec); /* display device */
	cmd_install_command(MID_TEST, VIEW_GLOBAL, " 1 2 3 4 ", vec); /* display device slot <slot-id> */
	cmd_install_command(MID_TEST, VIEW_GLOBAL, " 1 5 6 ", vec); /* display user-name <user-name-value> */
	
	/* ????????4: ??????? */
	CMD_VECTOR_FREE(vec);

	return 0;
}

??????cmd_install_command?????????????????????????????cmd_regelement_new????????????????????

  1. ????cmd_regcallback?????????
ULONG show_callback(VOID *pRcvMsg) {
	ULONG iLoop = 0;
	ULONG iElemNum = 0;
	ULONG iElemID = 0;
	VOID *pElem = NULL;
	ULONG vtyId = 0;
	ULONG SlotId = 0xFFFFFFFF;
	ULONG isDevice = 0;
	ULONG isUsername = 0;
	CHAR szName[32] = {0};
	
	vtyId = cmd_get_vty_id(pRcvMsg);
	iElemNum = cmd_get_elem_num(pRcvMsg);

	for (iLoop = 0; iLoop < iElemNum; iLoop++)
	{	
		pElem = cmd_get_elem_by_index(pRcvMsg, iLoop);
		iElemID = cmd_get_elemid(pElem);

		switch(iElemID)
		{
			case CMO_SHOW_SHOW_DEVICE:	
				isDevice = 1;
				break;
			case CMO_SHOW_SHOW_SLOTID:
				SlotId = cmd_get_ulong_param(pElem);				
				break;
			case CMO_SHOW_SHOW_NAME:
				isUsername = 1;
				cmd_copy_string_param(pElem, szName);				
				break;				
			default:
				break;
		}
	}

	if (SlotId != 0xFFFFFFFF)
	{
		vty_printf(vtyId, "Run command display device slot %u.\r\n", SlotId);
		return 0;
	}

	if (isDevice)
	{
		vty_printf(vtyId, "Run command display device.\r\n");
		return 0;
	}

	if (isUsername)
	{
		vty_printf(vtyId, "Run command display user-name %s.\r\n", szName);
		return 0;
	}
	
	return 0;

}ULONG cmd_callback(VOID *pRcvMsg) {
	ULONG iRet = 0;
	ULONG iTBLID = 0;

	/* ?????tblid??????????????? */ 	iTBLID = cmd_get_first_elem_tblid(pRcvMsg);
		
	switch(iTBLID)
	{
		case CMO_TBL_SHOW:
			iRet = show_callback(pRcvMsg);
			break;
		case CMO_TBL_CFG:
			iRet = cfg_callback(pRcvMsg);
			break;

		default:
			break;
	}

	return iRet;
}

(void)cmd_regcallback(MID_TEST, cmd_callback);

PS: ????????show_callback??????????

??libcli???

  1. https://gitee.com/jungle/online-judge-private ??????libcli?telnet???vty?????

 
 
 
 
Vote: I like it 0 Vote: I do not like it

Reply: Reply it before Login