You are not logged in.

#1 2012-11-21 15:52:51

phunni
Member
From: Bristol, UK
Registered: 2003-08-13
Posts: 794

Single network connection using multiple profiles

Apologies if this has already been asked - I have searched!

Is there a way of setting up netcfg - or a wrapper script - so that it tries multiple profiles  but then stops when it finds one that works?  These profiles might be a mix of wired or wireless - so simply using net-auto-* doesn't look like a solution (unless I'm misunderstanding them?).

My laptop often lives in my office where it is connected to a wired connection.  But, being a laptop, it is sometimes moved and will need, in most cases, to connect to a wireless connection.

I'd like the system, during boot, to try profiles in a given order and stop when one connects - is this possible?

Offline

#2 2012-11-21 16:39:40

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Single network connection using multiple profiles

Run ifplugd first - if it finds a cable connection, run net-auto-wired, if it doesn't, run net-auto-wireless.

Offline

#3 2012-11-21 16:39:42

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Single network connection using multiple profiles

Sounds like what netcfg normally does if you enable the service? Or net-profiles as the initscript was called. (This is based on documention, never used this myself.)

https://wiki.archlinux.org/index.php/Ne … t-Profiles

Edit: I run net-auto-wired and -wireless with POST_UP and PRE_DOWN commands to stop net-auto-wired when a cable is plugged in and restart it when the cable is removed. Seemed to work pretty well, but I think I've never actually used a wired connection since I set that up...

Last edited by Raynman (2012-11-21 16:47:30)

Offline

#4 2012-11-21 16:45:42

phunni
Member
From: Bristol, UK
Registered: 2003-08-13
Posts: 794

Re: Single network connection using multiple profiles

@Raynman - no, I have tried that.  If you enter multiple profiles in /etc/netcfg then it will attempt to start all of those profiles.  I only want it to start the first one that works.

@tomk - sounds interesting.  I want this to start at boot, though, so I'm going to have to write a script.  Looking through the ifplugd man page, there doesn't seem to be an obvious way to check the result of ifplugd and enable a profile. Am I missing something, or is this simply the wrong way to go?

Offline

#5 2012-11-21 16:56:08

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Single network connection using multiple profiles

The ifplugd package includes a utility ifplugstatus. So this might work:

if ifplugstatus -q eth0; then
  # run net-auto-wired
else
  # run net-auto-wireless
fi

Offline

#6 2012-11-21 17:30:53

phunni
Member
From: Bristol, UK
Registered: 2003-08-13
Posts: 794

Re: Single network connection using multiple profiles

That's a good approach. Another approach, which would be slightly less scattershot, although, perhaps, also less efficient, would be to write a script which ran netcfg on all the profiles in network.d in a given order until one worked.  Does netcfg give a nice, simple exit value that could be picked up by a script?

Offline

#7 2012-11-28 20:33:08

phunni
Member
From: Bristol, UK
Registered: 2003-08-13
Posts: 794

Re: Single network connection using multiple profiles

It looks like there is a status code - presumably either 0 or 1.  Does anyone know how I would go about picking that up in a shell script? I could write a full on program to do it, but that seems a little overkill for this, although it puts me in more familiar territory.

Offline

#8 2012-11-28 21:44:08

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Single network connection using multiple profiles

You can pick it up directly in an if statement as in my previous reply. There is also the special variable $? which gives you the exit status of the last command.

http://www.tldp.org/LDP/abs/html/exit-status.html

Offline

#9 2012-11-29 11:10:46

phunni
Member
From: Bristol, UK
Registered: 2003-08-13
Posts: 794

Re: Single network connection using multiple profiles

Ok - this is how far I've managed to get with a lot of help from Google:

#!/bin/bash
i=0
while read line; do
	profiles[i]=$line
	((i++))
done < rotater.config
for p in $profiles
do
	netcfg -u $p
  	if [ $? -eq 0 ]
	then 
		echo "Started profile $p succesfully"
		break
	fi
done

As you can see - it reads in a list of profiles from a config file and tries to start them in order.  Except that it doesn't.  If the first one fails, for whatever reason, it simply stops and doesn't attempt to try any subsequent ones.

Can anyone see where I'm going wrong?

Offline

#10 2012-12-13 15:49:29

phunni
Member
From: Bristol, UK
Registered: 2003-08-13
Posts: 794

Re: Single network connection using multiple profiles

Never mind - I figured it out.  This is the script as it stands:

#!/bin/bash
i=0
while read line; do
	profiles[i]=$line
	((i++))
done < rotater.config
for p in ${profiles[@]}
do
	netcfg -u $p
  	if [ $? -eq 0 ]
	then 
		echo "Started profile $p succesfully";
		break;
	fi
done

Now I just need to write a service for it & a PKGBUILD

Offline

Board footer

Powered by FluxBB