LoginSignup
2
1

More than 3 years have passed since last update.

Robot Framework 使ってみる②

Last updated at Posted at 2019-09-06

リモートホストでコマンド実行してみる

Robot Framework 使ってみる①
やってみたいことの1番目:vyosのshowコマンドの出力結果を取得する に挑戦する。

http://robotframework.org/SSHLibrary/SSHLibrary.html
execute/read/start/write あたりでコマンド発行とその結果取得ができそう。

手動で show version を実行すると以下のようになる。

show version
vyos@vyos:~$ show version
Version:      VyOS 1.1.8
Description:  VyOS 1.1.8 (helium)
Copyright:    2017 VyOS maintainers and contributors
Built by:     maintainers@vyos.net
Built on:     Sat Nov 11 13:44:36 UTC 2017
Build ID:     1711111344-b483efc
System type:  x86 64-bit
Boot via:     disk
Hypervisor:   KVM
HW model:     KVM
HW S/N:       Not Specified
HW UUID:      76DF13EB-09FB-88F7-4C03-EC12D5287D9E
Uptime:       04:26:44 up 100 days, 13:45,  1 user,  load average: 0.01, 0.03, 0.05

vyos@vyos:~$

まず成功経験にすがりつく

ssh接続の確認で execute command での実行結果を変数に格納できたので模倣する。

execute command

test03.robot
# cat test03.robot
*** Settings ***
Library  SSHLibrary

*** Variables ***
${user}       vyos
${password}   vyos
${ip1}  172.17.0.2
${ip2}  172.17.0.3

*** Test Cases ***
ssh test of vyos
    vyosに接続
    vyosにログイン
    ${output} =  Execute Command  echo SSH to vyos is OK!
    Log To Console  ${\n}${output}
    Should Be Equal  ${output}  SSH to vyos is OK!

show version
    ${out1} =  Execute Command  show version
    Log Variables
    Close All Connections

*** Keywords ***
vyosに接続
    Open Connection  ${ip1}
vyosにログイン
    Login  ${user}  ${password}
#

で、実行。

result
# robot test03.robot
==============================================================================
Test03
==============================================================================
ssh test of vyos                                                      ...
SSH to vyos is OK!
ssh test of vyos                                                      | PASS |
------------------------------------------------------------------------------
show version                                                          | PASS |
------------------------------------------------------------------------------
Test03                                                                | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output:  /root/output.xml
Log:     /root/log.html
Report:  /root/report.html
#

お、PASSした!
log 見ると
001.png
何も入っていない。。。ですよね~世の中そんな甘くないですよね~
チートシートでは、Start Commandの説明が「リモートコマンドの実行を開始し、終了を待たずに処理を戻す」となってる。execute は終了するまで待つってことか?とりあえず試す。

Start Command

show version
    ${out1} =  Start Command  show version
    Log Variables

結果はPASSしたけど、${out1} = None となって期待動作にはならず。終了待たなかったからNoneを入れたのか???気持ちを切り替えて次。

Read Command Output

show version
    Start Command  show version
    ${out1} =  Read Command Output
    Log Variables

結果は execute command の時と一緒。echoなどで一行出力だと期待通りになるっぽいが、、、複数行は無理なのか?

Write & Read until

show version
    Write  show version
    ${out} =  Read Until  ~$
    Log Variables
    Close All Connections

002.png
きた!なんか行末に改行コードか制御文字か変なの付いているけど一応とれた。

一応できた。。。
もうちょっと試行錯誤して追記しよう

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1