xlogin でコマンド自動投入したあと,手動制御にもどす
xlogin -s
に渡すexpect スクリプトを書けばよい.
設定投入は自動でやって,温かみのある commit
を打ちたい場合などにつかう.( そんなのcommit
まで自動でやれよ,という話ではある)
jlogin の例
log_user 1 # 既存のrun_commands を参考に proc run { prompt command } { # 補完を有効にしておく send "set cli complete-on-space on\r" expect -re $prompt {} # handle escaped ;s in commands, and ;; and ^; regsub -all {([^\\]);;} $command "\\1;\u002;" esccommand regsub {^;} $esccommand "\u002;" command set sep "\\1\u001" regsub -all {([^\\])\;} $command "$sep" esccommand set sep "\u001" set commands [split $esccommand $sep] set num_commands [llength $commands] for {set i 0} {$i < $num_commands} { incr i } { send "[lindex $commands $i]\r" expect { -re "^\[^\n\r *]*$prompt $" {} -re "^\[^\n\r]*$prompt." { exp_continue } -re "(\r\n|\n)" { exp_continue } } } } # 自動で入力したいコマンド群 set cmd_text "configure set interfaces ge-0/0/0 description foo show | compare commit check " set command [join [split $cmd_text \n] \;] run $prompt $command interact
コマンド群を自動で send
したあと,interact
を呼べばいい.
実行例
configure
という名前で保存してあるとして,
codeout $ jlogin -s configure router1 router1 set cli complete-on-space on Enabling complete-on-space codeout> configure Entering configuration mode The configuration has been changed but not committed [edit] codeout# set interfaces ge-0/0/0 description foo [edit] codeout# show | compare [edit interfaces ge-0/0/0] + description foo; [edit] codeout# commit check configuration check succeeds [edit] codeout# [edit] codeout# <-- ここで手動制御にもどる
複数台に対して同じことをやりたい場合,
codeout $ jlogin -s configure router1 router2 ...
のようにまとめて呼ぶ.
ほかのclogin
などで試してないが,同じような処理でいけるはず.