一、Klish框架简介:
命令行界面(CLI)应用程序被广泛使用。无论是网络设备管理、系统配置还是软件调试,命令行界面提供了一种高效和灵活的交互方式。同时为了简化命令行界面的开发过程,Klish框架应运而生。Klish 是一个基于 C 语言开发的交互式命令行接口(CLI)工具,它提供了一种简单而灵活的方式来创建和管理命令行界面。Klish 允许开发者定义命令、选项和参数,以及相应的操作和输出。使用 Klish我们可以自由构建自定义的命令行界面,以满足特定的需求。
二、下载源码
https://src.libcode.org/download/klish/
三、安装依赖
sudo apt update
sudo apt install git gcc make autotools-dev autoconf automake libtool libxml2 python3-dev libexpat1-dev libxml2-dev
四、编译运行,因为项目需求我编译的是deb,不需要的话可以直接编译
#!/bin/bash
DIR=$(dirname `readlink -f $0`)
#== use klish commit id : 3705cb2da46247360095c3c63c16fb7eee0717df ==#
prefix="/usr/local/klish"
pushd $DIR/klish
git checkout .
make clean
./autogen.sh || {
echo "autogen klish failed"
exit 1
}
# -enable-debug可以编译出debug版本
./configure --prefix=$prefix --with-libexpat=yes --enable-legacy=yes --with-cpython=3 || {
echo "configure klish failed"
exit 1
}
make || {
echo "make klish failed"
exit 1
}
sudo make install || {
echo "install klish failed"
exit 1
}
sudo checkinstall --requires="libxml2" --pkgversion="2.2" -y || {
echo "build deb failed"
exit 1
}
cp ./klish*.deb ../..
popd
echo "Good, klish build & install(to ${prefix}) finished ..."
exit 0
五、安装运行
dpkg -i klish_2.2_arm64.deb
六、安装运行时需要的依赖
sudo apt install python3(生产环境和编译环境需要一致)
pip3 install urllib3 requests jinja2
七、设置环境变量
#!/bin/bash
# klish source code path
CURRENT_DIR=$(pwd)
#klish mgmt root
export KLISH_MGMT_ROOT=/usr/local/klish/bin
export CLISH_TARGET=$CURRENT_DIR/target
# klish execute script path
export CLISH_ROOT=$CLISH_TARGET/src
# klish command tree path
export CLISH_PATH=$CLISH_TARGET/tree
# klish path
export PATH=/usr/local/klish/bin:$PATH
# klish system name, set default prompt
export SYSTEM_NAME=klish_dev
#run confd
if [ -e "/tmp/konfd.socket" ]; then
konfd_pid=$(ps x | grep konfd | grep -v grep | awk '{print $1}')
kill -9 $konfd_pid
konfd_pid=$(ps -ef | grep konfd | grep -v grep | awk '{print $3}')
kill -9 $konfd_pid
rm /tmp/konfd.socket
rm /var/run/konfd.pid
fi
nohup $KLISH_MGMT_ROOT/konfd >> /var/log/syslog 2>&1 &
# klish alias, you can press cli
alias cli=clish
. ~/.bashrc
echo 'success'
说明
# Klish 说明
## 内部字段
`${__cmd}` 当前命令名称
`${__params}` 参数列表
`${__line}` 相当于 `"${__cmd}${__params}"`
## 关键字
这是一个有层级关系的无序列表
- `<?xml?>`
- `version` 版本
- `'1.0'`
- `encoding` 编码
- `'UTF-8'`
- `<!DOCTYPE CLISH_MODULE>`
- `<!ENTITY>` 实体, 类似于宏定义
- `<CLISH_MODULE>` 模块
- `xmlns`
- `"http://www.dellemc.com/sonic/XMLSchema"`
- `xmlns:xsi`
- `"http://www.w3.org/2001/XMLSchema-instance"`
- `xmlns:xi`
- `"http://www.w3.org/2001/XInclude"`
- `xsi:schemaLocation`
- `"http://www.dellemc.com/sonic/XMLSchema http://www.dellemc.com/sonic/XMLSchema/clish.xsd"`
- `<STARTUP>` 启动. 单个
- `view` 视图
- `default_shebang` 定义脚本语言. 默认是 `/bin/sh`. `<ACTION>` 中 `shebang` 可以重定义当前的执行脚本方式.
- `<PTYPE>` 形参类型
- `name` 名称
- `method` 方法
- `regexp` 正则表达式. 默认
- `integer` 整数. 用`"min..max"`定义
- `select` 选择. 表示从多个参数中选取其中一个. 格式为`"valueOne(ONE)" valueTwo(TWO) valueThree(THREE)"`
- `pattern` 样式. 可以为正则表达式, 整数范围等
- `help` 帮助. 帮助信息
- `<VIEW>` 视图. 汇聚了多条命令
- `name` 名称. 视图唯一标识, 可被引用.
- `prompt` 提示. 命令提示符
- `depth` 深度. 视图的嵌套深度. 默认为 "0"
- `restore` 恢复
- `"none"` 不改变当前视图. 默认
- `"view"` 设置当前视图为本地视图
- `"depth"` 将从指定深度恢复
- `<NAMESPACE>` 命名空间. 必须在 `<VIEW>` 标签内部, 允许从指定的其他视图导入命令
- `ref` 引用. 引用要从中导入命令的视图
- `"hidden-view"` 注释视图
- `"enable-view"`
- `"configure-view"`
- `help` 帮助. 帮助信息
- `prefix` 导入命令的文本前缀
- `"do"`
- `completion` 补全. 指示在命令完成时是否使用导入的命令(按"Tab"键)
- `"true"` 真. 默认
- `"false"` 假
- `<COMMAND>` 命令. 一个命令以 `<COMMAND>` 起始, `<PARAM>` 字段不能作为命令开头. 此标记可以在 [VIEW] 元素的范围内或全局范围内使用. `<COMMAND>` 标签定义命令. 有关其他信息, 请参阅锁定机制.
- `name` 名称
- `help` 帮助. 帮助信息
- `ref` 引用. `ref` 字段用于创建命令别名. 如果在 `<COMMAND>` 定义中使用`ref`字段, 则该命令不是独立的, 而是别名. `ref`包含要为其创建别名的目标原始命令的名称. 如果目标命令属于别名视图之外的另一个视图, 则必须在目标命令名称之后指定目标命令的视图. 命令名称和视图名称之间的分隔符是`@`符号. 有关详细信息和示例, 请参见命令别名页面.
- `view` 视图
- `access` 访问. 此字段控制 `<COMMAND>` 的访问权限. 如果访问被拒绝, 则用户不能使用命令. 通常该字段的内容是任意的. 这意味着真正控制权限的功能可以通过 `<HOOK>` 标签来设置. 默认情况下(内置函数)`access`字段包含授予访问权限的 UNIX 组列表. 这些组由`:`符号分隔. 如果未定义访问字段, 则授予访问权限.
- `args` 实参
- `args_help` 实参帮助. 帮助信息
- `escape_chars`
- `lock` 锁. 一个布尔标志. 它可以启用 (true) 或禁用 (false) 当前命令的锁定机制. 默认为真.
- `"true"` 真. 默认
- `"false"` 假
- `interrupt` 中断. `interrupt`字段指定 `<ACTION>` 脚本是否可由用户中断或不可中断. 如果`interrupt="true"` 那么脚本是可中断的, 否则脚本是不可中断的. 对于不可中断的脚本, SIGINT 和 SIGQUIT 被暂时阻止. 有关详细信息, 请参阅原子操作. `interrupt` 字段从 klish-1.4.0 开始可用.
- `<DETAIL>` 详细. 单个
- `<ACTION>` 动作. 单个
- `shebang` 重定义脚本语言. 默认使用 `<STARTUP>` 中的 `shebang`
- `builtin` 内建
- `"clish_nested_up"` 上一层
- `"clish_close"` 关闭
- `"clish_nop"` 什么也不做
- `<CONFIG>` 配置. 单个
- `priority`
- `"0x0b01"`
- `operation`
- `"dump"`
- `pattern`
- `<PARAM>` 形参
- `name` 名称
- `help` 帮助
- `ptype` 形参类型
- `"SUBCOMMAND"` 子命令
- `"STRING_63"` 63字节字符串
- `"STRING_WITH_PIPE"` 带管道字符串
- `"IP_ADDR"` IP地址字符串
- `"IP_ADDR_MASK"` IP地址掩码字符串
- `"RANGE_0_65535"` 0~65535范围整数
- `"RANGE_1_65535"` 1~65535范围整数
- `"RANGE_0_63"` 0~63范围整数
- `"RANGE_0_255"` 0~255范围整数
- `value` 值. 可显示的子命令名称可以由该字段复制. 例如, 用户可以使用两个子命令 "any"
- `"any"`
- `mode` 模式
- `"common"` 普通. 默认
- `"subcommand"` 子命令. 子命令由其"name"(或"value"字段(如果指定)标识, 可用作可选标志或用于分支. 如果使用了子命令, 则形参的值是其名称(如果指定, 则为"value"字段). 如果形参是可选的且未使用, 则形参的值未定义.
- `"switch"` 选择. 允许从其子形参中选择其中一个
- `optional` 可选. 指定形参是否可选
- `"false"` 假. 默认
- `"true"` 真
- `test` 条件参数. 参数可根据条件来动态使能/禁止
- `<xi:include>`
- `href` 引用
- `"include/pipe.xml"` 文件路径
## 条件参数
-n string
True if the length of string is nonzero.
-z string
True if the length of string is zero.
s1 = s2
True if the strings s1 and s2 are identical.
s1 != s2
True if the strings s1 and s2 are not identical.
s1 < s2
True if string s1 comes before s2 based on the ASCII value of
their characters.
s1 > s2
True if string s1 comes after s2 based on the ASCII value of
their characters.
s1 True if s1 is not the null string.
n1 -eq n2
True if the integers n1 and n2 are algebraically equal.
n1 -ne n2
True if the integers n1 and n2 are not algebraically equal.
n1 -gt n2
True if the integer n1 is algebraically greater than the integer
n2.
n1 -ge n2
True if the integer n1 is algebraically greater than or equal to
the integer n2.
n1 -lt n2
True if the integer n1 is algebraically less than the integer n2.
n1 -le n2
True if the integer n1 is algebraically less than or equal to the
integer n2.
These primaries can be combined with the following operators. The -a
operator has higher precedence than the -o operator.
! expression
True if expression is false.
expression1 -a expression2
True if both expression1 and expression2 are true.
expression1 -o expression2
True if either expression1 or expression2 are true.
( expression )
True if expression is true.