Quick and Dirty bash script to check bandwidth usage

199
Am i saying bull sheets?
Thanks!
An error occurred!

I need it to shutdown my home server when it’s idle for a long time. Nothing very elegant, a bit of grep on ifconfig output but it seems to work fine. Have fun!

Donwload bwusage


#! /bin/bash

# BandWidth Usage
# Federico Zanco

IFACE=eth0
TIMEOUT=10
RX="1"
TX="1"
TRAFFIC="FALSE"
UNIT="KiB"
VERBOSE=

show_help () {
echo
echo "usage: bwusage [-h|--help] [-i|--interface ] [--rx-only]"
echo " [--tx-only] [--traffic] [-t|--timeout ]"
echo " [-u|--unit [-v|--verbose]"
echo
}

change_unit () {
case "$UNIT" in

"KiB")
[ "$1" = "RX" ] && RX=`echo "$RX"|awk '{printf "%0.4f", $1 / 1024}'`
[ "$1" = "TX" ] && TX=`echo "$TX"|awk '{printf "%0.4f", $1 / 1024}'`
;;

"MiB")
[ "$1" = "RX" ] && RX=`echo "$RX"|awk '{printf "%0.4f", $1 / 1048576}'`
[ "$1" = "TX" ] && TX=`echo "$TX"|awk '{printf "%0.4f", $1 / 1048576}'`
;;

esac
}

TEMP=`getopt -q -o hi:t:u:v --long help,interface,rx-only,tx-only,traffic,timeout,unit,verbose -- "$@"`

eval set -- "$TEMP"

while true ; do
case "$1" in

-h|--help)
show_help
exit 0
;;

-i|--interface)
IFACE=$2
shift 2
;;

--rx-only)
TX=
shift
;;

--tx-only)
RX=
shift
;;

--traffic)
TRAFFIC="TRUE"
shift
;;

-t|--timeout)
TIMEOUT=$2
shift 2
;;

-u|--unit)
[ "$2" != "B" -a "$2" != "KiB" -a "$2" != "MiB" ] && echo && echo "ERROR: unit $2 not valid" && show_help && exit 1
UNIT=$2
shift 2
;;

-v|--verbose)
VERBOSE="1"
shift
;;

*)
# [ "--" != "$1" ] && echo && echo "ERROR: $1 unknown option" && show_help && exit 1
break
;;
esac
done

[ -z "$(ifconfig -s )|grep $IFACE" ] && echo && echo "ERROR: $IFACE not up or not valid" && exit 1

[ -n "$VERBOSE" ] && echo
[ -n "$VERBOSE" ] && echo "IFACE=$IFACE"
[ -n "$VERBOSE" ] && echo "TIMEOUT=$TIMEOUT s"
[ -n "$VERBOSE" ] && echo "TRAFFIC=$TRAFFIC"
[ -n "$VERBOSE" ] && echo "UNIT=$UNIT"
[ -n "$VERBOSE" ] && echo
[ -n "$VERBOSE" ] && echo "... please wait $TIMEOUT seconds ..."
[ -n "$VERBOSE" ] && echo

[ -n "$RX" ] && rx1=`/sbin/ifconfig $IFACE|tr -s ' '|grep -e "RX bytes:"|cut -d' ' -f3|cut -d: -f2`
[ -n "$TX" ] && tx1=`/sbin/ifconfig $IFACE|tr -s ' '|grep -e "RX bytes:"|cut -d' ' -f7|cut -d: -f2`

sleep $TIMEOUT

[ -n "$RX" ] && rx2=`/sbin/ifconfig $IFACE|tr -s ' '|grep -e "RX bytes:"|cut -d' ' -f3|cut -d: -f2`
[ -n "$TX" ] && tx2=`/sbin/ifconfig $IFACE|tr -s ' '|grep -e "RX bytes:"|cut -d' ' -f7|cut -d: -f2`

if [ -n "$RX" ]
then
RX=`echo "$rx2 - $rx1"|bc`
[ "$TRAFFIC" = "FALSE" ] && RX=`echo "$RX $TIMEOUT"|awk '{printf "%0.4f", $1 / $2 }'`
change_unit "RX"
[ -n "$VERBOSE" ] && echo -n "RX: "
echo "$RX"|awk '{printf "%0.2f", $1 }'
[ -n "$VERBOSE" ] && echo -n " $UNIT" && [ "$TRAFFIC" = "FALSE" ] && echo -n "/s"
fi

[ -n "$RX" ] && [ -n "$TX" ] && echo -n " "

if [ -n "$TX" ]
then
TX=`echo "$tx2 - $tx1"|bc`
[ "$TRAFFIC" = "FALSE" ] && TX=`echo "$TX $TIMEOUT"|awk '{printf "%0.4f", $1 / $2 }'`
change_unit "TX"
[ -n "$VERBOSE" ] && echo -n "TX: "
echo "$TX"| awk '{printf "%0.2f", $1 }'
[ -n "$VERBOSE" ] && echo -n " $UNIT" && [ "$TRAFFIC" = "FALSE" ] && echo -n "/s"
fi

echo

Yet another virtual terminal plugin: Instant messaging Remote Access

181
Am i saying bull sheets?
Thanks!
An error occurred!

imra_screenshot1

imra_screenshot1


Now… I know I’m a bit obsessed by this arguments but I was quite unhappy with ImVT. Having become aware of the work to reach a minimal stable state with ImVT I realized I don’t have enough time. I’d like to write a library that handles only issues terminal related (interpret signals, escape/control sequences, …) allowing you to avoid to use a GUI (Gnome VTE forces you to have a gui for your terminal emulator) and to work with a simplified interface to push input and read processed output in some friendly way (markup? XML/HTML?). I’d like to but… now I can’t (and I don’t know if I were able to!).
So the idea I’ve done this new plugin, less versatile but a little bit more robust and far far away easier to write! The final result is a kind of telnet via IM. If you’re unsatisfied with ImVT take a look to ImRA and please leave a feedback!

Funny ideas (virtual Terminal via IM???)

135
Am i saying bull sheets?
Thanks!
An error occurred!
I think the XMPP protocol can be considered as a sort of middleware allowing to transport any type of data in the network without worrying too much about the nature of the data, the place where data is, and protocols used to transport data from one side to another.

In a certain way this behaviour is quite similar to Sockets that, using a public service (such as a Jabber server), allow you to bypass many of the barriers (NATs, firewalls, …) dividing two hosts on the Internet.

Think about this idea: developing a libpurple plugin acting like netcat or a pipe between two buddies, that you can use to pass ANY kind of data or stream. Something like: right click on buddy and click on “create socket on port 1234” or “create named pipe”.

I know this is a trivial idea and there are plenty of systems that do this. But what can make the difference (IMHO) is that:

  • XMPP is XML based and this allows anyone to define their own protocol
  • a lot of IMs support  XMPP and chat/IMs are very popular and widely used, moreover they are a public services and usually can pass through NATs quite easily (although this should not be a problem with IPv6!)
In the last months I’ve stopped developing  XMPP / Gtalk invisibility plugins[1] to try to implement an “old” idea: create a virtual terminal controlled via IM.

After an initial stage to get a bit of knowledge about (pseudo) terminal emulators (Advanced Unix Programming rocks :)) I’ve given birth to Instant messaging Virtual Terminal (ImVT). It’s now a very buggy prototype, but for “easy” tasks it does its job. I was able to run commands like ssh, vim (with some issues), and some other commands that require a terminal. Forget to run commands that produce constantly output (i.e. top).

Unfortunately I can not spend too much time developing it so, for now, it’s only a small buggy toy.
If anyone more experienced (especially in terminal emulators programming!) thinks that I am not too insane and likes the idea to help to develop… just send me an email!

Please pay attention to security issues: every buddy allowed to chat with the account used by ImVT can gain access to the terminal emulator!!!

  1. [1] I’m looking forward to develop Google Invisibility Tracker enhancement log every buddies’ status change and every presence stanza received, including those sent by iGoogle or Gmail when chat is turned off (that means that buddy was on the Internet). I just have to find time to do it!

a little (and probably short term) hope about detecting invisible buddies in Google Talk

71
Am i saying bull sheets?
Thanks!
An error occurred!

a small variant of the first method to detect invisible buddies in Google Talk.


The first method consisted in starting a chat with the buddy we assumed invisible. If we got the message “mybuddy@gmail.com is offline and can’t receive messages right now.” then the buddy was offline but if didn’t appear any message then we could determine that our buddy was really invisible.


Now this bug has been fixed by Google but…


Let’s try to invite the invisible buddy in a group chat and see what happens…


In Pidgin this is quite easy: right click on the offline (invisible) buddy and click on “Start a chat”. This will start a group chat if the buddy is invisible.


Obviously this bug will have a short life and the method it’s not stealth (and difficult to apply to all buddies but not impossible) but for now…

Google fixed the “bug” to scan invisible buddies

57
Am i saying bull sheets?
Thanks!
An error occurred!

It seems that Google has fixed what we used to consider a bug but was not properly a bug but simply an implementation choice.

From XMPP IM page:

5.1.3. Presence Probes

Upon receiving a presence probe from the user, the contact’s server SHOULD reply as follows:

3. Else, if the contact has no available resources, the server MUST either (1) reply to the presence probe by sending to the user the full XML of the last presence stanza of type “unavailable” received by the server from the contact, or (2) not reply at all.

obviously if we consider invisibility like a offline status we have to accept both cases. Until now we could scan invisible buddies because Google Server used two different behaviors for Offline and Invisible status.

And now?

Now the “half scan method” is no more useful. I don’t know any other method to do the same.

For now the only way I know to have information about invisible buddies is this plugin but it’s neither tested nor reliable and you have to be online when your buddies goes invisible.

Under Construction… ?

5
Am i saying bull sheets?
Thanks!
An error occurred!

Now… installed WordPress to avoid that horrible index page but don’t expect too much from this little revolution. This site will probably be under construction for a looong long time.

Have fun!

Bulldozer