I use wvdial (PPP dialer) with a SIM card modem and sometimes this connection is reset by server side. I would like to run a script which greps ifconfig to get a volume of data downloaded and stores it somewhere. Obviously it has to be done when connection is still alive, otherwise I get nothing, or a few seconds later a value for fresh connection, because wvdial is configured to immediately reconnect when connection is lost. Any idea?


Of course I can grep output of ifconfig every second or two, but this is horrible workaround not even worth considering. The best scenario I can imagine is to somehow capture the signal which instructs ifconfig to "reset the counting" => suspend signal for a moment => run my script => unlock the signal. But maybe there are other possibilities which I'm not aware of like for example configure ifconfig itself to dump the previous result to a file if given interface disappears. Another option is to take output not from ifconfig by elsewhere. BTW, what is the input source for ifconfig?

  • If you have screen installed on the server, you can use that to run your script. – ryekayo Oct 1 '14 at 22:23
  • I do not have any access to the server. The server is ISP. – WeSenseASoulInSearchOfAnswers Oct 1 '14 at 22:24

you can write up your own shell script that logs exactly what you want, inside a while(1) loop with a sleep call at the end (or whatever length of sleep you want). Something like this:

function get_logname
  echo `date +'%F-%H-%M-%S'`.log

function am_online
   ping -c 1 google.com  >/dev/null 2>&1
   echo $?

function update_logfile
  echo "YOUR DATA TO BE LOGGED" >> $1 

  echo $CUR_LOG
  while [ 1 ]; do
        if [ "`am_online`" -eq 0 ]; then
           update_logfile $CUR_LOG
           echo "we are offline!"

        sleep 1

While you are online, it logs to your logfile (>> to append new data, > to rewrite the file to contain only the new data) every second. When you are offline it continually selects a new name for the next logfile, and logs nothing. When you go back online, it begins to log in a new logfile with the last selected logfile name, leaving the other logfile(s) intact.

This should do the trick for you. As with all code you find online, it is your responsibility to test it and make sure it does what you want and doesn't break anything. Specifically in this case, beware that logfiles can balloon in size pretty quickly, and that if you go offline often, you might end up with a bunch of logfiles.

Save as something like "my_internet_logging_script.sh" and run like:

sh my_internet_logging_script.sh

in the directory where you want your logfiles (or edit the script accordingly) to appear.

| improve this answer | |

Answering your last question: ifconfig reads /proc/net/dev and /proc/net/if_inet6. Information about transmitted data for different interfaces is available in the first of those files.

/proc is a special filesystem which files are not stored on hard disk but are created by kernel every time you want to read them, thus access to files is pretty fast. Moreover parsing /proc/net/dev directly should be a little bit faster then parsing ifconfig since that command does different things which you don't need.

Just to give you rough idea, here is time for 1000 repeated ifconfig or cat /proc/net/dev (repeat is zsh command):

$ time (repeat 1000 ifconfig > /dev/null)
1.14s user 3.85s system 90% cpu 5.513 total
$ time (repeat 1000 cat /proc/net/dev > /dev/null)
0.57s user 2.44s system 70% cpu 4.282 tota

Of course parsing /proc/net/dev still is not the good solution, but at least you can gain 20-30% in speed if, after all, you will decide to parse the output every second.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.