Quick Script to Show Changing Network Interface

I’ve been playing with the Raspberry pi’s for a long while, and have A TON of them. eventually on my travels I learned that you could plug in a Rasperry Pi ZeroW (RPiZW for short) and have it register as a network device so you can SSH into it. For me, it was a pain because the interface suffix would change every time I plugged it in. I got tired of trying to remember the interface name (and you can bet I was close to memorizing it by the time I wrote this one liner) so I analyzed what part of the interface name stayed the same. This allowed me to do a simple scripting to get the interface.

What are we actually doing here? When it comes to shell scripts/commands, the items inside the ` characters(it’s below the tilda, just under your escape key) indicate that this is something that needs to be executed or evaluated.  Therefore that command or set of commands will be processed first. We first take the output of ifconfig, and pass it over to, or ‘pipe’ it into the grep program. With the grep program we’re looking for lines that contain the text ‘enp0s20f’. That’s because this is the part of the interface that never changed for me (To be honest, more of the interface name didn’t changed, but I wanted to keep the search criteria loose).

Nice, we get the interface name, and a bunch of other information. We really only want the interface name, so we’re going to pipe the output through to the cut command.

What cut is doing, is literally “cutting” the text into pieces, using the colon (:) as the thing that separates the pieces. Then, it’s only keeping the first piece.

Pefect! it’s the interface name! Now lets use that as an input to ifconfig

Now that we’ve debugged our one-line script, we can save it as an alias with the alias command by encapsulating it with quotations (“). Make sure there’s no spaces between the alias name (rifconfig), the equal (=) sign, and the first quote (“).

we then make this a permanent alias for this user by echoing the command into the users .bashrc file (in the users home folder, which is indicated by the ~)

Taking it once step further, we incorporate setting up the interface with an IP, and sshing into the host (because civilized people use keys!)

Now I have a single, 5 letter command to setup and connect to my RPiZW

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.