2011-08-12 4 views
-1

sshを使ってリモートコンプに接続する必要のあるbash + expectスクリプトを持っています(ここではパスワードの識別が必要です)そのファイルを "hostname"( "hostname aaaa1111"のような)で特定の行を見つけ、このホスト名をwhileの後に使用する変数に格納します。 "hostname"パラメータの値を取得するにはどうすればよいですか?私はラインの内容が$ expect_out(バッファ)変数にあると思ったので(それをスキャンして分析することができます)、そうではありません。私のスクリプトは次のとおりです。リモートコンピュータ上でcatを実行し、期待値を使って変数を出力する

#!/bin/bash   
    ----bash part---- 
    /usr/bin/expect << ENDOFEXPECT 
    spawn bash -c "ssh [email protected]$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) 

が、それはこの場合には動作しないようですか?

+0

タイトルをあなたの質問をより詳しく説明するようにしてください。 – Brandon

+2

質問[サーバーフォールトに表示されます](http://serverfault.com/q/300238/30957) –

+0

...まだ回答なし、残念ながら – lugger1

答えて

1

期待しているとおりにしようと思うかもしれません。予想通り、bashを制御できます。

以下は、説明したとおりです。これがまさにあなたがしようとしているものなのかどうかは分かりません。

#!/bin/sh 
# the next line restarts using tclsh \ 
exec expect "$0" "[email protected]" 


spawn bash 
send "ssh [email protected]$IP\r" 
expect "password:" 
send "xxxx\r" 
expect ":~#" 
send "cat /etc/rc.d/rc.local |grep hostname \n" 
expect ":~#" 
set extractedOutput $expect_out(buffer) 
set list [split $extractedOutput "\n"] 
foreach line $list { 
    set re {(?x) 
     .* 
     (*)    
     -S.* 
    } 
    regexp $re $line total extractedValue 
    if {[info exists extractedValue] && [string length $extractedValue] > 1} { 
     set exportValue $extractedValue 
     break # We've got a match! 
} 

send "exit\r" # disconnect from the ssh session 

if {[info exists exportValue] && [string length $exportValue] > 1}{ 
    send "export VARIABLE $exportValue\r" 
} else { 
    send_user "No exportValue was found - exiting\n" 
    send "exit\r" 
    close 
    exit 1 
} 

# now you can do more things in bash if you like 
関連する問題