2013-06-27 33 views
0

我想捕獲幾個遠程設備的輸出使用cli命令,然後grep 717GE和打印屏幕爲多個主機。正如你在下面看到的,它從$ hosts獲取IP並將它傳遞給$ outip,而不是從命令行使用相同命令時顯示的內容。我認爲這個變量會捕獲從給定命令返回的數據。有人能幫助我並幫助我理解我在做什麼錯嗎?我對學習真的很感興趣,所以請暫緩這些拙劣的評論。如果可能的話。bash從遠程機器捕獲輸出到一個變量

for host in ${hosts[@]}; do 
    seven=($(cli $hosts show gpon ont summary -n --max=3 --host)) 
    outip=($(grep 717GE $seven)) 
    echo $outip 
done 

輸出:

+ for host in '${hosts[@]}' 
+ seven=($(cli $hosts show gpon ont summary -n --max=3 --host)) 
++ cli 10.100.112.2 show gpon ont summary -n --max=3 --host 
+ outip=($(grep 717GE $seven)) 
++ grep 717GE 10.100.112.2 grep: 10.100.112.2: No such file or directory 
+0

你的命令不似乎很有意義。嘗試'VAR = $(ssh user @ server「some command here」)' – devnull

+0

ssh沒有在遠程設備上偵聽。我出於各種原因使用cli。用法:cli [] [c:] [選項] – cvjones360

+0

1.刪除括號'seven'和'outip':'seven = $(cli $ hosts show gpon ont summary -n --max = 3 --host)''outip = $(grep 717GE $ 7)'2.當設置'seven'時,你似乎有一個錯字:使用'$ host'而不是'$ hosts'。 – devnull

回答

1

,除非你想創建一個數組,並使用下面的字符串一個bash grep something <<< "$var"(相當於echo "$var" | grep something)搜索匹配的行(否則你不要使用var=(..) 「再稱$var包含文件名列表的搜索,和一些選項爲grep設置):

for host in ${hosts[@]}; do 
    # Assign as variable rather than array, and use $host instead of hosts 
    seven=$(cli $host show gpon ont summary -n --max=3 --host) 
    # Grep with "$seven" as text input and not as a list of filenames 
    outip=$(grep 717GE <<< "$seven") 
    echo "$outip" 
done 
+0

你我是新收購的朋友,真是太棒了。謝謝你的教訓。我非常感謝你的幫助。 – cvjones360

+0

還有一個問題。當沒有717GE列出時,我的腳本會中斷。在for語句中嵌入If-else語句最好嗎? – cvjones360

+0

Nvm ..我在腳本的開始處設置了{set -e},當我刪除它時,我的腳本完美地工作。謝謝!! – cvjones360