Thanks Ogis, glad it worked for you.
Yes. This script works great in Debian 10. I'll try it tomorrow on Devuan 3.1
]]>What i like about this script is ive found a way to maintain timed loops inside the while loop itself using let. I would love any feedback to see if this is a viable script free from errors.
Thank you for this script. Works flawlessly and very well !
]]>if wlan0 is connected first and then usb0, the default is always going to be wlan0 and vice versa, would that be true?
Yes, I think you're right.
Another alternative:
ip link | awk '/UP /{print $2}'
dice wrote:ip l shows ifaces even if they are down whereas ifconfig and netstat does not. So this would make the script only output multiple if ip was used.
Ah yes, of course. But ifconfig shows all interfaces that are up, which includes interfaces which are not connected.
How about this:
ip r | awk '/^default/{print $5}'
^ That shows the name of the interface currently connected.
I suppose the default gateway is the way to go, so if wlan0 is connected first and then usb0, the default is always going to be wlan0 and vice versa, would that be true?. I wonder how conky does the gw_iface object though.
In regards to ifconfig, atleast in my case on devuan beowulf, if wlan0 is not connected it wont show up in ifconfig, only lo. where ip l all interfaces are shown regardless if they are up or down.
]]>ip l shows ifaces even if they are down whereas ifconfig and netstat does not. So this would make the script only output multiple if ip was used.
Ah yes, of course. But ifconfig shows all interfaces that are up, which includes interfaces which are not connected.
How about this:
ip r | awk '/^default/{print $5}'
^ That shows the name of the interface currently connected.
]]>ip route | grep -ow 'wlan0\|usb0' | sort -u
usb0
wlan0
otherwise without sort
ip route | grep -ow 'wlan0\|usb0'
wlan0
wlan0
usb0
dice wrote:If it is old and deprecated why is it still in the kernel?
It's not in the kernel, ifconfig is a(n obsolete) userspace utility.
And anyway what's wrong with
a=$(ip l | grep -ow "wlan0\|usb0")
Off topic for this thread but you should check out slstatus — pure C ftw!
In regards to why is it in the kernel, i should have referenced the answer i got from stackoverflow. I know it is part of the net-tools package so i was confused about this quote:
That said, ifconfig shouldn't be used at all on Linux either; it hasn't been maintained by the upstream kernel team for well over a decade now
In regards to ip, i have some issues with ip l , the difference being that ip l shows ifaces even if they are down whereas ifconfig and netstat does not. So this would make the script only output multiple if ip was used.
Here is ip l on my machine with usb0 and wlan0, with only wlan0 up.
ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
lo 65536 0 0 0 0 0 0 0 0 LRU
wlan0 1500 810 0 0 0 724 0 0 0 BMRU
ip l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
4: usb0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
I have used slstatus in the past, i may revert back to it again, although this endeavor was just to see if i could create shell scripts to pipe to dwm statusbar instead, which i have accomplished.
]]>If it is old and deprecated why is it still in the kernel?
It's not in the kernel, ifconfig is a(n obsolete) userspace utility.
And anyway what's wrong with
a=$(ip l | grep -ow "wlan0\|usb0")
Off topic for this thread but you should check out slstatus — pure C ftw!
]]>gw_iface Displays the default route's interface or "multiple"/"none" accordingly.
I was told over at stackoverflow i shouldnt be using ifconfig for something as simple as this, which i think is ridiculous, if i wanted to do fancy stuff with the networks i would use iproute2 but for this, not needed. If it is old and deprecated why is it still in the kernel?
This will print on my statusbar as follows,
If usb net connected = usb0
If wlan0 connected = wlan0
If both usb and wlan0 connected =multiple
If no network = not connected
#!/usr/bin/env bash
a=$(ifconfig | grep -ow "wlan0\|usb0")
b=$'wlan0\nusb0'
c="%printf '$b'"
if [[ "$a" = "wlan0" ]] ; then
printf "wlan0"
elif [[ "$a" = "usb0" ]] ; then
printf "usb0"
elif [[ "$a" =~ ["$c"] ]] ; then
printf "multiple"
else
printf "not connected"
fi
What i like about this script is ive found a way to maintain timed loops inside the while loop itself using let. I would love any feedback to see if this is a viable script free from errors.
#!/bin/bash
get_cpu (){
printf "Cpu:"
top -bn1 | awk 'NR==3 {print $2}'
}
get_disk (){
printf "| Disk:"
df -h | awk 'NR==4 { print $3" / "$2}'
}
get_mem (){
printf "| Mem:"
free -h | awk 'NR==2 {print $3" / "$2}'
}
get_temp (){
printf "| Temps:"
sensors | awk '/Core/ { printf substr($3,2,2)"°C " }'
}
get_time (){
printf "| Time:"
date +"%R"
}
let loop=0
while true; do
if [[ $loop%60 -eq 0 ]]; then
Time=$(get_time)
fi
if [[ $loop%300 -eq 0 ]]; then
Disk=$(get_disk)
let loop=0 #this prevents an eventual overflow
fi
if [[ $loop%8 -eq 0 ]]; then
Temp=$(get_temp)
fi
if [[ $loop%3 -eq 0 ]]; then
Mem=$(get_mem)
fi
if [[ $loop%2 -eq 0 ]]; then
Cpu=$(get_cpu)
fi
xsetroot -name "$Cpu $Temp $Disk $Mem $Time"
let loop=$loop+1
sleep 1
done
Update: ran this script through shell check, interesting diff.
https://www.shellcheck.net/
#!/bin/bash
get_cpu (){
printf "Cpu:"
top -bn1 | awk 'NR==3 {print $2}'
}
get_disk (){
printf "| Disk:"
df -h | awk 'NR==4 { print $3" / "$2}'
}
get_mem (){
printf "| Mem:"
free -h | awk 'NR==2 {print $3" / "$2}'
}
get_temp (){
printf "| Temps:"
sensors | awk '/Core/ { printf substr($3,2,2)"°C " }'
}
get_time (){
printf "| Time:"
date +"%R"
}
(( loop=0 ))
while true; do
if [[ $loop%60 -eq 0 ]]; then
Time=$(get_time)
fi
if [[ $loop%300 -eq 0 ]]; then
Disk=$(get_disk)
(( loop=0 )) #this prevents an eventual overflow
fi
if [[ $loop%8 -eq 0 ]]; then
Temp=$(get_temp)
fi
if [[ $loop%3 -eq 0 ]]; then
Mem=$(get_mem)
fi
if [[ $loop%2 -eq 0 ]]; then
Cpu=$(get_cpu)
fi
xsetroot -name "$Cpu $Temp $Disk $Mem $Time"
(( loop=loop+1 ))
sleep 1
done