2012-10-09 27 views
1

我試圖用bash腳本檢查是否有無線接口。我想我可以通過檢查/ proc/net/wireless中的Status字段來查看每個接口。然而,我試圖尋找這個領域可能的價值觀及其含義,並且似乎沒有出現。有人知道嗎?這是解決這個問題的理想方式嗎?/proc/net/wireless中的「Status」字段

+0

我不是太熟悉的無線接口,但不能使用'ifconfig'或'iwconfig'和grep爲UP還是什麼? – DanielGibbs

回答

1

您需要檢查每個接口的operstate以確定它是否是;向上,向下或未知。下面是使用GNU awk一個辦法:

awk '{ split(FILENAME, array, "/"); print array[5] ": " $1 }' $(find /sys/class/net/*/operstate ! -type d) 

在我的系統,這裏的一些結果:

eth0: up 
lo: unknown 
vboxnet0: down 
wlan0: up 

要檢查無線接口只,你將需要檢查一個名爲「無線」文件夾在每個界面下。以下是使用GNU awk的一種方法。

awk -F "/" 'FNR==NR { wire[$5]++; next } { split(FILENAME, state, "/"); if (state[5] in wire && $1 == "up") print state[5] }' <(find /sys/class/net/*/wireless -type d) $(find /sys/class/net/*/operstate ! -type d) 

結果:

wlan0 

僞代碼:

1. Get the directory names of the wireless devices as the 1st argument 
2. Split these names on the "/" delimiter 
3. Add the 5th column (the name of the wireless device) to an array called 'wire' 
4. Now read in the operstates of all network interfaces as the 2nd argument 
5. Split the interface filenames on the "/" delimiter to an array called 'state' 
6. If the interface is a wireless interface (i.e. if it's in the array called 
    wire) and its operstate is "up", print it. 
+0

謝謝。這似乎是朝着正確方向邁出的一步。但是,第二個和第三個awk語句是否僅返回依賴於以「wlan」開頭的所有無線接口的名稱的無線接口?在很多情況下很可能會出現這種情況,對吧? –

+0

@timtran:我刪除了第二個和第三個awk語句,因爲無線接口可能會用奇怪的字符命名。在我的測試中,我發現每個無線接口都應該包含一個名爲「無線」的目錄。上面編輯的代碼檢查這一點,只在無線接口名稱爲「up」時才返回。請參閱僞代碼。 HTH。 – Steve