#!/bin/bash # Company: PowerCraft Technology # Author: Copyright Jelle de Jong # Note: Please send me an email if you enhanced the script # Version: 0.0.2 # Date: 2009-01-08 / 2009-01-14 / 2009-01-22 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more de/usr/bin/tails. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # apt-get install bridge-utils version="0.0.2" author="Jelle de Jong " donation="http://www.tuxcrafter.net/pages/contact.html#paypal-donation" DHCP=0 STATIC=0 BRIDGE=0 network_manager=0 reset=0 SYSCONF="/etc/pct-wired-setup.conf" SYSDIR="/etc/pct-wired-setup.conf.d/" unset DEVICE unset ADDRESS unset NETMASK unset GATEWAY unset DEV0 unset DEV1 function version() { echo "Usage: $0 --help" echo "Version: $version" echo "Author: $author" echo "Donation: $donation" } function usage() { echo "program to configure wired networks with system width configuration" echo echo "Usage: $0 [options]" echo echo "Options:" echo " -h, --help, -? show this help information" echo " -d, --dhcp create a simple auto dhcp wired network" echo " -i, --device provide network device/interface to use" echo " -s, --static create a static network configuration" echo " -a, --address address to use for static configuration" echo " -n, --netmask netmask to use for static configuration" echo " -g, --gateway gateway to use for static configuration" echo " -b, --bridge create a network bridge between dev0 and dev1" echo " -0, --dev0 dev0 device to use for bridge configuration" echo " -1, --dev1 dev1 device to use for bridge configuration" echo " -c, --config name of id string used in configuration file" echo " -m, --network-manager configure system for network-manager to take control" echo " -r, --reset removes 70-persistent-net.rules file (reboot needed)" echo echo "Examples: [tested]" echo " pct-wired-setup --dhcp --device eth0" echo " pct-wired-setup -d -i eth0" echo " pct-wired-setup --static --device eth0 --address 192.168.1.180 --netmask 255.255.255.0 --gateway 192.168.1.1" echo " pct-wired-setup -s -i eth0 -a 192.168.1.180 -n 255.255.255.0 -g 192.168.1.1" echo " pct-wired-setup --bridge --dev0 eth0 --dev1 eth1" echo " pct-wired-setup -b -0 eth0 -1 eth1" echo echo " pct-wired-setup --static --config config0" echo " pct-wired-setup --static --config eth1" echo " pct-wired-setup --dhcp" echo echo " ifconfig -a" echo " bwm-ng" echo echo "Configuration:" echo " $SYSCONF" echo " $SYSDIR" echo echo " In the configuration files the default device, address, netmask" echo " and gateway can be set, so only options as --static, --dhcp or" echo " --bridge are needed to switch between network profiles. With the" echo " --config argument a configuration can be selected, for example" echo " --static --config config0 will use the settings in the section" echo " [static-config0]. Look inside the configuration files to get a" echo " better idea of how to use the configuration files." echo echo " This program is a helper program to efficient setup common" echo " wired network configurations. You can debug this script by" echo " running it as bash -x pct-wired-setup [options]. The script" echo " is a setup wrapper with a profile system to setup the standard" echo " /etc/network/interfaces file." echo echo "Description: [package]" echo " This package contains a script and configuration system to easy and" echo " efficient configure the /etc/network/interfaces file with configurations" echo " like dhcp, static or bridged network setup. It has a configuration file" echo " structure where different setups can be defined, this makes it easy" echo " to switching between network configuration. The script is designed to" echo " setup and maintain simple network configurations for all network clients." echo " The package is maintained by PowerCraft Technology. Take a look at the" echo " help argument for all the useful and simple options of this tool." echo echo "Website:" echo " https://secure.powercraft.nl/svn/packages/" echo "Mailinglist:" echo " http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-commit" echo " http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-devel" } while [[ "$1" == -* ]]; do case "$1" in -h|--help|-\?) usage; exit 0;; -d|--dhcp) DHCP=1; shift;; -i|--device) DEVICE="$2"; shift; shift;; -s|--static) STATIC=1; shift;; -a|--address) ADDRESS="$2"; shift; shift;; -n|--netmask) NETMASK="$2"; shift; shift;; -g|--gateway) GATEWAY="$2"; shift; shift;; -b|--bridge) BRIDGE=1; shift;; -0|--dev0) DEV0="$2"; shift; shift;; -1|--dev1) DEV1="$2"; shift; shift;; -c|--config) CONFIG="$2"; shift; shift;; -m|--network-manager) network_manager=1; shift;; -r|--reset) reset=1; shift;; -v|--version) version; exit 0;; --) shift; break;; -*) echo "invalid option: $1"; usage; exit 1;; esac done if [ $(id -u) != 0 ]; then echo "please run this script with superuser permissions" echo "example: sudo $0 [options]" exit 1 fi function scan_configuration() { file="$1" unset options [ ! -z "$DEVICE" ] && options+=("DEVICE") [ ! -z "$ADDRESS" ] && options+=("ADDRESS") [ ! -z "$NETMASK" ] && options+=("NETMASK") [ ! -z "$GATEWAY" ] && options+=("GATEWAY") [ ! -z "$DEV0" ] && options+=("DEV0") [ ! -z "$DEV1" ] && options+=("DEV1") for value in "${options[@]}" do sed -n -i -e "/$value/!p" "$file" done } function get_configuration() { section="$1" if [ ! -z "$section" ] && [ ! -z "$CONFIG" ]; then section+="-$CONFIG" elif [ -z "$section" ] && [ ! -z "$CONFIG" ]; then section="$CONFIG" fi [ ! -d "$SYSDIR" ] && mkdir --parents --verbose "$SYSDIR" tempfile=$(mktemp -p "/tmp/" tempfile.XXXXXXXXXX) trap "rm -f $tempfile" 0 1 2 3 15 [ -z "$tempfile" ] && exit 1 files="$SYSCONF" files+=$'\n' files+=$(run-parts --list "$SYSDIR" ) if [ -n "$files" ]; then while read file; do set +e [ ! -r "$file" ] && continue sed -n '/^\['"$section"'\]$/,/^\[.*\]$/{//!p}' "$file" > "$tempfile" scan_configuration "$tempfile" . "$tempfile" set -e done < <(echo "$files") fi } function bridge() { echo "pct-wired-setup: bridge" get_configuration bridge if [ -z "$DEV0" ]; then echo "dev0 not set, try adding --dev0 eth0, closing now" exit 1 fi if [ -z "$DEV1" ]; then echo "dev1 not set, try adding --dev1 eth1, closing now" exit 1 fi # todo: change to a system width config in the interfaces file ifconfig br0 down brctl delbr br0 brctl show killall dhclient export DEV0=${DEV0} export DEV1=${DEV1} ifconfig ${DEV0} down ifconfig ${DEV1} down ifconfig ${DEV0} 0.0.0.0 ifconfig ${DEV1} 0.0.0.0 brctl addbr br0 brctl addif br0 ${DEV0} brctl addif br0 ${DEV1} ifconfig br0 up brctl show dhclient br0 } function static() { echo "pct-wired-setup: static: configure interfaces file" get_configuration static if [ -z "$DEVICE" ]; then echo "device not set, try adding --device eth0, closing now" exit 1 fi if [ -z "$ADDRESS" ]; then echo "address not set, try adding --address 192.168.1.180, closing now" exit 1 fi if [ -z "$NETMASK" ]; then echo "netmask not set, try adding --netmask 255.255.255.0, closing now" exit 1 fi if [ -z "$GATEWAY" ]; then echo "gateway not set, try adding --gateway 192.168.1.1, closing now" exit 1 fi /etc/init.d/networking stop && sleep 1 [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto "$DEVICE" | tee -a /etc/network/interfaces echo iface "$DEVICE" inet static | tee -a /etc/network/interfaces echo address "$ADDRESS" | tee -a /etc/network/interfaces echo netmask "$NETMASK" | tee -a /etc/network/interfaces echo gateway "$GATEWAY" | tee -a /etc/network/interfaces #~ echo dns-nameservers 208.67.222.222 | tee -a /etc/network/interfaces #~ echo dns-nameservers 208.67.220.220 | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces /etc/init.d/networking start } function dhcp() { echo "pct-wired-setup: dhcp: configure interfaces file" get_configuration dhcp if [ -z $DEVICE ]; then echo "device not set, try adding --device eth0, closing now" exit 1 fi /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto $DEVICE | tee -a /etc/network/interfaces echo iface $DEVICE inet dhcp | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces /etc/init.d/networking start } function network_manager() { echo "pct-wired-setup: network-manager: configure interfaces file" /etc/init.d/networking stop [ -e /etc/network/interfaces ] && mv --verbose /etc/network/interfaces /etc/network/interfaces.backup touch /etc/network/interfaces echo | tee -a /etc/network/interfaces echo auto lo | tee -a /etc/network/interfaces echo iface lo inet loopback | tee -a /etc/network/interfaces echo | tee -a /etc/network/interfaces rm --verbose /etc/wpa_supplicant.conf /etc/init.d/networking restart } function reset() { echo "pct-wired-setup: reset: remove 70-persistent-net.rules" file="/etc/udev/rules.d/70-persistent-net.rules" rm --verbose --interactive "$file" [ -e "$file" ] || echo "please reboot your system, or replug your network devices" } if [ ! -z $CONFIG ]; then get_configuration; fi if [ $DHCP = 1 ]; then dhcp; elif [ $STATIC = 1 ]; then static; elif [ $BRIDGE = 1 ]; then bridge; elif [ $network_manager = 1 ]; then network_manager; elif [ $reset = 1 ]; then reset; else echo "pct-wired-setup: configuration variables not found, exiting now" fi exit