expect 脚本问题执行 awk 命令并在变量中发出 puttng expect_out。

expect script issues executing awk command and issue puttng expect_out in variable.

在我的脚本的 spawn ssh 中执行 awk 命令似乎有问题。另外,我无法将输出输出到变量。

set file [open "hosts.test"]
set hosts [split [read -nonewline $file] "\n"]
close $file

foreach host $hosts {
        puts $host
        spawn ssh -q -o StrictHostKeyChecking=no  [lindex $argv 0]@$host
        expect "Password: "
        send "[lindex $argv 1]\r"
        expect -re "(>|#) "
        send "sudo su -\r"
        expect "Enter YOUR password: "
        send "[lindex $argv 1]\r"
        expect -re "(>|#) "
        send "cat /etc/SuSE-release | awk -F= '/=/ {print $2}' | sed -e 's/ * //g' | tr '2' '.' | sed -e 's/\.$//'"
        set version $expect_out(buffer)
        expect -re "(>|#) "
        puts "$version"
        send "exit\r"
        expect -re "(>|#) "
        send "logout\r"
}

错误:

 # "
send: sending "cat /etc/SuSE-release | awk -F= '/=/ {print }' | sed -e 's/ * //g' | tr '\n' '.' | sed -e 's/.$//'" to { exp4 }
Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster.

expect: does "\u001b(B\u001b[m" (spawn_id exp4) match regular     expression "(>|#) "? (No Gate, RE only) gate=yes re=no
cat /
expect: does "\u001b(B\u001b[mcat /" (spawn_id exp4) match regular   expression "(>|#) "? (No Gate, RE only) gate=yes re=no
etc/Su
expect: does "\u001b(B\u001b[mcat /etc/Su" (spawn_id exp4) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
SuSE-release | awk -F= '/=/ {print }' | sed -e 's/ * //g' | tr '

' '.' | sed -e 's/.$//' expect: does "\u001b(B\u001b[mcat /etc/SuSE-release | awk -F= '/=/ {print }' | sed -e 's/ * //g' | tr '\r\n> ' '.' | sed -e 's/.$//'" (spawn_id exp4) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=yes expect: set expect_out(0,string) "> " expect: set expect_out(1,string) ">" expect: set expect_out(spawn_id) "exp4" expect: set expect_out(buffer) "\u001b(B\u001b[mcat /etc/SuSE-release | awk -F= '/=/ {print }' | sed -e 's/ * //g' | tr '\r\n> " send: sending "exit\r" to { exp4 } Gate keeper glob pattern for '(>|#) ' is ''. Not usable, disabling the performance booster.

expect: does "' '.' | sed -e 's/.$//'" (spawn_id exp4) match regular expression "(>|#) "? (No Gate, RE only) gate=yes re=no
 exit

我不是 expect 专家,但您可以通过这样做大大简化解析:

awk '/VERSION/ {a=} /PATCHLEVEL/ {a=a"."} END{print a}' /etc/SuSE-release

或者如果你喜欢更简洁但不那么明显的东西:

awk '/=/ {a=a?a".":} END{print a}' /etc/SuSE-release

可能你需要逃避一些事情,也许是这样?

send "awk '/VERSION/ {a=$3} /PATCHLEVEL/ {a=a\".\"$3} END{print a}' /etc/SuSE-release"

此外,您可以直接在 ssh 命令行中向远程主机发送命令,这可能是另一种探索途径。