Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-07-2009, 06:56 PM   #1
LQ Newbie
Registered: Jul 2005
Posts: 1

Rep: Reputation: 0
Bash/perl script to determine network activity

I was wondering if anyone had a small script (preferably bash, but will work with perl) that I could use to determine if there's a certain amount of network activity.

Currently closing the lid on my laptop doesn't do anything. If I want to put it to sleep I have to use a fn-key press. I'd like to configure it to sleep when the lid is closed (and I know how), but if I'm in the middle of a download I'd prefer it to not do anything but turn the screen off.

I don't mind working out a level of network activity to trigger on and I'm familiar enough with bash/perl to start, just need some ideas on the best way to do this.
Old 07-07-2009, 08:03 PM   #2
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
I don't have anything written, but I've been poking around and found that the file /proc/net/dev contains a line for each interface, which contains, inter-alia, the number of bytes received and sent. This might be a starting point:

net_stats () {
iface=$(grep 'eth0:' /proc/net/dev) # assuming your connection is via eth0
iface=${iface#*:} # strip the eth0: prefix
rec=$(echo $iface | awk '{print $1}') # field 1 contains the bytes received
tran=$(echo $iface | awk '{print $9}') # field 9 contains the bytes sent

traffic_per_sec () {
sleep 1
traffic=$((($rec - $last_rec) + ($tran - $last_tran)))
You'd probably want a few zero readings (or a zero reading over a longer interval than 1 second) before deciding that a download has finished, rather than just paused for a while, though.

Last edited by Robhogg; 07-08-2009 at 07:39 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
best method to determine network devices in a script? agrestic Linux - Newbie 3 01-15-2009 06:15 AM
Start and stop a bash script based on network activity? creativetweak Linux - Software 8 05-04-2008 11:28 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
Does anyone know of a bash script that will determine if RPM exists on a system? jimwelc Linux - Software 3 12-28-2004 04:01 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration