# Linux network question

**URL:** <https://boards.straightdope.com/t/linux-network-question/149722>\
**Category:** Factual Questions\
**Created:** [January 20, 2003, 2:54am UTC](https://boards.straightdope.com/t/linux-network-question/149722 "2003-01-20T02:54:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![MannyL](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@MannyL](https://boards.straightdope.com/u/MannyL)\
**Post date:** [January 20, 2003, 2:54am UTC](https://boards.straightdope.com/t/linux-network-question/149722/1 "2003-01-20T02:54:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Popup](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Popup](https://boards.straightdope.com/u/Popup)\
**Post date:** [January 20, 2003, 12:03pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/2 "2003-01-20T12:03:35Z")

</div>

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](http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/#howto).  
Good Luck!

---

<div class="post-metadata">

**Author:** ![MannyL](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@MannyL](https://boards.straightdope.com/u/MannyL)\
**Post date:** [January 20, 2003, 5:04pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/3 "2003-01-20T17:04:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pestie](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@pestie](https://boards.straightdope.com/u/pestie)\
**Post date:** [January 20, 2003, 6:21pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/4 "2003-01-20T18:21:41Z")

</div>

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!)

---

<div class="post-metadata">

**Author:** ![MannyL](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@MannyL](https://boards.straightdope.com/u/MannyL)\
**Post date:** [January 20, 2003, 6:41pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/5 "2003-01-20T18:41:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pestie](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@pestie](https://boards.straightdope.com/u/pestie)\
**Post date:** [January 21, 2003, 3:25pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/6 "2003-01-21T15:25:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tourbot](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tourbot](https://boards.straightdope.com/u/tourbot)\
**Post date:** [January 21, 2003, 8:37pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/7 "2003-01-21T20:37:20Z")

</div>

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](http://slackware.com/config/network.php) website has sample rc.inet scripts, but they don’t show a DHCP sample.

---

<div class="post-metadata">

**Author:** ![MannyL](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@MannyL](https://boards.straightdope.com/u/MannyL)\
**Post date:** [January 22, 2003, 1:05am UTC](https://boards.straightdope.com/t/linux-network-question/149722/8 "2003-01-22T01:05:54Z")

</div>

One is Gentoo and one is Slackware

---

<div class="post-metadata">

**Author:** ![tourbot](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tourbot](https://boards.straightdope.com/u/tourbot)\
**Post date:** [January 22, 2003, 2:06am UTC](https://boards.straightdope.com/t/linux-network-question/149722/9 "2003-01-22T02:06:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MannyL](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@MannyL](https://boards.straightdope.com/u/MannyL)\
**Post date:** [January 22, 2003, 2:57am UTC](https://boards.straightdope.com/t/linux-network-question/149722/10 "2003-01-22T02:57:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tourbot](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tourbot](https://boards.straightdope.com/u/tourbot)\
**Post date:** [January 22, 2003, 2:56pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/11 "2003-01-22T14:56:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pestie](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@pestie](https://boards.straightdope.com/u/pestie)\
**Post date:** [January 22, 2003, 5:20pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/12 "2003-01-22T17:20:24Z")

</div>

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.

```auto

#!/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!
";

```

---

<div class="post-metadata">

**Author:** ![pestie](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@pestie](https://boards.straightdope.com/u/pestie)\
**Post date:** [January 22, 2003, 5:33pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/13 "2003-01-22T17:33:15Z")

</div>

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:

```auto

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

```

to this:

```auto

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. 🙂

---

<div class="post-metadata">

**Author:** ![pestie](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@pestie](https://boards.straightdope.com/u/pestie)\
**Post date:** [January 22, 2003, 5:38pm UTC](https://boards.straightdope.com/t/linux-network-question/149722/14 "2003-01-22T17:38:19Z")

</div>

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.)
