-1

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)

この場合はうまくいかないようですか?

4

1 に答える 1

1

expect は bash を制御できるため、expect からすべてを実行してみることをお勧めします。

以下は、あなたが説明したことを行う必要があります。これがまさにあなたがやろうとしていることかどうかはわかりません。

#!/bin/sh
# the next line restarts using tclsh \
exec expect "$0" "$@"


spawn bash 
send "ssh root@$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
于 2011-08-16T07:32:44.490 に答える