1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VIOSへのexpectが動かない?

Posted at

expectを使用してVIOSからデータを取り出したい!

VIOSの設定情報を抜き出したい、でもサーバ1台につき複数のVIOSがあるからコマンド何度も打つのは大変だなぁ...
そうだ! expect使ってどこかのサーバからまとめて取り出せばいいじゃん!

こんなの楽勝

「これくらいで動くでしょ?」って作ってみた。

first.exp
#!/usr/bin/env expect
set vios [lindex $argv 0]
set password [lindex $argv 1]
set command [lindex $argv 2]

spawn ssh $vios -o PubkeyAuthentication=no -l user $command
expect -nocase Password:
send $password\r
expect eof

実行してみる

$ ./first.exp myvios1 password lsdev
spawn ssh myvios1 -o PubkeyAuthentication=no -l user lsdev
user@myvios1's password:
rksh: lsdev: not found.

なんで?大抵のunix系ならこんな感じで動くのに?

試しにちょっとだけ頑張ってみた

「これならどうだ」ってやつ。

second.exp
#!/usr/bin/expect
set vios [lindex $argv 0]
set password [lindex $argv 1]
set command [lindex $argv 2]

set timeout -1
spawn ssh $vios -l user
expect password:
send $password\r
expect \\$
send "$command\r"
expect \\$
send exit\r
expect eof

実行してみる

$ ./second.exp myvios1 password lsdev
spawn ssh myvios1 -o PubkeyAuthentication=no -l user lsdev
user@myvios1's password:
Last login: *************************
user@myvios1:/home/user$ lsdev
name     status     description
L2cache  Available  L2 Cache
cache0   Available  SSD Cache virtual device
以下略

動くじゃん。

ということは?

普通にログインしたら当然実行できるし、second.expでも動くということは環境変数などが足りないんだなぁと、ここで気づいた。
じゃぁ.profileを見てみよう。

.profile
$ cat .profile
前略
alias lsdev="ioscli lsdev"
後略

こんな感じですべてのコマンドに"ioscli"がくっついてる。
ってことはfirst.expでもioscliをつければ動くのかな?

やってみた

first_2.exp
#!/usr/bin/env expect
set vios [lindex $argv 0]
set password [lindex $argv 1]
set command [lindex $argv 2]

spawn ssh $vios -o PubkeyAuthentication=no -l user "ioscli $command"
expect -nocase Password:
send $password\r
expect eof

実行してみた

$ ./first_2.exp myvios1 password lsdev
spawn ssh myvios1 -o PubkeyAuthentication=no -l user ioscli lsdev
user@myvios1's password:
name     status     description
L2cache  Available  L2 Cache
cache0   Available  SSD Cache virtual device
以下略

動いた!

知見

エイリアス設定されているのは知らなかった。
これでoem_setup_envやったときとの切り替えを作ってるんだね。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?