LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Blogs > Debian, This That and the Other
User Name
Password

Notices

Over several years of using Linux distros (Debian happens to be my fave) and BSDs for my primary computing, I've picked up the odd piece of useful info.
Rate this Entry

Network Scripting, Debian style.

Posted 09-29-2009 at 07:27 PM by ofaring
Updated 01-02-2010 at 02:17 AM by ofaring

Everybody has a different idea of how to get connected. More and more people only look for automagic network connections or fancy GUIs to accomplish their bidding, but that's not me. So, in the interests of old hardware and always knowing exactly where I'm getting a connection, here's my methodology.

This involves modifying /etc/network/interfaces and the use of some simple bash scripting. Pertinent info on the networking process can be found in the Debian Reference Manual, chapter 5, and I recommend you read the entire chapter. But, for the impatient here is the exact link:

My (partial) /etc/network/interfaces setup:

Code:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

## Tricky Holly ##
iface TH inet dhcp
 wpa-ssid PLATO
 wpa-psk your_passkey

## Espresso Post ##
iface EP inet dhcp
 wpa-ssid penquino
 wpa-psk your_passkey

## Open Connection ##
iface WiFi inet dhcp
And now, the fun part: wee bash scripts. For the purposes of my setup, I've done individual scripts. There are numerous other ways to do such things, and my method primarily works because I'm the sole user of my laptop. Anyone else would get confused and have to resort to other means. The scripts follow the same framework with the only changes necessary being the network card and interface names.

Code:
#!/bin/bash
# This script is part of my custom /e/n/i.
# It connects my laptop to the net at
# Tricky Holly's.
#
OPTIONS="netUP netDOWN Quit"
select opt in $OPTIONS; do
 if [ "$opt" = "netUP" ]; then
	sudo ifup ath0=TH
  exit
 elif [ "$opt" = "netDOWN" ]; then
	sudo ifdown ath0=TH
  exit
 elif [ "$opt" = "Quit" ]; then
  exit
 else
	echo 'Try again, sucka.'
 fi
done
As root, I created /opt/sbin. My scripts are named according to location, so, Tricky Holly became "TH.sh", which I then placed in /opt/sbin. On Debian, you will most likely have to create this directory yourself.
Code:
# mkdir /opt/sbin
# cp -v <your location>/TH.sh /opt/sbin/
"But," you say, "Do I have to type as root
Code:
# /opt/sbin/TH.sh
EVERY time I want to use it?" You, the enlightened Linux user know the answer to that. Make each script executable (Shall we say
Code:
# chmod 777
so that your regular user can execute it along with the usage of sudo.) If you had to create /opt/sbin you will also have to add it to your path. You can do a quick check with
Code:
% echo $PATH
Not there? Here's one way of making the addition:

Code:
echo 'PATH="${PATH}:/opt/bin:/opt/sbin"; export PATH' >> ~/.zshrc
Because obviously you are using ZSH. Failing that, .bashrc, or whatever the shell you happen to use. (In my case, I'm also using /opt/bin for other scripts and programs.)

Now, about the use of "sudo" in the script. This will not interfere with your root account being able to use the script, but it does require some extra setup. (I will be posting another entry immediately after this one with more details.) Once you have properly configured /etc/sudoers we are good to type as our regular user:
Code:
% TH.sh
which will give us the options in our script.

I've made an individual script for each physical location, and because I frequently change location, the simple naming scheme works for me. Yes, I could and have used WPA Supplicant with the accompanying /etc/wpa_supplicant.conf, but that still defeats my purpose: messing with bash scripting and having fun with Debian.

A final word of caution. I don't recommend that you mix and match personal network management with automatic methods, at least not without backing up your work. GUIs like Wicd and GNOME's NetworkManager will change /e/n/interfaces to match their requirements. See here:

I hope this helps you enjoy your system's flexibility.
Posted in Examples
Views 1412 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 08:43 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration