scripting help(solved)

Message
Author
TauTsar
Posts: 38
Joined: Thu Jun 21, 2018 4:49 am

scripting help(solved)

#1 Post by TauTsar »

I’d like have an accumulating total of network data displayed in my top panel. Generic Monitor can do this if I knew how to write scripts. I have it setup right now to: cat /sys/class/net/wlan0/statistics/rx_bytes .

What I would like to do is add the values from rx_bytes and tx_bytes, then display the value in kilo bytes, rather then just bytes, in the color #FF00FF. This is very difficult stuff to Google so any help would be great.

I’m guessing cat can acquire values from text files. But adding two values together then inserting the sum into a third text file, how? Then have Generic Monitor cat the “sum text” and output in a specific color. How?
Last edited by TauTsar on Tue Jun 26, 2018 1:30 pm, edited 1 time in total.

User avatar
richb
Administrator
Posts: 10323
Joined: Wed Jul 12, 2006 2:17 pm

Re: scripting help

#2 Post by richb »

This can also be done with a conky. I can provide the code if you want.
You do not have the required permissions to view the files attached to this post.
Forum Rules
Guide - How to Ask for Help

richb Administrator
System: MX 23 KDE
AMD A8 7600 FM2+ CPU R7 Graphics, 16 GIG Mem. Three Samsung EVO SSD's 250 GB

User avatar
kmathern
Developer
Posts: 2403
Joined: Wed Jul 12, 2006 2:26 pm

Re: scripting help

#3 Post by kmathern »

TauTsar wrote: Tue Jun 26, 2018 7:25 amWhat I would like to do is add the values from rx_bytes and tx_bytes, then display the value in kilo bytes, rather then just bytes...
I think this snippet of code will get the sum of rx_bytes and tx_bytes and convert to kilo bytes

Code: Select all

cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc | awk '{print $1/1024}'

TauTsar
Posts: 38
Joined: Thu Jun 21, 2018 4:49 am

Re: scripting help

#4 Post by TauTsar »

richb wrote: Tue Jun 26, 2018 7:31 am This can also be done with a conky. I can provide the code if you want.
You know I went through the conky manager looking for one which might be useful. A seemingly useful one wasn't. Yours isn’t in my collection but it has individual totals for tx & rx. Not what I had in mind. I could use a conky but the panel is always visible which is where I'd prefer it to be. Thanks.

TauTsar
Posts: 38
Joined: Thu Jun 21, 2018 4:49 am

Re: scripting help

#5 Post by TauTsar »

kmathern wrote: Tue Jun 26, 2018 7:49 am
TauTsar wrote: Tue Jun 26, 2018 7:25 amWhat I would like to do is add the values from rx_bytes and tx_bytes, then display the value in kilo bytes, rather then just bytes...
I think this snippet of code will get the sum of rx_bytes and tx_bytes and convert to kilo bytes

Code: Select all

cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc | awk '{print $1/1024}'
The man pages are like Greek to me; delimiters (reuse characters from LIST instead of TABs). “bc is a language that supports arbitrary precision numbers with interactive execution of statements.” Another language, lol. I’m not good at math either. Awk, there’s a whole book.

cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc | awk '{print $1/1024}'

This works in the terminal but in Generic Monitor I’m given: cat invalid option – ‘d’ Try the man pages for more help, lol. Cat –help for more information actually.
Still looking..

User avatar
handy
Posts: 611
Joined: Mon Apr 23, 2018 2:00 pm

Re: scripting help

#6 Post by handy »

I really think that conky is your easy way out. You can position your conky output anywhere on the screen that your want. You can make it always be there on top of whatever is on your screen if you want, or not. If you make it always be there on top VLC knows that it should be on top of your conky when it plays a video - VLC takes care of that automatically for you. :)

Go looking for conky scripts, find one that is as close to what you want, & then modify it. Conky isn't that hard to learn, & trial & error is your (certainly my) friend.

Here is a conky that I use that doesn't quite do what you want, but it really only needs the slightest modification to do what you do want:

Code: Select all

######################
# - Conky settings - #
######################

background yes
imlib_cache_size 0
double_buffer yes
no_buffers yes

#####################
# - Text settings - #
#####################
use_xft yes
xftfont Ariel:size=20
#text_buffer_size 1024

#############################
# - Window specifications - #
#############################
own_window_class Conky
own_window yes
own_window_type normal
#own_window_type panel
own_window_transparent yes
#own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
own_window_hints above,undecorated,sticky,skip_taskbar,skip_pager
own_window_argb_visual yes
own_window_argb_value 0

alignment bl
gap_x 43
gap_y 1048

draw_borders no
border_outer_margin 0

#########################
# - Graphics settings - #
#########################
draw_shades no

default_color efefef
default_shade_color 1d1d1d
color0 ffffff
color1 ffffff
color2 ffffff


TEXT
## execi tells a command to repeat - every 10 seconds (or whatever ?) it checks the external IP address. I've stuck my wireless in/out speed on the end (wlp6s0 = Artix; wlan0 = Devuan). Anyone else using this will/may need to modify the script to suit the name of their wireless/wired device.

${font Ariel:pixelsize=24}${color pink}External IP:$color  ${color green}${execi 15 curl -s ipinfo.io/ip}$color ${if_existing /sys/class/net/wlan0/operstate up}${color pink}Down:$color $alignr ${downspeed wlan0} ${color pink}Up:$color $alignr ${upspeed wlan0} ${endif}
You really should only need to edit the last line of the above script, the rest of it is just all of the info' it requires to set it up. My screen res' is 1080p. If yours is different then the conky output is going to be in the wrong place, so the other lines to edit to make this conky display as "I" intended are these ones in the above script:

Code: Select all

gap_x 43
gap_y 1048
You could likely want to change the type of font & more likely the font size (most likely if you are using a different screen resolution than 1080p. To do that you edit this line in the conky script above:

Code: Select all

xftfont Ariel:size=20
The script is setup for my wireless device, it is REALLY easy to change that to your wired device (or a differently named wireless device) - all you have to do is replace any instances of wlan0 with whatever the name of the network device that your machine is using. The hardest part "may" be identifying the device's name.

This page https://www.cyberciti.biz/faq/linux-lis ... s-command/ has good info on the topic. I find that using the terminal command ip link show is the easiest these days. You just look at the output from that command & find the device that has UP written in its output. As shown below (the device that is "UP" is my wireless network device called wlan0, it is number 3. in the ip link show command's output as shown below):

Code: Select all

   [handy@madmx ~]$ ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
    link/ether 80:fa:5b:03:4d:15 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
    link/ether [b](alpha numeric addresses that I removed... ;) )[/b]
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN mode DEFAULT group default qlen 100
    link/none 
The colours are REALLY easy to change too. All you have to do is a tiny bit of research & you can make this script yours.

Have fun, 'cause it IS fun, much more fun than just having someone give you the script that you want. ;)
Last edited by handy on Tue Jun 26, 2018 10:43 am, edited 1 time in total.
1_MSI: MAG B560 TORP', i5, RAM 16GB, GTX 1070 Ti 12GB, M2 238GB + USB, MX-23 Fb to Openbox
2_Lenovo: Ideapad 520S, i5, RAM 8GB, GPU i620, HDD 1TB, MX-21 - Openbox
3_Clevo: P150SM-A, i7, RAM 16GB, nVidia 8600, 2x 1TB HDD & M.2 256 GB, MX-21 - Openbox

TauTsar
Posts: 38
Joined: Thu Jun 21, 2018 4:49 am

Re: scripting help

#7 Post by TauTsar »

Thank Handy. I've been working with what kmathern provided.

If I have this: cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc | awk '{print $1/(1024*1024)}' >>/home/mrpolite/sum.txt , running in a loop which I don't yet know how to accomplish, then I can use the Generic Monitor panel plugin to read the sum.txt file for the value I want.

Problem; the sum.txt file should not accumulate more than the current value. The above code fails to remove the previous value.

User avatar
kmathern
Developer
Posts: 2403
Joined: Wed Jul 12, 2006 2:26 pm

Re: scripting help

#8 Post by kmathern »

TauTsar wrote: Tue Jun 26, 2018 10:42 am...If I have this: cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc | awk '{print $1/(1024*1024)}' >>/home/mrpolite/sum.txt , running in a loop which I don't yet know how to accomplish, then I can use the Generic Monitor panel plugin to read the sum.txt file for the value I want.

Problem; the sum.txt file should not accumulate more than the current value. The above code fails to remove the previous value.
If you the change the '>>' I highlighted in red to just a single '>', it should stop accumulating.

I also found the command below works in genmon without needing to use a sum.txt file. I did without the awk/print command I was using earlier.

Code: Select all

bash -c 'echo $(cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc)/1024|bc'

TauTsar
Posts: 38
Joined: Thu Jun 21, 2018 4:49 am

Re: scripting help

#9 Post by TauTsar »

I'll try that. I had learned about the single >. Thanks for the help. This is what I just had come up with but it's is working yet.

Code: Select all

#!/bin/bash
while true
do
        echo "Press CTRL+C to stop the script execution"

        # cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc | awk '{print $1/(1024*1024)}' >/home/mrpolite/sum.txt

done 
kmathern wrote: Tue Jun 26, 2018 12:00 pm
TauTsar wrote: Tue Jun 26, 2018 10:42 am...If I have this: cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc | awk '{print $1/(1024*1024)}' >>/home/mrpolite/sum.txt , running in a loop which I don't yet know how to accomplish, then I can use the Generic Monitor panel plugin to read the sum.txt file for the value I want.

Problem; the sum.txt file should not accumulate more than the current value. The above code fails to remove the previous value.
If you the change the '>>' I highlighted in red to just a single '>', it should stop accumulating.

I also found the command below works in genmon without needing to use a sum.txt file. I did without the awk/print command I was using earlier.

Code: Select all

bash -c 'echo $(cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc)/1024|bc'
Last edited by TauTsar on Tue Jun 26, 2018 12:20 pm, edited 1 time in total.

TauTsar
Posts: 38
Joined: Thu Jun 21, 2018 4:49 am

Re: scripting help

#10 Post by TauTsar »

Thank you so much. That's the magic.
kmathern wrote: Tue Jun 26, 2018 12:00 pm

Code: Select all

bash -c 'echo $(cat /sys/class/net/wlan0/statistics/{rx,tx}_bytes | paste -sd+ | bc)/1024|bc'

Post Reply

Return to “General”