2015-03-31 98 views
0

执行命令通过ssh库中robotframework通过ssh库中robotframework

执行命令我使用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 
============================================================================== 

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

回答

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中的字符串。