Discussion:
[Help-bash] eth0 up or down?
Mike McClain
2017-04-23 05:08:49 UTC
Permalink
Howdy,
How can I tell from bash whether eth0 is up or down that would be
faster or less expensive than $(ifconfig eth0;)? Is there some where
in /proc or elsewhere in memory a bash program can reach?
Thanks for your thoughts,
Mike
--
A wise man will make more opportunities than he finds.
- Francis Bacon
Pierre Gaston
2017-04-23 13:35:19 UTC
Permalink
Post by Mike McClain
Howdy,
How can I tell from bash whether eth0 is up or down that would be
faster or less expensive than $(ifconfig eth0;)? Is there some where
in /proc or elsewhere in memory a bash program can reach?
Thanks for your thoughts,
Mike
--
A wise man will make more opportunities than he finds.
- Francis Bacon
You don't mention the OS you are targeting, you might be able to do
something like: $(</sys/class/net/eth0/operstate).
Though if you are not running this in a quick loop the price you pay for a
fork is probably not a problem
Mike McClain
2017-04-24 04:56:49 UTC
Permalink
Post by Pierre Gaston
Post by Mike McClain
Howdy,
How can I tell from bash whether eth0 is up or down that would be
faster or less expensive than $(ifconfig eth0;)? Is there some where
in /proc or elsewhere in memory a bash program can reach?
You don't mention the OS you are targeting, you might be able to do
something like: $(</sys/class/net/eth0/operstate).
Though if you are not running this in a quick loop the price you pay for a
fork is probably not a problem
Thanks Pierre,
That will do nicely.
Oops, you're right, Debian Linux so your advice was spot on.

Your pointer allowed me to find this substitution too:
From this:
pk=$( ifconfig $eth | awk '/TX packets:/{print $2;}' )
pkts=${pk#*:}; # packet count
To this:
# due to ***@gmail.com
pkts=$(< /sys/class/net/eth0/statistics/tx_packets)

Which will obviously will be quicker. I'm using 'old iron',
P3 1/2M ram so try to be frugal with bytes and time.
It's just a little script to bring eth0 down if I'm doing something
else but I want to show as little footprint as needful.
Thanks again for your help.
Mike
--
Toward a happier life, always hang up immediately you've found
you've been called by a machine. - MM

Loading...