作爲編碼的你有2個獨立的,不相關的命令:
# run `ping` and place in background
ping -c1 $line &
和
# regardless of what the `ping` command does ... always
# print 'reachable` and if that fails print `down`
echo $line is reachable || echo $line is down
我假設你真正想要的是使用ping
(成功或失敗)的結果,以確定哪些echo
命令發佈;在這種情況下,你需要用雙`& &「像這樣的替代單&
:
ping -c1 $line &&
echo $line is reachable ||
echo $line is down
雖然這應該提供條件的輸出,你坪現在在前臺執行一個-AT-A-時間。
如果你想在後臺運行的坪(即「多線程」)和也執行條件打印,你可以放置ping/echo/echo
代碼的函數裏面,然後在for
循環做出了一個電話功能......確保放置在後臺的函數調用,如:
網站
樣本列表要平:
$ cat pinglist
www.yahoo.com
www.google.com
www.peanut_butter.and_jelly
www.bbc.co.uk
www.not_today_your.not
建議腳本:
$ cat pinger
#!/bin/bash
function pingit() {
target=${1}
# send stdout/stderr to /dev/null since all we need is the return code
ping -c1 ${target} >/dev/null 2>&1 &&
echo ${target} is reachable ||
echo ${target} is down
}
data=$(<pinglist)
for line in ${data}
do
# call our function and place the call in the background
pingit ${line} &
done
# wait for all outstanding background jobs to complete before continuing
wait
# [optional] let operator know we're done.
echo "Completed."
點
一些樣品運行,同時要注意的是,輸出消息的順序會根據每個ping
命令完成的速度有多快:
$ pinger
www.yahoo.com is reachable
www.google.com is reachable
www.peanut_butter.and_jelly is down
www.bbc.co.uk is reachable
www.not_today_your.not is down
Completed.
$ pinger
www.peanut_butter.and_jelly is down
www.not_today_your.not is down
www.google.com is reachable
www.bbc.co.uk is reachable
www.yahoo.com is reachable
Completed.
我們將再次運行測試和排序輸出,忽略「已完成」。消息:
$ pinger | egrep -v "Completed" | sort
www.bbc.co.uk is reachable
www.google.com is reachable
www.not_today_your.not is down
www.peanut_butter.and_jelly is down
www.yahoo.com is reachable
!請記住,如果你有一長串主機可以ping通,並且你在一個小小的盒子上運行這個腳本,那麼如果你啓動數以千計的同時撥打的數千個電話,那麼你的系統管理員可能不會太高興......並且天堂禁止你犯一個編碼錯誤,產生一個無限循環的命令。
這種類型的不端行爲可以被編碼爲(例如,限制未ping
來電或sleep
荷蘭國際集團爲ping
電話等每一個X號後幾秒鐘的數量),但我會離開這個編碼功課;-)
如果你不想用的功能(例如,你正在做一個快速的命令行中環/平)來打擾,你可以包裝在括號中ping/echo/echo
並運行此結構在背景,雖然你會想要決定如何處理與工作有關的信息,例如:
$ data=$(<pinglist)
$ for line in ${data}
do
(ping -c1 ${line} >/dev/null 2>&1 && echo ${line} is reachable || echo ${line} is down) &
done
[1] 12212
[2] 12213
[3] 12214
[4] 12215
[5] 12217
www.google.com is reachable
www.peanut_butter.and_jelly is down
www.not_today_your.not is down
www.bbc.co.uk is reachable
www.yahoo.com is reachable
[5] + Done for line in ${data};do; (ping -c1 ${line} >/dev/null 2>&1 && echo ${line} is reachable || echo ${line} is down) & 2>/dev/null;done
[4] - Done for line in ${data};do; (ping -c1 ${line} >/dev/null 2>&1 && echo ${line} is reachable || echo ${line} is down) & 2>/dev/null;done
[3] Done for line in ${data};do; (ping -c1 ${line} >/dev/null 2>&1 && echo ${line} is reachable || echo ${line} is down) & 2>/dev/null;done
[2] Done for line in ${data};do; (ping -c1 ${line} >/dev/null 2>&1 && echo ${line} is reachable || echo ${line} is down) & 2>/dev/null;done
[1] Done for line in ${data};do; (ping -c1 ${line} >/dev/null 2>&1 && echo ${line} is reachable || echo ${line} is down) & 2>/dev/null;done
一個選項(組裝機?),用於掩蓋工作相關的消息需要包裝我們ping/echo/echo/&
中包含的語句和指導標準錯誤到/ dev/null的...
$ data=$(<pinglist)
$ for line in ${data}
do
{(ping -c1 ${line} >/dev/null 2>&1 && echo ${line} is reachable || echo ${line} is down) & } 2>/dev/null
done
www.peanut_butter.and_jelly is down
www.not_today_your.not is down
www.google.com is reachable
www.yahoo.com is reachable
www.bbc.co.uk is reachable
...雖然這可能掩蓋一些你可能感興趣的錯誤信息,ymmv。
的'&'運行'ping'進程在後臺讓你的狀態下面是報告只報告發現並執行了'ping'(而不是''ping'退出時發生了什麼)。使用shell腳本,我不認爲有一種方法來加快這一點。尋找一個更好的工具,用於掃描IP範圍。想到'nmap'。祝你好運。 – shellter
謝謝Shelter我想這解釋了爲什麼它很難找到我在找什麼,我知道nmap,但希望在Bash中找到相同的效果,因爲nmap沒有默認安裝 –
bash不支持多線程。 'foo&'運行一個新的*進程*,而不是一個線程。 –