スクリプトは自動的にログインし、コマンドを発行して出力を表示すると予想します。

スクリプトは自動的にログインし、コマンドを発行して出力を表示すると予想します。

私はスクリプトに初めて触れ、助けが必要です。ssh/logHP StoreOnceアプライアンスに自動的に入るLinuxサーバーで予想されるスクリプトを作成しています。

#!/usr/bin/expect -f
set password "password"
match_max 1000
spawn ssh -o StrictHostKeyChecking=no [email protected]
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
interact

このスクリプトを実行すると、自動的にログインしてそこからコマンドを入力できます。

以下の出力を参照してください。

=================================================================
[root@dpis tmp]# ./test.exp
spawn ssh -o StrictHostKeyChecking=no [email protected]
Password:
Last login: Thu Jan 26 08:37:24 2017 from 10.x.x.x

Welcome to the HP StoreOnce Backup System Command Line Interface.
Type 'help' at the prompt for context-sensitive help.

>

しかし、私が達成したいのは、スクリプトが自動的にログインするだけでなく、自動的にコマンドを入力して出力を表示して終了することです。

対話後に次の行を追加してみました。

expect ">"

send -- "serviceset show status"

これを達成する方法についてのヘルプをお探しですか?ありがとうございます!

ベストアンサー1

良い出発をしました。コマンドを入力するにはEnterキーを押す必要があります(文字を送信するには\r)。その後、結果を表示するには別のプロンプトを待つ必要があります。

expect ">"
send -- "serviceset show status\r"
expect ">"

期待通りに出力をキャプチャするのは少しPITAです。次のことができます。

# at the top of the program
log_user 0
# ... log in ...
expect ">"

set cmd "serviceset show status\r"
send -- $cmd
expect -re "$cmd\n(.*)\r\n>"
set output $expect_out(1,string)
puts $output

# disconnect
send -- "exit\r"
expect eof

おすすめ記事