I’m making a little project that logs bandwidth usage of the network connection over a set period of time. For this I need to get the amount of data sent and received. I can do this with system events from the Activity Viewer application, but this requires launching another application (Activity Viewr) which I would rather avoid if possible.
Does anyone know of another way to get this data? this is what I have at the moment:
launch application "Activity Monitor"
tell application "System Events"
tell process "Activity Monitor"
click radio button "Network" of tab group 1 of window "Activity Monitor"
set {"", pout, "", Datasent, "", pin, "", DataRec, "", sentsec, "", recsec, "", poutsec, "", pinsec} ¬
to get value of every static text of tab group 1 of window "Activity Monitor"
end tell
end tell
any help or ideas gratefully received.
Model: MacBook Pro 2.0, 2.0GB
AppleScript: 1.10.7 / X code 2.2.1
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)
Hmmm, thanks StefanK. That’s all a bit over my head to be honest :o I couldn’t see how to get the data sent and received since login from that - but maybe it’s in there somewhere… I guess there isn’t a simpler way?
My first thought here is to use netstat. I have a little Perl program that I run in a Terminal window that prints out how many bytes have been sent and recieved every 10 seconds. It uses netstat to get the data. So I am probably biased to think about netstat first.
Here is some AppleScript code that uses netstat and to extract the packets in, bytes in, packets out, and bytes out:
to getNetworkTotals()
(* Get network usage totals.
* netstat reports the numbers,
* grep and sort filter the output so we only consider the network interfaces we want (no loopback or link-local only interfaces),
* awk totals the numbers up across all the remaining interfaces
*)
set totalStrs to words of (do shell script "netstat -bin | grep -Ev '^lo[0-9]+|Link' | sort -uk 1,1 |tee /dev/tty | awk 'NR!=1{ip+=$5;ib+=$7;op+=$8;ob+=$10}END{print ip,ib,op,ob}'")
(* Convert the totals from strings to numbers *)
set totalNums to {}
repeat with numStr in totalStrs
set end of totalNums to ((contents of numStr) as number)
end repeat
(* Return the totals: Packets In, Bytes In, Packets Out, Bytes Out *)
totalNums
end getNetworkTotals
on run
set previousDate to current date
set previousTotals to getNetworkTotals()
repeat 2 times
delay 10
set currentDate to current date
set currentTotals to getNetworkTotals()
set timeDelta to currentDate - previousDate
set deltas to {}
repeat with i from 1 to 4
set end of deltas to ((item i of currentTotals) - (item i of previousTotals)) / timeDelta
end repeat
set report to "Network Usage in the last " & timeDelta & " seconds:" & return & ¬
"Packets In: " & (item 1 of deltas) & "/s" & return & ¬
"Bytes In: " & (item 2 of deltas) & "/s" & return & ¬
"Packets Out: " & (item 3 of deltas) & "/s" & return & ¬
"Bytes Out: " & (item 4 of deltas) & "/s"
log report
display dialog report with title "Network Usage" giving up after 2
set {previousDate, previousTotals} to {currentDate, currentTotals}
end repeat
end run
The main thing you need is the getNetworkTotals handler. The run handler is just some demo code that does something similar to what my Perl script does.
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)
This won’t really help you with your applescripting, but just yesterday I ran across a program that monitors bandwidth usage… much like you’re trying to do. I haven’t tried it but maybe it’ll help you.