Quick switching from CLI between LAN and WiFi

Wan Lan

Usually, I work on my LAN connection, but to use a printer I need to jump to a Wi-Fi connection. This is needed because the printer is connected to the other network and I can’t reach this network from my LAN. I prepared two simple bash scripts and saved it in the directory that included in the environment PATH variable, so I could call these two scripts from anywhere. This post is intended for saving these scripts.

The same actions can be called from the Network Manager, but it requires more steps, like find wifi connection, turning it on, finding a LAN connection, turn it off.

Anyway here are the scripts

Create 2wifi file

$ touch 2wifi && chmod +x 2wifi && nano 2wifi

with the next content

#!/bin/bash
 
INT_WIFI=ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep wlx | head -1
INT_ETH=ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep eno1 | head -1
 
nmcli dev disconnect $INT_ETH
nmcli dev connect $INT_WIFI
echo "\n"
nmcli dev status | grep -E "(DEVICE|$INT_WIFI|$INT_ETH)"

Create 2lan file

$ touch 2lan && chmod +x 2lan && nano 2lan

with the next content

#!/bin/bash
 
INT_WIFI=ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep wlx | head -1
INT_ETH=ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep eno1 | head -1
 
nmcli dev disconnect $INT_WIFI
nmcli dev connect $INT_ETH
echo "\n"
nmcli dev status | grep -E "(DEVICE|$INT_WIFI|$INT_ETH)"

If you are on wifi, just execute 2lan, eg

$ 2lan

and vice versa – if you are on lan connection just execute 2wifi

$ 2wifi

Notes

If something doesn’t make sure that the interface is parsed correctly from your ifconfig output and align the next command accordingly

#!/bin/bash
echo "Step 1 == WIFI"
ifconfig -a
 
echo "Step 2 == WIFI"
ifconfig -a | sed 's/:[ \t].*//;/^$/d'
 
echo "Step 3 == WIFI"
ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://'
 
echo "Step 4 == WIFI"
ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep wlx
 
echo "Step 5 == WIFI"
ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep wlx | head -1
 
echo "Step 1 == WIFI"
ifconfig -a
 
echo "Step 2 == ETH"
ifconfig -a | sed 's/:[ \t].*//;/^$/d'
 
echo "Step 3 == ETH"
ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://'
 
echo "Step 4 == ETH"
ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep eno1
 
echo "Step 5 == ETH"
ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep eno1 | head -1
 
## Make changes in the next lines
## INT_WIFI=ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep wlx | head -1
## INT_ETH=ifconfig -a | sed 's/:[ \t].*//;/^$/d' | sed -e 's/://' | grep eno1 | head -1

You May Also Like

About the Author: vo