ssh経由でリモートcompに接続する必要があるbash + expectスクリプトがあり(sshキーを使用できないため、ここでパスワード識別が必要です)、そこでファイルを読み取り、「ホスト名」を含む特定の行を見つけます(「 hostname aaaa1111") を作成し、このホスト名を変数に格納して、while の後に使用します。「ホスト名」パラメータの値を取得するにはどうすればよいですか? 行の内容は $expect_out(buffer) 変数にあると思っていたので (スキャンして分析できます)、そうではありません。私のスクリプトは次のとおりです。
#!/bin/bash
----bash part----
/usr/bin/expect << ENDOFEXPECT
spawn bash -c "ssh root@$IP"
expect "password:"
send "xxxx\r"
expect ":~#"
send "cat /etc/rc.d/rc.local |grep hostname \r"
expect ":~#"
set line $expect_out(buffer)
puts "line = $line, expect_out(buffer) = $expect_out(buffer)"
...more script...
ENDOFEXPECT
行変数を表示しようとすると、これだけが表示されます:line = , expect_out(buffer) = (buffer)
ファイルから変数に行を取得する正しい方法は何ですか? または、期待してリモートコンピューターでファイルを開き、ファイルをスキャンして、変数に必要なものを取得することは可能ですか? ここにhttp://en.wikipedia.org/wiki/Expectに例があります:
# Send the prebuilt command, and then wait for another shell prompt.
send "$my_command\r"
expect "%"
# Capture the results of the command into a variable. This can be displayed,
set results $expect_out(buffer)
この場合はうまくいかないようですか?