Wbm2009v2/NodeConfigurationFactory

From Hackerspace Brussels
Jump to: navigation, search

Parent: Wbm2009v2


Contents

[edit] Node Configuration Factory

[edit] Script

#!/bin/bash

ME="ssh-loop.sh"
IP="192.168.1.1"
SKIP_LINE_COUNT=11

_usage() {
  cat <<__END_OF_USAGE
Usage: $ME HOSTS COMMANDS

  HOSTS      file containing hosts list (hwaddr,param1,param2...param9))
  COMMANDS   file containing commands to be run on each host

__END_OF_USAGE
}

HOSTS=$1
COMMANDS=$2
[ -n "$HOSTS" -a -n "$COMMANDS" ] || {
  _usage
  exit 1
}
[ -f "$HOSTS" ] || {
  echo "$ME: error accessing HOSTS file '$HOSTS'"
  exit 1
}
[ -f "$COMMANDS" ] || {
  echo "$ME: error accessing COMMANDS file '$COMMANDS'"
  exit 1
}

echo "1. checking sudo..."
sudo true || exit 1

echo "2. looping over nodes..."
IFS=","
cat $HOSTS | grep -v '^#' | while read mac param1 param2 param3 param4 param5 param6 param7 param8 param9; do
  echo -n "-- mac: $mac -- " 1>&2
  sudo arp -s $IP $mac >/dev/null
  ping -c 1 -q -r -t 1 $IP >/dev/null
  if [ $? -eq 0 ]; then
    echo "alive! ---" 1>&2
    cat $COMMANDS \
	| sed	-e "s/@MAC@/$mac/g" \
		-e "s/@PARAM1@/$param1/g" \
		-e "s/@PARAM2@/$param2/g" \
		-e "s/@PARAM3@/$param3/g" \
		-e "s/@PARAM4@/$param4/g" \
		-e "s/@PARAM5@/$param5/g" \
		-e "s/@PARAM6@/$param6/g" \
		-e "s/@PARAM7@/$param7/g" \
		-e "s/@PARAM8@/$param8/g" \
		-e "s/@PARAM9@/$param9/g" \
	| ssh -T -q -o "UserKnownHostsFile /dev/null" -o "StrictHostKeyChecking no" root@"$IP" 2>&1 \
	| tail -n +$SKIP_LINE_COUNT
  else
    echo "not found! ---" 1>&2
  fi
  sudo arp -d $IP >/dev/null
done
echo "3. done!"

The script runs a command snippet on a series of devices plugged on a switch via their LAN interface with default LAN IP address (192.168.1.1/24).

The input hosts file format is:

MAC,PARAM1,PARAM2,PARAM3,PARAM4,PARAM5,PARAM6,PARAM7,PARAM8,PARAM9

The values of PARAM1...PARAM9 are substituted in the code snippet (see below) before it is sent to each device.

[edit] Parameters

  • PARAM1: hostname
  • PARAM2: wireless IP address

[edit] Snippets

[edit] Setting the hostname

set -a

NAME="@PARAM1@"

sysctl -w kernel.hostname=$NAME

uci set system.@system[0].hostname=$NAME

uci commit

[edit] Configuring the wireless interface

set -a

IP="@PARAM2@"

uci set wireless.wifi0.channel=5
uci set wireless.wifi0.disabled=0
uci set wireless.wifi0.hwmode=11g

uci set wireless.@wifi-iface[0].network=wlan
uci set wireless.@wifi-iface[0].mode=adhoc
uci set wireless.@wifi-iface[0].ssid=WBM2009v2-Test0
uci set wireless.@wifi-iface[0].encryption=none
uci set wireless.@wifi-iface[0].bssid=ca:ca:fe:ca:ca:00
uci set wireless.@wifi-iface[0].rate=54M
uci set wireless.@wifi-iface[0].bgscan=0

uci set network.wlan=interface
uci set network.wlan.proto=static
uci set network.wlan.ipaddr=$IP
uci set network.wlan.netmask=255.255.255.0

uci commit

[edit] Disabling dnsmasq, firewall & httpd

set -a

/etc/init.d/dnsmasq stop
/etc/init.d/dnsmasq disable

/etc/init.d/firewall stop
/etc/init.d/firewall disable

/etc/init.d/httpd stop
/etc/init.d/httpd disable

[edit] Start/Stop scripts

here's a good read on how to install scripts to start on boot

https://forum.openwrt.org/viewtopic.php?id=11301


[edit] Enable wireless interface

 uci set wireless.wifi0.disabled=0
 uci commit wireless && wifi


[edit] Installing on openwrt

you can install the nodeconfigfactory on an openwrt machine, with some minor changes

[edit] needed packages

install following packages using the opkg tool

  • net-tools (provides a decent arp, allows to set mac adresses in the arp table, which the default implementation does not provide (basically that is a "cat /proc/net/arp")
  • openssh-client (as the dropbear ssh is not willing to cooperate :-)

! be sure your private key has the right access priv (only readable by your user)

! i needed to --force-overwrite some executables already installed by dropbear & busybox, i don't know if this is the best option, but it does the job...

[edit] needed changes in script

as openwrt doesn't know the sudo command, i did some (trivial) adaptations to the script -- run it as root user (mainly because 'arp -s' needs root privileges)