robotframeworkを使ってTelnetに接続してコマンドのログを取得する方法に調べることがありました。
今回はrobotframeworkでtelnetを呼び出す方法についてのメモ。
robotframeworkでtelnetを行うサンプルプログラムです。
サンプルプログラム
tenletに接続してコマンドを実行する簡単なプログラムです。
# telnet_test.robot
*** Settings ***
Library Telnet
*** Variables ***
${ip_address} = 192.168.1.100
${username} = cisco
${pass} = cisco
*** Keywords ***
Telnet接続を行う
Open Connection ${ip_address}
Login ${user_name} ${pass}
ログ表示でmoreを表示しない
Write terminal length 0
show ip routeコマンド実行
Write show ip route
${output}= Read delay=3s
Log To Console ${¥n}${output}
*** Test Cases ***
Telnet接続を行う
ログ表示でmoreを表示しない
show ip routeコマンド実行
Telnetはrobotframeworkの標準ライブラリですのでそのままで使用できます。
Open Connectionで接続をし、Loginでユーザ名やパスワードを入力します。
ログが「–more–」で全て表示されないのを防ぐのにterminal length 0をコマンドの前にあらかじめ使用しています。
コマンドを実行したい場合、WriteとReadを使い、結果をLog to Consoleに出力しています。
基本的なTelnetの使い方になりますが、不要な箇所を削除しているのでわかりやすいのではないかと思います。
お役に立てれば嬉しいです。