LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-07-2009, 05:56 PM   #1
mrjoe42
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, 07:03 PM   #2
Robhogg
Member
 
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:

Code:
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 () {
net_stats
last_rec=$rec
last_tran=$tran
sleep 1
net_stats
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 06:39 AM.
 
  


Reply


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 04:11 AM
best method to determine network devices in a script? agrestic Linux - Newbie 3 01-15-2009 05:15 AM
Start and stop a bash script based on network activity? creativetweak Linux - Software 8 05-04-2008 10:28 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Does anyone know of a bash script that will determine if RPM exists on a system? jimwelc Linux - Software 3 12-28-2004 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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