PDA

View Full Version : How to fix this Geektool Script for Lion?




its
Jul 28, 2011, 01:38 AM
Hey Guys

Since I have upgraded to Lion, one of my geektool script is broken.. Actually not my script.. but copied from internet.. anyone good with programming , can u fix it please??


# Change this to en0 for an ethernet connection instead of airport or en1 for airport
INTF=en1

# get the current number of bytes in and bytes out
sample1=(`/usr/sbin/netstat -ib | awk "/$INTF/"'{print $7" "$10; exit}'`)

# wait one second
sleep 1

# get the number of bytes in and out one second later
sample2=(`/usr/sbin/netstat -ib | awk "/$INTF/"'{print $7" "$10; exit}'`)

# find the difference between bytes in and out during that one second
# and convert bytes to kilobytes
results=(`echo "2k ${sample2[0]} ${sample1[0]} - 1024 / p" "${sample2[1]} ${sample1[1]} - 1024 / p" | dc`)

# print the results
printf "In: %.2f Kb/sec\nOut: %.2f Kb/sec\n" ${results[0]} ${results[1]}


I guess somewhere in awk command its going wrong..



its
Jul 28, 2011, 04:01 AM
Ok.. here is the problems.. there are two scripts spiting out errors to /var/log/system.log

Battery status Code

my_ac_adapt=`ioreg -w0 -l | grep ExternalConnected | awk '{print $5}'`
if [ "$my_ac_adapt" == "Yes" ]
then
echo "Power : External"
else
cur_power=`ioreg -w0 -l | grep CurrentCapacity | awk '{print $5}'`
max_power=`ioreg -w0 -l | grep MaxCapacity | awk '{print $5}'`
bat_percent=`echo "scale=2;$cur_power / $max_power" | bc`
bat_percent=`echo "$bat_percent * 100" | bc | sed 's/.00//'`
cyc_count=`ioreg -w0 -l | grep "Cycle Count" | awk 'BEGIN { FS = "=" } ; {print $8}' | awk 'BEGIN { FS = "}" } ; {print $1}'`
echo "Power : Battery ($bat_percent%)"
echo "Cycles : $cyc_count"
fi


Error
ul 28 18:58:54 hostname ioreg[46580]: FIXME: IOUnserialize has detected a string that is not valid UTF-8, "??????p???????????????????????".

Network Speed Code

# Change this to en0 for an ethernet connection instead of airport or en1 for airport
INTF=en0

# get the current number of bytes in and bytes out
sample1=(`/usr/sbin/netstat -ib | awk "/$INTF/"'{print $7" "$10; exit}'`)

# wait one second
sleep 1

# get the number of bytes in and out one second later
sample2=(`/usr/sbin/netstat -ib | awk "/$INTF/"'{print $7" "$10; exit}'`)

# find the difference between bytes in and out during that one second and convert bytes to kilobytes
results=(`echo "2k ${sample2[0]} ${sample1[0]} - 1024 / p" "${sample2[1]} ${sample1[1]} - 1024 / p" | dc`)

# print the results
printf "In: %.2f Kb/sec\nOut: %.2f Kb/sec\n" ${results[0]} ${results[1]}


Error
Jul 28 18:59:40 hostname GeekTool[660]: Geeklet 7149C187-223F-4DF1-ADA8-79F336455B7E. Task already running (# Change this to enů)


Somebody with knowledge plz help :0

allejo
Aug 10, 2011, 03:20 AM
I can't reproduce either error. both scripts run fine for me. The first error means a character that is not UTF-8 is being returned, which not sure how that happens; it's probably some weird character with an accent. As for the second error, it is self-explanatory, the process is already running so it can't be run again. Have you tried restarting your computer?

-allejo

its
Aug 10, 2011, 06:12 AM
I can't reproduce either error. both scripts run fine for me. The first error means a character that is not UTF-8 is being returned, which not sure how that happens; it's probably some weird character with an accent. As for the second error, it is self-explanatory, the process is already running so it can't be run again. Have you tried restarting your computer?

-allejo

I get both the errors on my /var/log/system.log

Ofcourse I have rebooted N number of times and I cannot see any other script running duplicate..

allejo
Aug 10, 2011, 12:26 PM
When called, a battery should return normal characters; so maybe you have a faulty battery? Have you reinstalled Lion?

Try running this in Terminal:
sudo /usr/libexec/configd -dv -t /System/Library/SystemConfiguration/PowerManagement.bundle

My output is (Which should be the norm):
loading bundles
calling bundle load() functions
No more work for the "configd" plugins

As for the Network script, try copy pasting it into a file called "myscript.sh" on your desktop and then in terminal type:
cd ~/Desktop;sh myscript.sh
(You can do the copy paste for the battery script also)

-allejo

P.S. The scripts are perfectly fine, they're not buggy. This issue has to do with your setup.

its
Aug 13, 2011, 03:40 AM
When called, a battery should return normal characters; so maybe you have a faulty battery? Have you reinstalled Lion?

Try running this in Terminal:
sudo /usr/libexec/configd -dv -t /System/Library/SystemConfiguration/PowerManagement.bundle

My output is (Which should be the norm):
loading bundles
calling bundle load() functions
No more work for the "configd" plugins

As for the Network script, try copy pasting it into a file called "myscript.sh" on your desktop and then in terminal type:
cd ~/Desktop;sh myscript.sh
(You can do the copy paste for the battery script also)

-allejo

P.S. The scripts are perfectly fine, they're not buggy. This issue has to do with your setup.

Lion was a brand new install.. I get the same output as u..

Network script, I tried that.. same error.. already running.. but this time quotes the .sh file in error..

its
Aug 14, 2011, 01:42 AM
Ok... fixed the network speed one..

the problem is if i set it to refresh every 1s or say 2s or something.. the error pops up in system.log

but then the problem is that if i don't set it to that.. the update is not realtime..

HawaiiMacAddict
Aug 14, 2011, 02:25 AM
Ok... fixed the network speed one..

the problem is if i set it to refresh every 1s or say 2s or something.. the error pops up in system.log

but then the problem is that if i don't set it to that.. the update is not realtime..

Aloha its,

Here is my network script, which is based on the one you have. It shows both Ethernet (en0) and Airport (en1) stats, although the call to system_profiler makes the Airport one sometime not show up for a few seconds.

You'll notice the code commented at the top for the colors you can use in terminal. That's pretty cool and it works in GeekTool as well. Also, I left in the other commented lines. You can uncomment them if you want to see the output. For the available bandwidth, I simply added up the data in and data out figures and subtracted from the known total bandwidth. That's probably not the correct figure, but it's good enough for jazz. BTW, this is run on my iMac.


#! /bin/bash

# echo -e "\033[0;30m" echo in black
# echo -e "\033[0;31m" echo in red
# echo -e "\033[0;32m" echo in green
# echo -e "\033[0;33m" echo in yellow
# echo -e "\033[0;34m" echo in blue
# echo -e "\033[0;35m" echo in magenta
# echo -e "\033[0;36m" echo in cyan
# echo -e "\033[0;37m" echo in white

# echo -e "\033[4;37m" echo in white underlined

# 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 "\033[4;33m" "AirPort"
if [ "$WiFi_IP" = "" ];
then echo -en "\033[0;31m" "Not connected to wireless AP.\n"
else echo -e "\033[0;37m" "Wi-Fi AP: " "\033[0;36m" $WiFi "\033[0;37m" "\nIP Address: " "\033[0;36m" $WiFi_IP $3 "\033[0;37m" "\nSubnet Mask: " "\033[0;36m" $SubMask "\033[0;37m" "\033[0;37m" "\nTransmit Rate: " "\033[0;36m" $TRate
fi
# 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

#echo -e "\033[0;36m" "\nBytes in 1 = " $myvar1
#echo "Bytes in 2 = " $myvar2
#echo -e "\nBytes out 1 = " $myvar3
#echo "Bytes out 2 = " $myvar4

# find the difference between bytes in and out during that one second
subin=$(($myvar2 - $myvar1))
subout=$(($myvar4 - $myvar3))

#echo -e "\nsubin= " $subin
#echo "subout= " $subout

# convert bytes to kilobytes
kbin=$(echo "scale=2 ; $subin/1048576;" | bc)
kbout=$(echo "scale=2 ; $subout/1048576;" | bc)

#echo "kbin= " $kbin
#echo -e "kbout= " $kbout "\n"

# print the results
echo -e "\033[0;37m" "Data In: " "\033[0;36m" $kbin "Mb/sec"
echo -e "\033[0;37m" "Data Out: " "\033[0;36m" $kbout "Mb/sec"

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 "\033[4;33m" "\n\nEthernet"
if ["$EthIP" = ""];
then echo -en "\033[0;31m" "Not connected to wired network.\n"
else echo -e "\033[0;37m" "IP Address: " "\033[0;36m" $EthIP $3 "\033[0;37m" "\nSubnet Mask: " "\033[0;36m"$EthSubMask $3
fi

# get the current number of bytes in and bytes out
myvar1=$(netstat -ib | grep -e en0 -m 1 | awk '{print $7}') # bytes in
myvar3=$(netstat -ib | grep -e en0 -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 en0 -m 1 | awk '{print $7}') # bytes in again
myvar4=$(netstat -ib | grep -e en0 -m 1 | awk '{print $10}') # bytes out again

# Display the results of the two sets of commands above
#echo -e "\033[0;36m" "Bytes in 1 = " $myvar1
#echo "Bytes in 2 = " $myvar2
#echo -e "\nBytes out 1 = " $myvar3
#echo "Bytes out 2 = " $myvar4

# find the difference between bytes in and out during that one second
subin=$(($myvar2 - $myvar1))
subout=$(($myvar4 - $myvar3))

#echo "subin= " $subin
#echo "subout= " $subout

# convert bytes to kilobytes
kbin=$(echo "scale=2 ; $subin/1048576 ; " | bc)
kbout=$(echo "scale=2 ; $subout/1048576;" | bc)
kbin2=$(echo "scale=2 ; (100 - $kbin) ; " | bc)
kbTotal=$(echo "scale=2 ; ($kbin2 - kbout) ; " | bc)

#echo "kbin= " $kbin
#echo -e "kbout= " $kbout "\n"

# print the results
echo -e "\033[0;37m" "Data In: " "\033[0;36m" $kbin "Mbps"
echo -e "\033[0;37m" "Data Out: " "\033[0;36m" $kbout "Mbps"
echo -e "\033[0;37m" "Available Bandwidth: " "\033[0;36m" $kbTotal "Mbps"


----------

Aloha its,

I must apologize. I tweaked that script and made two scripts from it, one for the Ethernet and one for the Airport adapter. Here is the Ethernet script:

#! /bin/bash

# echo -e "\033[0;30m" echo in black
# echo -e "\033[0;31m" echo in red
# echo -e "\033[0;32m" echo in green
# echo -e "\033[0;33m" echo in yellow
# echo -e "\033[0;34m" echo in blue
# echo -e "\033[0;35m" echo in magenta
# echo -e "\033[0;36m" echo in cyan
# echo -e "\033[0;37m" echo in white

# echo -e "\033[4;37m" echo in white underlined

# Print name of network service
echo -e "\033[4;33m" "Ethernet"

# Get ethernet status from OS X
Status=$(ifconfig en0 | awk '/status/ {print $2}')

# Check for network activity
if [ "$Status" = "inactive" ];

# Inform user if not connected to ethernet network
then echo -en "\033[0;31m" "Status: " $Status "\nNot connected to wired network.\n"

# Inform user if connected to ethernet network
else echo -e "\033[0;32m" "Status" $Status

# Get ethernet stats from OS X
EthIP=$(networksetup -getinfo Ethernet | grep -v IPv6 | awk '/IP address/ {print $3}')
EthSubMask=$(networksetup -getinfo Ethernet | grep -v IPv6 | awk '/Subnet mask/ {print $3}')
LineSpeed=$(ifconfig en0 | awk '/media/ {print $3}' | sed 's/[^0-9]//g')

# Get the current number of bytes in and bytes out
myvar1=$(netstat -ib | grep -e en0 -m 1 | awk '{print $7}') # bytes in
myvar3=$(netstat -ib | grep -e en0 -m 1 | awk '{print $10}') # bytes out

#Wait two seconds
sleep 2

# Get the number of bytes in and out one second later
myvar2=$(netstat -ib | grep -e en0 -m 1 | awk '{print $7}') # bytes in again
myvar4=$(netstat -ib | grep -e en0 -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)

mbUsed=$(echo "scale=2 ; $mbin+$mbout;" | bc)

AvailBW=$(echo "scale=2 ; $LineSpeed-$mbUsed;" | bc)

echo -e "\033[0;37m" "IP Address: " "\033[0;36m" $EthIP "\033[0;37m" "\nSubnet Mask: " "\033[0;36m" $EthSubMask

# Output bandwidth in Mbps and Gbps
if [ "$LineSpeed" = "1000" ] ;
then LineSpeed2=$(($LineSpeed/1000))
echo -e "\033[0;37m" "Line Speed: " "\033[0;36m" $LineSpeed2 "Gbps"
else echo -e "\033[0;37m" "Line Speed: " "\033[0;36m" $LineSpeed "Mbps"
fi

# Check for available Mbps and Gbps bandwidth
if [ "$AvailBW" = "1000" ];
then AvailBW2=$(($AvailBW/1000))
echo -e "\033[0;37m" "Available Bandwidth: " "\033[0;36m" $AvailBW2 "Gbps"
else echo -e "\033[0;37m" "Available Bandwidth: " "\033[0;36m" $AvailBW "Mbps"
fi
echo -e "\033[0;37m" "Data In: " "\033[0;36m" $mbin "Mbps" "\n" "\033[0;37m" "Data Out: " "\033[0;36m" $mbout "Mbps"
fi



and here is the Airport script:

#! /bin/bash

# echo -e "\033[0;30m" echo in black
# echo -e "\033[0;31m" echo in red
# echo -e "\033[0;32m" echo in green
# echo -e "\033[0;33m" echo in yellow
# echo -e "\033[0;34m" echo in blue
# echo -e "\033[0;35m" echo in magenta
# echo -e "\033[0;36m" echo in cyan
# echo -e "\033[0;37m" echo in white

# echo -e "\033[4;37m" echo in white underlined

echo -e "\033[4;33m" "Airport"

# Get Airport status from OS X
Status=$(ifconfig en1 | awk '/status/ {print $2}')


if [ "$Status" = "inactive" ];
then echo -e "\033[0;31m" "Status: " $Status "\nNot connected to wireless AP."
else echo -e "\033[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 "\033[0;37m" "Wi-Fi AP: " "\033[0;36m" $WiFi "\033[0;37m" "\nIP Address: " "\033[0;36m" $WiFi_IP $3 "\033[0;37m" "\nSubnet Mask: " "\033[0;36m" $SubMask "\033[0;37m" "\nTransmit Rate: " "\033[0;36m" $TRate " Mbps\n" "\033[0;37m" "Available Bandwidth: " "\033[0;36m" $AvailBW " Mbps\n" "\033[0;37m" "Data In: " "\033[0;36m" $mbin "Mbps\n" "\033[0;37m" "Data Out: " "\033[0;36m" $mbout "Mbps"

fi

justwright
Sep 5, 2011, 07:15 PM
Hey Guys

Since I have upgraded to Lion, one of my geektool script is broken.. Actually not my script.. but copied from internet.. anyone good with programming , can u fix it please??


# Change this to en0 for an ethernet connection instead of airport or en1 for airport
INTF=en1

# get the current number of bytes in and bytes out
sample1=(`/usr/sbin/netstat -ib | awk "/$INTF/"'{print $7" "$10; exit}'`)

# wait one second
sleep 1

# get the number of bytes in and out one second later
sample2=(`/usr/sbin/netstat -ib | awk "/$INTF/"'{print $7" "$10; exit}'`)

# find the difference between bytes in and out during that one second
# and convert bytes to kilobytes
results=(`echo "2k ${sample2[0]} ${sample1[0]} - 1024 / p" "${sample2[1]} ${sample1[1]} - 1024 / p" | dc`)

# print the results
printf "In: %.2f Kb/sec\nOut: %.2f Kb/sec\n" ${results[0]} ${results[1]}


I guess somewhere in awk command its going wrong..



I use this same script for my Geektool setup and ever since I upgraded to the Lion friendly v3.0.1 the script will display for a few seconds and then disappear. very frustrating

judge2005
Jun 21, 2012, 06:30 PM
The problem is that the arguments given to netstat look at all the interfaces. One of them causes it to pause for a long time. This fixes it:


#!/bin/sh
# Change this to en0 for an ethernet connection instead of airport
INTF=en1

# get the current number of bytes in and bytes out
sample1=(`/usr/sbin/netstat -ib -I $INTF | /usr/bin/awk "/$INTF/"'{print $7" "$10; exit}'`)

# wait one second
/bin/sleep 1

# get the number of bytes in and out one second later
sample2=(`/usr/sbin/netstat -ib -I $INTF | /usr/bin/awk "/$INTF/"'{print $7" "$10; exit}'`)

# find the difference between bytes in and out during that one second
# and convert bytes to kilobytes
results=(`/bin/echo "2k ${sample2[0]} ${sample1[0]} - 1024 / p" "${sample2[1]} ${sample1[1]} - 1024 / p" | /usr/bin/dc`)

# print the results
/usr/bin/printf "In:\t\t%.2f Kb/sec\nOut:\t%.2f Kb/sec\n" ${results[0]} ${results[1]}