Linux network question

I have two systems with wifi adapters when they are in my house I want them to talk to my AP and get an IP. When they are not in my house I want them to run in ad-hoc mode to talk to each other. How can I do this?

AP == Access Point?

I’m pretty sure that they need to have the same IP all the time, in order to comunicate with each other.

I haven’t played much with wireless networks, so all I can do for you is to bump this, and direct you to The Linux Wireless LAN Howto.
Good Luck!

Yes AP is Access Point. I guess what I was thinking was a way of sensing if they are near the AP (possibly by seeing if they get an IP) and if no IP is assigned on bootup run a script that sets them to ad-hoc mode (peer to peer) and manually assigns an IP

The only way I can think of to do this would be with a perl (or some other language, maybe even straight shell) script. You’d have to issue an iwconfig command to attempt to associate with the access point. If it succeeded, it would then run dhcpcd or pump to get an IP address via DHCP. If it failed, it would issue another iwconfig to put the card into ad-hoc mode and an ifconfig to give it a static IP. This script would have to run on both laptops, obviously, and it also would have to check periodically to see if it had lost communication with the access point or not, either using ping (or something like it) or by parsing the output of iwconfig for signal level and switching to ad-hoc mode if it fell below a certain threshold.

One problem I see is that it’d be tricky to detect when it needed to make the switch from ad-hoc mode back to managed (AP) mode. I can’t think of a clean way to do that which doesn’t disrupt connectivity for a moment.

If you just want this process to happen at boot time, it’s a lot easier. The steps would basically be:

  1. Use iwconfig to attempt to associate with the AP
  2. Parse the output of iwconfig to see if it was successful
  3. If yes, use dhcpcd or pump to get an IP address
  4. If not, use iwconfig to set up ad-hoc mode, then ifconfig to assign a static IP

That help?

(woohoo, my first GQ post!)

pestie what you described doing on bootup is exacly what I need. Do you know where I can find examples of shell scripts I might be able to modify?

I don’t know where you’d find any examples, unfortunately. Sorry.

Most linux network startup files contain some conditional statements already, so I don’t think this is too out there. What version are you running (because it matters whether you are using System V inits or BSD style)? I’m pretty sure if you are set up with DHCP, your init (rc.inet1 in Slackware) will contain a conditional statement with what to do if DHCP fails (at the least it should display an error). So all you should really need to do is add to that a second set of IP configuration settings with static addresses. I don’t have access to my linux box ATM or I would be more specific, but if you take a look at your startup files you should be able to figure it out. The Slackware website has sample rc.inet scripts, but they don’t show a DHCP sample.

One is Gentoo and one is Slackware

Actually, now that I think about it, what prevents you from using the same static IP for both setups?

Tourbot I don’t want to use a static IP at home because I would have to lock out the IP from the DHCP server and I would still have to put it into ad-hoc mode when I am using the two systems away from home

Huh? Why can’t you statically assign the same IP address (or one in the same range) that the DHCP server would assign? That’s how I connect my Linux box to the network at work. I’ve never worked with wireless, so maybe there is something I am overlooking as a result.

OK, I finally have my wireless laptop back in the house (I left in in the car overnight in frigid weather - I’m glad it still works). I threw together a simple perl script to do what you want. I’ve tested it minimally, and it should work with most common configurations, but it may need some tweaking if you’ve got anything unusual in your setup. Let me know if you have any questions about this script, or about making it work with a different setup.



#!/usr/bin/perl

# Configuration - change these to match your setup

$wlanif = 'eth1';               # Ethernet interface of wlan card
$essid = 'illuminati';          # ESSID of home access point
$staticip = '192.168.2.23';     # Address to assign in ad-hoc mode
$netmask = '255.255.255.0';     # Netmask to use in ad-hoc mode
$broadcast = '192.168.2.255';   # Broadcast address to use in ad-hoc mode
# First, attempt to associate with access point, then check the output
# of iwconfig to see if we were successful

system("/sbin/iwconfig $wlanif essid '$essid'");

sleep(2); # Wait two seconds for association to happen

$currentconfig = `/sbin/iwconfig $wlanif`;

# The output of iwconfig should have the hardware address of the associated
# access point. If it's the default, unassociated "FF:FF:FF:FF:FF:FF" then
# association has failed, so fall back to ad-hoc mode.

if ($currentconfig =~ /Access Point: FF:FF:FF:FF:FF:FF/) {
  print "Association failed, falling back to ad-hoc mode... ";
  system("/sbin/iwconfig $wlanif mode Ad-hoc");
  system("/sbin/ifconfig $wlanif $staticip netmask $netmask broadcast $broadcast");
}
else {
  print "Association with AP successful! Attempting to get address via DHCP... ";
  system("/sbin/dhcpcd $wlanif");
}

print "Done!
";



One more quick note about this script: it assumes that the wireless card is in a virgin state (unconfigured because the system just booted, or it was just inserted if it’s a PCMCIA card). If you run it when the card’s already been configured by some other method it probably won’t do what you want. One fix I should probably add is to change this line:



system("/sbin/iwconfig $wlanif essid '$essid'");


to this:



system("/sbin/iwconfig $wlanif mode Managed essid '$essid'");


Every card I’ve seen defaults to managed mode, but in case yours doesn’t, this’ll make it work properly. Also, this change may make it work on an already-configured card in some situations.

Finally, just to be clear, I wrote every line of this script myself, so I’m the copyright holder, and I’m hereby placing this code into the public domain. If someone finds a way to make a million dollars using this code, by all means, send me a cut of the profits, but somehow I doubt that’s going to happen. :slight_smile:

Oh, and this script doesn’t include a provision for WEP. If you need that, I’ll add it, but you can probably figure out how to add it yourself pretty easily. I left it out because WEP is mysteriously broken on my AP at home and I can’t use it at all (I use a strong firewall and a VPN to keep out troublemakers.)