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

时间:2017-02-17 19:45:39

标签: variables awk expect

在我的脚本中,在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 '\012' '.' | 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 $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 /.$//'        期待:" \ u001b(B \ u001b [mcat / etc / SuSE-release | awk -F =' / = / {print $ 2}' | sed -e' s / * // g' | tr' \ r \ n>'' |' | sed -e' s /.$//' "(spawn_id exp4)匹配正则表达式"(> |#)"?(无门,仅限RE)gate = yes re = yes        expect:set expect_out(0,string)"> "        expect:set expect_out(1,string)">"        expect:设置expect_out(spawn_id)" exp4"        expect:设置expect_out(缓冲区)" \ u001b(B \ u001b [mcat / etc / SuSE-release | awk -F =' / = / {print $ 2}' | sed -e& #39; s / * // g' | tr' \ r \ n&n;"       发送:发送"退出\ r"到{exp4}       守门员水晶模式为'(> |#)'是'。不可用,禁用性能助推器。

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

1 个答案:

答案 0 :(得分:1)

我没有awk '/VERSION/ {a=$3} /PATCHLEVEL/ {a=a"."$3} END{print a}' /etc/SuSE-release 专家,但你可以通过这样做简化解析:

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

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

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

可能你不得不逃避一些事情,也许是这样吗?

ssh

此外,您可以直接在public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); RunningMedianHeaps s = new RunningMedianHeaps(); int n = in.nextInt(); for(int a_i=0; a_i < n; a_i++){ printMedian(s,in.nextInt()); } in.close(); } public static void printMedian(RunningMedianHeaps s, int nextNum){ s.addNumberInHeap(nextNum); System.out.printf("%.1f\n",s.getMedian()); } } class RunningMedianHeaps{ PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(); PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(Comparator.reverseOrder()); public double getMedian() { int size = minHeap.size() + maxHeap.size(); if(size % 2 == 0) return (maxHeap.peek()+minHeap.peek())/2.0; return maxHeap.peek()*1.0; } private void balanceHeaps() { if(maxHeap.size() < minHeap.size()) { maxHeap.add(minHeap.poll()); } else if(maxHeap.size() > 1+minHeap.size()) { minHeap.add(maxHeap.poll()); } } public void addNumberInHeap(int num) { if(maxHeap.size()==0 || num <= maxHeap.peek()) { maxHeap.add(num); } else { minHeap.add(num); } balanceHeaps(); } } 命令行中向远程主机发送命令,这可能是另一种探索途径。