# smartx-cli使用指南
# smartx-cli简介
SmartX v3.4.28版本开始新增命令行工具smartx-cli,客户可以通过命令行窗口、python、shell脚本等工具调用smartx-cli命令行指令,对SmartX进行一些自动化的运维操作,详细情况参见cli api。
# 通过命令行与smartx进行交互
查看命令行帮助:help

命令行交互过程:根据指定的组件ID启动扩展组件

# 通过python脚本实现一键启动扩展组件
一键启动扩展组件示例包含start.bat和demo.py。双击start.bat即可启动。
示例start.bat源码
python demo.py
1
示例demo.py源码
import wexpect
child = wexpect.spawn('smartx-cli.exe login 315000000') #315000000为资金账号
child.expect("smartx>password:")
child.sendline('12345678') #12345678为账号密码
child.expect("smartx>\[0000\]登录成功")
child.sendline("startExtension demo123-local") #demo123-local为扩展组件的ID(从组件列表ID列查看)
child.expect("smartx>\[0000\]启动成功")
child.sendline("exit") #启动完成,退出脚本
child.expect("\[0000\]exit completed")
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 通过python脚本实现扩展组件运行守护程序
一键启动扩展组件运行守护程序示例包含start.bat和demo.py。双击start.bat即可启动守护程序,每10秒检测一次扩展组件运行状态,组件未运行,则自动拉起运行。
示例start.bat源码
python demo.py
1
示例demo.py源码
import time
import wexpect
import logging
#设置日志框架
logger = logging.getLogger("auto") # 初始化日志收集器
logger.setLevel("DEBUG") # 设置日志收集器级别
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(process)d - %(filename)s - %(funcName)s - %(lineno)d - %(message)s')
handler = logging.FileHandler("pylog.log") # 初始化日志处理器 - 文件输出(指定位置使用绝对路径,默认当前目录下)
handler.setLevel("DEBUG") # 设置日志处理器级别 默认warning
handler.setFormatter(formatter)
logger.addHandler(handler) # 添加handler
# 注意:上面level设置的是显示的最低严重级别,小于level设置的最低严重级别将不会打印出来
child = wexpect.spawn('smartx-cli.exe login 315000000') #315000000为资金账号
def init():
logger.debug('----------------登录smartx-------------------')
child.expect("smartx>password:")
child.sendline('12345678') #12345678为账号密码
child.expect("smartx>\[0000\]登录成功")
logger.debug('登录成功')
def startExtension():
logger.debug('启动扩展组件')
child.sendline("startExtension demo123-local") #demo123-local为扩展组件的ID(从组件列表ID列查看)
child.expect("smartx>\[0000\]启动成功")
logger.debug('启动成功')
def check():
logger.debug('检查组件运行状态')
child.sendline("status extension demo123-local") #demo123-local为扩展组件的ID(从组件列表ID列查看)
child.expect("smartx>\[")
logger.debug('状态返回:smartx>[%s',child.buffer)
if("extension正在运行" in child.buffer):
logger.debug('不需要启动')
elif("extension已停止" in child.buffer):
logger.debug('已停止,需要启动')
startExtension()
else:
logger.debug('状态获取异常')
def exit():
logger.debug('退出')
child.sendline("exit") #启动完成,退出脚本
child.expect("\[0000\]exit completed")
logger.debug('退出完成')
try:
init()
while True:
check()
time.sleep(10) #间隔10秒检查一次
exit()
except Exception as err:
logger.error(err,exc_info=True, stack_info=True)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Mac系统通过Shell脚本实现扩展组件自动运行
拷贝shell脚本auto.sh到/Applications/SmartX.app/Contents/MacOS下面,打开命令行,执行shell脚本即可自动拉起SmartX并启动扩展组件
#!/usr/bin/expect
spawn smartx-cli login 315000000
expect {
"smartx>password:" {
send "123456\n";
expect "smartx>\[0000\]登录成功"
send "startExtension demo123-local\n";
expect "smartx>\[0000\]启动成功"
send "exit\n";
interact
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Q&A问题解答
# 1、windows系统,python 缺少wexpect库,怎么安装?
命令行输入
pip list列出当前已安装的python库,如果没有wexpect,则执行pip install wexpect
# 2、安装wexpect报错,安装失败?
由于本地pip cache有缓存导致加载的包有问题,清除appData下的相关pip文件:
%USERPROFILE%\AppData\Local\pip(C:\Users\当前用户\AppData\Local\pip)%USERPROFILE%\AppData\Roaming\pip(C:\Users\当前用户\AppData\Roaming\pip)
删除之后,重新执行
pip install wexpect安装
# 3、Mac系统配置环境变量?
编辑用户目录下的文件.bash_profile 在文档末尾增加下面三行:
PATH=/Applications/SmartX.app/Contents/bin/cpython/bin:/Applications/SmartX.app/Contents/bin/cpython/Scripts:/Applications/SmartX.app/Contents/MacOS:${PATH}export PATHalias python="/Applications/SmartX.app/Contents/bin/cpython/Scripts/python"
增加完了,保存退出。
命令行执行
source ~/.bash_profile让环境变量生效。
# 4、Mac下怎么执行自动化脚本?
- 拷贝shell脚本
auto.sh到/Applications/SmartX.app/Contents/MacOS下面。 - 打开命令工具依次输入命令,
cd /Applications/SmartX.app/Contents/MacOS,auto.sh - 注意
auto.sh涉及到相关的账号设置,需要根据自己的情况调整参数。