# WLAN and LAN netscript, easy to install

## Zacay

Last update 30 may 2004

The problem

I have laptop with both a WLAN card and a LAN card, and I had trouble finding a script that handles this 

At home I have wireless and at work I have wired.

And having both on rc-update default was a pain when one of the networks wasn't available... Always waiting for dhcp..

And sometimes I don't have connection at all... And the wait is unbearable!

To add more problems, my brother have same ESSID on his network as mine but a different key. So I needed configs not only based on ESSID.

And finally my neighbour has a router that he keeps open essid but with macfiltering, his network seemed available but it isn't and netowork scripts tries to connect.

I've seen some fixes to LAN script by adding mii-tool stuff but they never worked for me good.. I don't know why!!

And anyway when I emerge new baselayout I need to reedit these files and add mii-tool stuff again.. ( I never understood why mii-tool isn't on by default )

And for WLAN, i found somescript, but when emerging new baselayout I had to do some editing to keep them updated, or I never felt satisfied with the configuration or how the automatic search is done for networks.

The script

Checks for both wireless and for wired cards.

No wait for dhcp when you don't have a network connection.

Checks for already preferenced networks (Both open and hidden)

Checks for open wireless networks

Handles several network with same ESSID but different properties.

Easy installation and simple config.

The script don't need any updating everytime when new baselayout and netscript are installed. It uses the default netscript.

Its not a modified version thats need some work when you emerge new baselayout.

And it solves all my problems with wireless networking

I choose not to use modified net script cause adding code to already complex code usually leads to more unwanted bugs, so this script have to be alone and so simple as possible. Maintaining gets easier like this.

Requires

1. mii-diag (I think its a default package)

2. wireless-tools (You should have this installed to connect with wireless)

I am trying not use use other packages than default, here a thread I started. Check if cable is in network card, without extra tools ???

Installation

(Assuming eth0 is your wired network card and eth1 is your wireless card)

Backup everything, USE it at your OWN RISK!!

```
# cd /etc/init.d/

# wget http://www.zacay.com/gentoo/net.zacay

# rc-update add net.zacay default

# rc-update del net.eth0

# rc-update del net.eth1 (if you have it)

# rm net.eth1 (if you have it)

# ln -s /etc/init.d/net.eth0 /etc/init.d/net.eth1

# mkdir /etc/conf.d/zacay

I link net.eth1 to net.eth0 instead of having another script, and when you emerge baselayout, you always have the latest net script for both lan and wlan.

```

Configuration of WLAN networks

You should only configure networks that needs a key, or networks that are invisible. (Configuring your networks will make the script find your network faster). The script finds open networks automatically.

The configuration files should be in /etc/conf.d/zacay/

```
Lets say you have a network RADIOLAN with a string key

iwconfig eth1 mode Managed

iwconfig eth1 essid RADIOLAN

iwconfig eth1 key <YOUR KEY HERE>

 - Read iwconfig about how to set up the key and other things

The config file would be /etc/conf.d/zacay/RADIOLAN and it should contain:

mode Managed

essid RADIOLAN

key <YOUR KEY HERE>

The config files are case sensitive to the essid! (This will be fixed)

```

Configuration of WLAN networks with same essid but different keys Or one without a key...

Same as above but now you need to ad one more line for each network

configfile to this essid.

Lets say you have a home network called RADIOLAN and at work you have the same network name but different key.

Create two files one RADIOLAN or RADIOLAN_HOME

and one RADIOLAN_WORK. (You must use underscore for different files, but one file can be without anything, like only RADIOLAN)

Now you need to know the accesspoint address for each network

```
# ifconfig eth1 up

# iwconfig eth1 essid RADIOLAN

# iwconfig eth1 | grep "Address" (if it don't show up, try this command again)

# ifconfig eth1 down

```

 Do this for each network and add the address to the config file like this

```
ap <AP_ADDRESS>
```

And each config file can support several accesspoints, lets say you have a third network RADIOLAN at school with same key as work, you dont have to create a new config file, just ad that accesspoint address to the RADIOLAN_WORK ap line.

```
ap <AP_ADDRESS> <AP_ADDRESS> ... ... ...
```

HintsIf you add ap mac address to config files, the search will go much faster.

Todo (To be fixed)Config files are case sensitive

Config files aren't so great, you need to know iwconfig

Change logCheck for already preferenced networks first then tries to find open

Restarting bug fixed

Code cleaning and variuos bugfixing

Supports several networks with same essid and different keys

Config files support comments #

Config files don't need the .NET ending

Added check for wireless-tools

Script handles even net.wlan (Thanx UberLord for telling me about net.wlan)

Now the script handles both net.eth and net.ath

Added a check for mii-tool

Seperated wired and wireless checking to different functions

Changed order of network checking, now it checks wired network before wireless. This makes the script faster when you want wired network, cause before it tried to check wireless first and that take more time than check wired network. (Thanks to Magnus)

Hiding behind a stone

I don't know if there is a smarter way for handling this (probably there is), but I grew tired of not finding it so I made this myself and now everything works great.

This is my very first bash script so I learned some bash on the way of making it.  :Very Happy: 

This helped me to learn bashscripting a litte, still have much to learn. Advanced Bash Scripting Guide

I am welcome for tips and corrections.

----------

## UberLord

Whats wrong with ifplugd   :Question: 

----------

## Zacay

Well, I actually dont know..  :Very Happy: 

I am a newbie, so I just learn the commands I used.. Don't know much more  :Very Happy: 

If I tell you that I just learned using grep and the meaning of | .. then you know how much I can..

But I will look into ifplugd...

This is what I like with linux, it make me want to learn, and I feel more and more comfortable using it.. Its become my main desktop, and I've been only using it one year..

----------

## UberLord

 *Zacay wrote:*   

> 
> 
> This is what I like with linux, it make me want to learn, and I feel more and more comfortable using it.. Its become my main desktop, and I've been only using it one year..

 

What I like is how customisable and interchangable it is.

You have your script - and that's fine and dandy.

I have mine and the world is happy  :Smile: 

----------

## magatgentoo

Hello,

Your script works well. If this is your first script: "chapeau!". 

Here are some comments:

 *Quote:*   

> Problems (To be fixed)
> 
> 1. The script works only if your network cards starts with "eth" 

 

I use the madwifi driver (device athX), it works when modifying "eth" to "ath" in the grep statement:

```
  # Check for wireless

        ebegin "Checking for wireless card"

                eth=$(iwconfig 2> /dev/null | awk '{print $1}' | grep 'ath')

                if [ ${#eth} -gt 0 ] ;

```

I think it could be a good idea to create a variable with the grep statement to be more customizable.

 *Quote:*   

> 3. Config files aren't so great , you need to know the iwconfig command still
> 
> 4. Config file names case sensitive

 

For 3., simply do variables: key, ssid, mode... It think this can be enough, for expert you add a variable "other_opts=<other options goes here>" 

For 4., that should not be a big issue to change if really needed.

As general comment, I think it is more practical to check for wired connection before wireless, don't you think? For example, you need to transfer a file *fast*, have wireless but are connected to your 100 Mb/s lan...

There is no point in having wireless if you already have wired, faster connection  :Smile: 

Once again, well done.

Magnus

----------

## Zacay

 *magatgentoo wrote:*   

> Hello,
> 
> Your script works well. If this is your first script: "chapeau!". 
> 
> 

 

Yeah, thanx!

 *magatgentoo wrote:*   

> 
> 
> I use the madwifi driver (device athX), it works when modifying "eth" to "ath" in the grep statement:
> 
> ```
> ...

 

Yeah I was thinking about that, but I want the user do as little as possible.

But you gave me another idea.. Maybe I can add a check for both eth and ath. I actually haven't seen other names.. Is there?

If its only eth and ath,  its easy, if not I will add variables..

Adding variables gives another advantage, the script wont have to search  :Very Happy: 

 *Quote:*   

> 
> 
> For 3., simply do variables: key, ssid, mode... It think this can be enough, for expert you add a variable "other_opts=<other options goes here>" 
> 
> For 4., that should not be a big issue to change if really needed.
> ...

 

Good idea, I will implement this soon. Its nice to have good configs.. 

 *Quote:*   

> 
> 
> As general comment, I think it is more practical to check for wired connection before wireless, don't you think? For example, you need to transfer a file *fast*, have wireless but are connected to your 100 Mb/s lan...
> 
> There is no point in having wireless if you already have wired, faster connection 
> ...

 

Absolutly, I will do this, and another great thing with having wired first is if the cable isn't in, it will be faster for the script to continue to wireless than having the wireless card searching for networks untill it finds nothing!!

Thank you, good comments  :Very Happy:  now time to change some stuff.. I will update it later on this thread..

----------

## UberLord

Generic wireless card detection from my script

```
if [ -x "${IWCONFIG}" ]; then

   wireless_IFACE="$(${IWCONFIG} ${IFACE} 2>/dev/null | gawk -v IFACE=${IFACE} '{ if ($1 == IFACE) print "yes" }')"

fi

wireless_IFACE=${wireless_IFACE:-"no"}
```

Not being funny or anything, but why don't you look through my script and nick anything from there that you need? I've done an awful lot of work with wireless scripts and you may as well use some of it  :Wink: 

----------

## UberLord

 *Zacay wrote:*   

> I have both a WLAN card and a LAN card.
> 
> At home I have wireless and at work I have wired.
> 
> And having both on rc-update default was a pain when one of the networks wasn't available... Always waiting for dhcp..
> ...

 

ifplugd is the package that you need - starts and stop net scripts when an ethernet cable is plugged in / removed.

 *Quote:*   

> 
> 
> And for wireless lan, i found somescript, but to keep them updated I had to download them again and again..
> 
> I needed a script that first checks for wireless and then for wired cards.. 
> ...

 

Refering to my script? Well, yes - it gets updated. Normally with minor issues or new features at the request of users. I challenge you NOT to update your script. If you do, the same critique can be levelled at you.

My script checks for wireless extensions and handles it appropriately (see above post)

The ebuild of my script doesn't require updating when new baselayout/netscripts are installed - thanks to the new preup() predown() functions that the new net.eth0 in baselayout >= 1.8.8 provides.

However, as my script also overrides network settings for net.eth0 (iface, ifconfig, dhcpcd, etc) then they have to be kept in sync with net.eth0's internal nameing scheme - so they do have to match to a certain extent.

Sorry about the rant - just knocking down a few of your points   :Razz: 

----------

## Zacay

 *UberLord wrote:*   

> 
> 
>  *Quote:*   
> 
> And for wireless lan, i found somescript, but to keep them updated I had to download them again and again..
> ...

 

No I dont refer to your script, actually never used your script, I saw it but never downloaded it.

And if you clearly read what I wrote above, you will understand that I don't want a script that needs updating everytime I emerge baselayout plus some other stuff.

With my script I don't have to, cause it uses the default net script from baselayout, and by that I always get the latest fixed netscript, no need to overwrite default net script.

So using my script your system is always updated with only the emerge world command there is no reason to go to the net and download my script everytime. And nor do I have to update my script, even with new releases of baselayout. So maintaining my script is easy.

Baselayout gets updated alot, my script wont get many more updates.. The main reason I put my script in the forum is not to post a finished script, it was to get some help finishing up my last problems and then my script is done. Thanx to Magnus for giving me some ideas. People like him are helpfull, and that the kind of people usually are in gentoo forums.

And another advantage with this script is if other services depends on net script, like apache mysql and other services, they will still start if no network available...

And whats up with a challenge?

 *UberLord wrote:*   

> 
> 
> My script checks for wireless extensions and handles it appropriately (see above post)
> 
> The ebuild of my script doesn't require updating when new baselayout/netscripts are installed - thanks to the new preup() predown() functions that the new net.eth0 in baselayout >= 1.8.8 provides.
> ...

 

I never mentioned you script nor used it, I am sure it works perfectly and has alot of advantages of the real net script with baselayout. But it wasn't what I was searching for. Anyway, I never understood the net script in baselayout as good as you do so I couldn't go inside of them and edit them, it to advanced for me. And again even if I did understand them, I wouldn't edit them, it wasn't what I wanted for my system.

And again, "preup" and "predown", well I told you I am a noob.

My script isn't here to replace other netscript, it here because I dont want to have both net.eth0 and net.eth1 on rc default and I dont want to make any grub choices. I just want it to handle everything automatically. And still using original scripts. And I feel like my script is eaiser to update even they do mayor updates on default net scripts.

I am sorry that you feel like knocking down my points, but you missed me  :Razz: 

----------

## UberLord

My ebuild script doesn't alter net.eth0 at all. So mine uses the latest.

It does require a patch to /etc/conf.d/net though - but the ebuild tells you when you need it and how it install it. There are reasons for this, but I won't go into them here.

But you really ought to look at other packages.

ifplugd will start and stop network scripts if an ethernet cable is plugged in or removed. It also works with wireless cards. Using ifplugd, you don't need any net.xxx scripts in any runlevels as the daemon takes care of starting and stopping them for you.

No need to alter grub either!

waproamd is by the same author to do the same job as ifplugd but only for wireless devices.

Good luck with your script though  :Smile: 

PS - part of it got me off my lazy arse to remove IFS variables from mine and indicate whether a given ESSID required WEP or not.

Competition is good and healthy   :Cool: 

----------

## Zacay

Well forget competition, I am not into that. So you are competing with yourself.. I did this script for my self, and I don't need people to use it, so competing is useless in my case.. I just share this script with anyone who wants it.. And I dont count users nor posts.. 

And yes I will read about ifplugd, thanx for the advice..  :Very Happy: 

----------

## Zacay

 *magatgentoo wrote:*   

> 
> 
>  *Quote:*   Problems (To be fixed)
> 
> 1. The script works only if your network cards starts with "eth"  
> ...

 

I updated the script to this in all occurences:

```
grep '[ea]th'
```

And now handles both... hehe easy  :Very Happy:  *dooh*

----------

## UberLord

 *Zacay wrote:*   

>  *magatgentoo wrote:*   
> 
>  *Quote:*   Problems (To be fixed)
> 
> 1. The script works only if your network cards starts with "eth"  
> ...

 

You forgot about wlan0.

----------

## Zacay

 *UberLord wrote:*   

> 
> 
> You forgot about wlan0.

 

Oh, didn't know that there was wlan0..

Ok redid the command to this

```
iwconfig 2>/dev/null | sed -n '1p' | awk '{print $1}'
```

now it should work,  I removed the [ea]th check  :Very Happy: 

EDIT: UPDATED THE CODE

----------

## Zacay

I have made a nice update to the script:

Two or more different networks but same essid

Now the script can handle several networks with same essid!

So if at home you have a network called RADIOLAN with your own key and at work you have another network called RADIOLAN with another key, I have the solution.  :Very Happy: 

For different networks with same essid and different keys, just do more config files, like:

RADIOLAN or RADIOLAN_HOME

RADIOLAN_WORK

And in both files you should ad a new line

```

ap <The access point mac address>

```

This will restrict the config file for that accesspoint. You can add more accesspoints for each file, seperating them with space, and you get a config file that works on two accesspoints.

You can easily get the mac address

```
ifconfig eth1 up

iwconfig essid RADIOLAN (wait a second or try the next command a couple of times till you get the address)

iwconfig eth1 | grep "Access" 

```

Config files

Config files support # comments now so that you can comment stuff out and maybe put some notes in. But still you need to know iwconfig command. (easy thing anyway)

Config files don't need .NET ending anymore, but it still can find them, so no need to change them. Also the ending isn't case sensitive anymore.

But files should be the same case as ESSID

Well all this made me go though my script and I have corrected some stuff I didn't see before. So there alot of changes. But no problem, it still works and also works with the old config files with ".NET" ending..  :Very Happy:   :Very Happy:   :Very Happy: 

----------

## Zacay

It was time for an update, (if there is anyone interested)  :Razz: 

This time I have done alot of updates and cleaned the script alot.

I've added a new network check for wireless,

now it first checks for your already preferenced networks, it searches the config files for essid's or for hidden networks it searches for the ap addresses (that is if you added the ap mac address in the config files)

And after this it now checks for other hidden networks that you haven't put the mac address of.

And finally it searches for open networks.

This made the script alot better and faster. At the same time I fixed bugs and refined the code.

I made this update mostly because my neighbour got himself a wireless router and he has open essid and only macfiltering, so my old script tried first to connect to his router. 

I also fixed my restart bug.

So now I have fixed all my bugs and todo list.

There are probably some bugs in the script, if you find any please post the error output here  :Very Happy:  (if there is anyone using it, that is)

I haven't come up with more stuff to do, any ideas?

My next step is probably a gui for my script, so this can be controlled in an easier way.

----------

