#!/bin/bash
## name: linklocal.sh
## see http://forum.ubuntuusers.de/topic/pc-direktverbindung-zwischen-linux-ubuntu-und-/#post-5553487
## Instant Link Local Modus
## elektronenblitz63 ubuntuusers.de 2013
## published under GPL v3
## Version 0.0.2 / 27. April 2013

## LAN statisch 
laniface=eth0
laddress=192.168.3.1
lbroadcast=192.168.3.255
lnetmask=255.255.255.0
lgateway=192.168.3.1

# Basisadresse DHCP-Adresspool (WLAN-IP + X)
# Adresspool / Anzahl Adressen / hier nur eine
# IP-Adresse des Client ist hier 192.168.3.2
lanbaseip=1
ipaddresses=1

# Lease-Time
leasetime=infinite

## Pause in Sekunden für Konfigurationsparameter
## Vorgabewert 2
configdelay=2

#### Ende freie Variablen
##

## be root if not
test `id -u` -eq 0 || exec sudo "$0" "$@"

## Prüfung der Konfiguration
if [ "$1" != "-stop" ]; then

##  prüfe Basiskonfiguration Schnittstellen 
 check_laniface="`ifconfig $laniface | grep Link | awk {'print $1'}`"

if [ "$check_laniface" = $laniface ]; then
echo "LAN-Interface" $laniface "gefunden. Setze Konfiguration fort."

else
 echo "LAN-Interface" $laniface "nicht gefunden! Breche Konfiguration ab."
  exit 0
fi
 fi

echo "Link Local Netzwerkkonfiguration"
 echo "Starte gewählte Konfiguration ..."
  sleep $configdelay

## aut. Adressberechnung DHCP-Range für dnsmasq-base
## gemäß Vorgabe LAN-Schnittstelle

ipaddresses=$[$ipaddresses+$lanbaseip]
 baseendaddr="`echo $laddress | tr -s . " " | awk {'print $4'}`"
  basestartaddr="`echo $laddress | tr -s . " " | awk {'print $1,$2,$3'} | tr -s " " .`"
   endaddr="$basestartaddr""."$[$startaddr+$ipaddresses]
    startaddr="$basestartaddr""."$[$baseendaddr+$lanbaseip]
     ipaddresses=$[$ipaddresses-1]

if [ "$1" != "-start" ]; then
echo "Stoppe alle Dienste und beende die Verbindungen ..."

defgw="`route -n | grep UG | awk {'print $2'}`"
 /sbin/route del default gw $defgw $laniface
  echo '' | tee /etc/resolv.conf
 
   /sbin/ifconfig $laniface down
     /usr/bin/killall dnsmasq
      sleep $configdelay
fi

## Restart Network-Manager - beende Netzwerkkonfiguration
if [ "$1" = "-stop" ]; then
 echo
  echo "reaktiviere Network-Manager."
   service network-manager start
    sleep $configdelay
     service network-manager restart

echo "Konfiguration beendet."
 exit
  fi

# Konfiguration LAN
echo "Beende Network-Manager"
 service network-manager stop
  echo -e "Starte benötigte Dienste, und Verbindungen ...\n"
   sleep $configdelay

sleep $configdelay

  echo "Starte statische LAN-Konfiguration ..."
   /sbin/ifconfig $laniface down
    /sbin/ifconfig $laniface $laddress broadcast $lbroadcast netmask $lnetmask
     sleep $configdelay
      /sbin/ifconfig $laniface up

## vorhandene Route ggf. löschen
 echo "Lösche Default-Gateway ..."
  defgw="`route -n | grep UG | awk {'print $2'}`"
   /sbin/route del default gw $defgw $laniface

echo
 echo "Setze Gateway und Route ..."
  /sbin/route add default gw $lgateway $laniface
   sleep $configdelay

## dnsmasq starten
echo "Starte dnsmasq"
 echo -e "DHCP-Range dnsmasq-base - Startadresse: $startaddr Endadresse: $endaddr\n"

 /usr/sbin/dnsmasq -i $laniface -F $startaddr,$endaddr,$leasetime

## Ausgabe der Konfiguration. Einstellungen der Schnittstellen,
## Routingtabelle und DNS prüfen

echo "Aktuelle Konfiguration ..."

 echo -e "Systemkonfiguration: /etc/resolv.conf\n"
  /bin/cat /etc/resolv.conf

 echo -e "Systemkonfiguration: Routingtabelle\n"
  /sbin/route -n

 echo "Konfiguration LAN:"
  /sbin/ifconfig $laniface | egrep 'Link|inet Adresse'

echo -e "Basiskonfiguration abgeschlossen.\n"
 echo "Netzwerkkabel an Windows-Rechner anschließen."
  exit 0
