Workaround OpenWrt scripting

Im not exactly a professional but im trying to implement some scripting to take the interfaces with awk in this case and use them like underneath example. Im trying to avoid installing any unnecessary full packages on OpenWrt.

What ive come up with is the following and it works for Linux afaik, not for awk on OpenWrt. Anyone any clue? Im hesitating its cause of minimal packages or Openwrt

for i in "$(ifconfig | grep "RUNNING" | awk '{print $1}' | grep ':' | tr -d ':' | grep -v lo)"; do
stuff ; done 

My bad just checked right now the layout of ifconfig is different on Linux. No wonder its not working... The problem is I intended this to have some compatibility over several devices. Anyone knows any better method than the above?

So far only methods like underneath work both on both. But its not really a clean method

echo "$(ifconfig | awk '{print $1}' | grep -v "UP\|RX\|inet\|TX\|collisions\|Interrupt\|lo\|ether" | tr -d ':' )"

Id be glad hearing about a better workaround.

Check if this will work for you

root@Office:~# for i in $(ip link | grep 'UP\,LOWER_UP' | grep -v lo | awk '{print $2}' | sed 's/:\+$//;s/\@.*//'); do echo "$i"; done
eth0
tun0
br-guest
eth0.4
br-lan
eth0.1
eth0.2
wg0
1 Like

Worked like a charm. Thanks! Was wondering after seeing different output compared to ifconfig I have an extra interface br.lan:1 in ifconfig while running stock settings. This is actually on an x-wrt device.
But thanks to your skills I just applied your command on ifconfig to include that interface

for i in $(ifconfig | grep 'Link' | grep -v lo | awk '{print $1}' | sed 's/:\+$//;s/\@.*//'); do echo "$i"; done

Topic solved. Thanks again

Oh no, cant use ifconfig its completely different in Linux. Wrt device output:

eth0      Link encap:Ethernet  HWaddr XXX 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7141679 errors:0 dropped:0 overruns:0 frame:0
          TX packets:7142593 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:128 
          RX bytes:6975613319 (6.4 GiB)  TX bytes:6942682912 (6.4 GiB)
          Interrupt:5 

Linux output:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet xxx  netmask 255.255.255.0  broadcast xxx
        ether xxx  txqueuelen 128  (Ethernet)
        RX packets 150429  bytes 191380676 (182.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 84039  bytes 28358783 (27.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

If i could just grab regardless of ifconfig output just the interfaces. Wonder if there is a way to just pick the words starting from the far left forgetting the rest of the output.
ahh this seemed to solve it

for i in $(ifconfig | grep 'flags\|Link' | grep -v lo | awk '{print $1}' | sed 's/:\+$//;s/\@.*//'); do echo "$i"; done

Thanks again.

1 Like

One last thing, the original command within the script is exactly this way:

iface="$(ifconfig | grep 'flags\|Link' | grep -v lo | awk '{print $1}' | sed 's/:\+$//;s/\@.*//')"

for i in "$iface" ; do
ethtool -G $i rx $rx tx $tx
ethtool -K $i rx $fl tx $fl sg $fl tso $fl ufo $fl gso $fl gro $fl lro $fl rxvlan $fl txvlan $fl rxhash $fl ntuple $fl
ethtool -s $i duplex $duplex autoneg $autoneg
ethtool --negotiate $i
ethtool -r $i 
macchanger -rbe $i
iw phy $i set frag $frag
iw phy $i set rts $rts
iw dev $i set beacon_int $beacons
iw dev $i set txpwr $txpower
iw dev $i set power_save $pwrsave
iwconfig $i txpower $txpower
iwconfig $i power $pwrsave
iwconfig $i rts $rts
iwconfig $i frag $frag
iwconfig $i commit ; done 

However when I run it just applies on one interface. My intention was grabbing the list of all interfaces and loop them through this exact command to have them all applied without manual configuration but the way I have done it as im seeing just picks one interface out of the list. What am I doing wrong here?

nowdays ip command is used almost every linux instead of ifconfig.

ip -o link | awk -F ':' '{print $2}' should give you the list.

1 Like

Thanks a lot!
As for the command im guessing this is the way it needs to be done? Is this a good method? I think it seems to work.

for c in $(macchanger -rbe) ; do
for i in $(ip -o link | awk -F ':' '{print $2}') ; do
$c $i ; done ; done

Basically it works on that command but have no clue how to get it working on the rest. As of right now to give the impression of what Im trying to achieve underneath a copy paste. I dont mind if it tries applying wireless settings on lan because they wont get applied. Just trying to keep it as simple as possible while keeping it compact as long as it works. Dont know if its the right mindset. Would be glad to hear how its supposed to be done.

for i in $iface ; do
macchanger -rbe $i 
ethtool -G $i rx $rx tx $tx
ethtool -K $i rx $fl tx $fl sg $fl tso $fl ufo $fl gso $fl gro $fl lro $fl rxvlan $fl txvlan $fl rxhash $fl ntuple $fl
ethtool -s $i duplex $duplex autoneg $autoneg
ethtool --negotiate $i
ethtool -r $i 
iw phy $i set frag $frag
iw phy $i set rts $rts
iw dev $i set beacon_int $beacons
iw dev $i set txpwr $txpower
iw dev $i set power_save $pwrsave
iwconfig $i txpower $txpower
iwconfig $i power $pwrsave
iwconfig $i rts $rts
iwconfig $i frag $frag
iwconfig $i commit ; done

Edited the command to the latest attempt on getting it to work and think it does. Good enough I guess if it gets the job done. Thanks for the quick help. Bash is quite frustrating to learn at times as a beginner.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.