需要帮助使用GeekTool与system_profiler和networksetup



大家好,

我创建了一个小的bash shell脚本,我想用GeekTool,但我在我的iMac(11,1)和我的MacBook Pro(8,3)上使用相同的脚本经历了不同的结果。我正在使用networksetup和system_profiler拉网络数据。到目前为止,我对这两个进程都进行了多次调用,但我希望将其合并为对每个进程的一次调用,以创建要在脚本中使用的变量。下面是我到目前为止的脚本:

# Get Ethernet and Wi-Fi information and display it for GeekTool
WiFi=$(networksetup -getairportnetwork en1 | awk '{print $4 " " $5 " " $6 " " $7 " " $8}')
WiFi_IP=$(networksetup -getinfo Wi-Fi | grep -v IPv6 | awk '/IP address/ {print $3}')
SubMask=$(networksetup -getinfo Wi-Fi | awk '/Subnet mask/ {print $3}')
S2N=$(system_profiler SPAirPortDataType | awk '/Noise/ {print $4 " " $5 " " $6 " " $7 " " $8}')
TRate=$(system_profiler SPAirPortDataType | awk '/Rate/ {print $3 " Mbps"}')
echo  -e "33[4;33m" AirPort
if [ "$WiFi_IP" = "" ];
    then echo "Not connected to wireless AP."
    else echo -e "Wi-Fi AP: " $WiFi "nIP Address: " $WiFi_IP "nSubnet Mask: " $SubMask "nTransmit Rate: " $TRate
    fi
EthIP=$(networksetup -getinfo Ethernet | grep -v IPv6 | awk '/IP address/ {print $3}')
EthSubMask=$(networksetup -getinfo Ethernet | grep -v IPv6 | awk '/Subnet mask/ {print $3}')
echo -e "33[4;33m" "nEthernet"
if [ "$EthIP" = "" ];
    then echo "Not connected to wired network."
    else echo -e "IP Address: " $EthIP "nSubnet Mask: " $EthSubMask
    fi

当我在GeekTool中创建一个shell并输入脚本的路径时,在我的MacBook Pro上一切正常,但在我的iMac上,GeekTool显示一次结果,然后在刷新时(我设置为15秒),结果消失并且永远不会返回到屏幕上。虽然我知道我的方法是有效的,但它对两个进程进行了太多的调用,我希望将其合并为每个进程一次调用,将所需的信息提取到容器中,例如可以从中访问所需信息的数组。

换句话说,有没有人知道我如何才能使一个调用networksetup来获得从- getairportnetworken1和-getinfo Wi-Fi以及system_profiler SPAirPortDataType收集的所有信息(这需要最长的时间-每次大约5-8秒)?

我稍微修改了一下我的极客工具脚本,并省略了信号与噪声的比例。下面是新的AirportStatus.sh文件的样子:

#! /bin/bash
# echo -e "33[0;30m"       echo in black
# echo -e "33[0;31m"       echo in red
# echo -e "33[0;32m"       echo in green
# echo -e "33[0;33m"       echo in yellow
# echo -e "33[0;34m"       echo in blue
# echo -e "33[0;35m"       echo in magenta
# echo -e "33[0;36m"       echo in cyan
# echo -e "33[0;37m"       echo in white
# echo -e "33[4;37m"       echo in white underlined
echo -e "33[4;33m" "Airport"
# Get Airport status from OS X
Status=$(ifconfig en1 | awk '/status/ {print $2}')

if [ "$Status" = "inactive" ];
    then echo -e "33[0;31m" "Status: " $Status "nNot connected to wireless AP."
    else echo -e  "33[0;32m" "Status" $Status
    # Get wireless stats from OS X
    WiFi=$(networksetup -getairportnetwork en1 | awk '{print $4 " " $5 " " $6 " " $7 " " $8}')
    WiFi_IP=$(networksetup -getinfo Wi-Fi | grep -v IPv6 | awk '/IP address/ {print $3}')
    SubMask=$(networksetup -getinfo Wi-Fi | awk '/Subnet mask/ {print $3}')
    TRate=$(system_profiler SPAirPortDataType | awk '/Transmit/' | sed 's/[^0-9]//g')
    # Get bytes in/out
    # get the current number of bytes in and bytes out
    myvar1=$(netstat -ib | grep -e en1 -m 1 | awk '{print $7}') #  bytes in
    myvar3=$(netstat -ib | grep -e en1 -m 1 | awk '{print $10}') # bytes out
    #wait one second
    sleep 1
    # get the number of bytes in and out one second later
    myvar2=$(netstat -ib | grep -e en1 -m 1 | awk '{print $7}') # bytes in again
    myvar4=$(netstat -ib | grep -e en1 -m 1 | awk '{print $10}') # bytes out again
    # find the difference between bytes in and out during that one second
    subin=$(($myvar2 - $myvar1))
    subout=$(($myvar4 - $myvar3))
    # convert bytes to megabytes
    mbin=$(echo "scale=2 ; $subin/1048576;" | bc) 
    mbout=$(echo "scale=2 ; $subout/1048576 ; " | bc)
    usedMB=$(echo "scale=2 ; $mbin+$mbout ; " | bc)
    AvailBW=$(echo "scale=2 ; $TRate-$usedMB ; " | bc)
    echo -e "33[0;37m" "Wi-Fi AP: " "33[0;36m" $WiFi "33[0;37m" "nIP Address: " "33[0;36m" $WiFi_IP $3 "33[0;37m" "nSubnet Mask: " "33[0;36m" $SubMask "33[0;37m" "nTransmit Rate: " "33[0;36m" $TRate " Mbpsn" "33[0;37m" "Data In: "  "33[0;36m" $mbin "Mbps" "n" "33[0;37m" "Data Out: "  "33[0;36m" $mbout "Mbpsn" "33[0;37m" "Available Bandwidth: " "33[0;36m" $AvailBW " Mbps"
    fi

我检查了活动状态,假设它最初是不活动的。如果没有,那么我放入大量的If语句,所有的变量都被初始化,计算就完成了。对system_profiler只有一次调用,即检索传输速率的调用,但是我想查看无线网络的"线路速度"。这款应用很好用,虽然效果不如我想象的那么好,但它仍然是一个很好的练习,让我了解了在Mac上从哪里获取信息,以及如何同时使用awk和see。我还有很多东西要学,但到目前为止,这已经很令人兴奋了。

相关内容

  • 没有找到相关文章