1
votes

There seems to be an issue executing an awk command in a spawn ssh in my script. Also, I am unable to get the output to a variable.

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 '\012' '.' | sed -e 's/\.$//'"
        set version $expect_out(buffer)
        expect -re "(>|#) "
        puts "$version"
        send "exit\r"
        expect -re "(>|#) "
        send "logout\r"
}

Error:

 # "
send: sending "cat /etc/SuSE-release | awk -F= '/=/ {print $2}' | 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 $2}' | sed -e 's/ * //g' | tr '

' '.' | sed -e 's/.$//' expect: does "\u001b(B\u001b[mcat /etc/SuSE-release | awk -F= '/=/ {print $2}' | 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 $2}' | 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
1

1 Answers

1
votes

I'm no expect expert, but you can simplify that parsing quite a bit by doing this:

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

Or if you like things even more concise, but less obvious:

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

Probably you'd have to escape a couple of things, perhaps like so?

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

Also, you can send commands to a remote host directly in the ssh command line, that might be another avenue to explore.