Need ifconfig lo create name to fail instead of incrementing lo

vbman213

New Member


Messages: 8

I'm running

Bash:
ifconfig lo create name myrandomname

This works fine. However, if myrandomname already exists, ifconfig will still create a lo interface by just incrementing lo1 to lo2. I need it to fail.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 11,618
Messages: 37,958

Don't do the creation and renaming in one go.
Code:
    create  Create the specified network pseudo-device.  If the interface is
             given without a unit number, try to create a new device with an
             arbitrary unit number.  If creation of an arbitrary device is
             successful, the new device name is printed to standard output
             unless the interface is renamed or destroyed in the same ifconfig
             invocation.
 
OP
V

vbman213

New Member


Messages: 8

So I guess I have to

Bash:
ifconfig lo create

then

Bash:
ifconfig lo1 name MYNAME

and then if that fails

Bash:
ifconfig lo1 destroy
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 11,618
Messages: 37,958

How about testing if the interface exists and creating it if it doesn't?
 
OP
V

vbman213

New Member


Messages: 8

Yea, I'm terrible at bash. I know if I run

Bash:
ifconfig lo create MYNAME
and it fails i.e. $?<>0 I can do something like this:

Bash:
ifconfig lo create name MYNAME || .......
is there anyway to use that logical operator and grab the output of the create to turn around and destroy it?



Edit:

So I'm looking for a one-liner:

The only solution I've got is

Bash:
ifconfig lo create name MYNAME > /tmp/error || ifconfig `cat /tmp/error` destroy
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 11,618
Messages: 37,958

I'm terrible at bash.
It's a shell script. Use sh(1), not bash. And we all had to start at the beginning. This site is always useful: https://www.grymoire.com/Unix/Sh.html

I'll give you a hint, look at the output of ifconfig -l, parse the output and if you can't find the interface you're looking for then create it.

[Moved the thread to "Userland Programming and scripting" because this is more related to scripting than networking.]
 

olli@

Daemon
Developer

Reaction score: 1,235
Messages: 1,128

I would check upfront if an interface with that name already exists, and create it only when needed.
There are several ways to do it …
Code:
if ! ifconfig myname >/dev/null 2>&1; then
        ifconfig lo create name myname
fi
or if you prefer a one-liner (I think this is uglier, though):
Code:
ifconfig myname >/dev/null 2>&1 || ifconfig lo create name myname
You can omit the redirections (>/dev/null 2>&1) if you don’t mind the output from the first ifconfig command.
 
Top