通过机器人框架中的ssh库执行命令

时间:2015-03-31 10:54:23

标签: ssh robotframework

通过robotframework中的ssh库执行命令

我正在使用robotframework连接到远程服务器并在那里启动应用程序。我使用这些关键字:

Create SSH Link
    Open Connection     ${Ip}
    Login               ${Username}     ${Password}

Start Service
    ${cmd} =    "cd " + ${appAddr} + "; ./" + ${appName} + " " + ${appInputArg} + " > /tmp/" + ${appName} "-out.log 2>&1"
    log to console      Going to run command: ${cmd}
    execute command     ${cmd}
    log to console      \nConnected Successfully

Run App
    ${cmd} =    "netstat -putan | grep LISTEN | grep 8898 | awk '{print $7}' | cut -d '/' -f 1"
    ${stdout} =     execute command     ${cmd}
    Run Keyword If          ${stdout} == ${EMPTY}       log to console      \nCommand ${cmd} retruned Error
    Run Keyword Unless      ${stdout} == ${EMPTY}       Start Service    

这是我的测试用例:

Test Connecting to Remote Server
    Create SSH Link
    Run App

输出:

==============================================================================
Scenarios                                                                     
==============================================================================
Scenarios.Test :: First Test                                                  
==============================================================================
Test Connecting to Remote Server                                      
Connected Successfully
| FAIL |
No keyword with name '"netstat -putan | grep LISTEN | grep 8898 | awk '{print $7}' | cut -d '/' -f 1"' found.
------------------------------------------------------------------------------
Scenarios.Test :: First Test                                          | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 0 passed, 1 failed
==============================================================================
Scenarios                                                             | PASS |
0 critical tests, 0 passed, 0 failed
1 test total, 0 passed, 1 failed
==============================================================================

为什么它不会在远程服务器上执行此命令并搜索关键字?

1 个答案:

答案 0 :(得分:2)

问题在于这句话:

${cmd} =    "netstat -putan | grep LISTEN | grep 8898 | awk '{print $7}' | cut -d '/' -f 1"

这不是有效的机器人框架声明。 Robot Framework正在寻找一个关键字,在此声明中找不到任何关键字。

应改为:

${cmd} =    set variable    netstat -putan | grep LISTEN | grep 8898 | awk '{print $7}' | cut -d '/' -f 1

注意:字符串周围不需要双引号。默认情况下,变量是Robot中的字符串。