Script de configuration réseau itinérant.

From Tuxunix
Jump to: navigation, search
   1.#! /bin/bash
   2.       
   3.#
   4.#@Name confignet.sh
   5.#@Authors Tuxedo
   6.#@Date 10/09/2006
   7.#@note Configuration automatique du réseau
   8.#
   9.#
  10.       
  11.#
  12.#Init vars
  13.#
  14.init_vars()
  15.{
  16.#DIR
  17.dir_interfaces=/etc/network/interfaces
  18.dir_interfaces_old=/etc/network/interfaces.old
  19.dir_resolv=/etc/resolv.conf
  20.dir_resolv_old=/etc/resolv.conf.old
  21.}
  22.       
  23.config()
  24.{
  25.#save file orig
  26.echo "Save file orig..."
  27.mv $dir_interfaces $dir_interfaces_old
  28.mv $dir_resolv $dir_resolv_old
  29.       
  30.#implement in file interfaces
  31.echo '
  32.# This file describes the network interfaces available on your system
  33.# and how to activate them. For more information, see interfaces(5).
  34.      
  35.# The loopback network interface
  36.auto lo
  37.iface lo inet loopback ' > $dir_interfaces
  38.       
  39.#config interface eth or rausb
  40.if [ $face == eth ]
  41.then
  42.echo '
  43.The primary network interface
  44.allow-hotplug eth0
  45.iface eth0 inet static
  46.address '$ip'
  47.netmask '$mask'
  48.gateway '$pass'
  49.auto eth0
  50.' >> $dir_interfaces
  51.       
  52.fi
  53.       
  54.if [ $face == wifi ]
  55.then
  56.       
  57.echo '
  58.allow-hotplug rausb0
  59.iface rausb0 inet static
  60.address '$ip'
  61.netmask '$mask'
  62.gateway '$pass'
  63.wireless-essid '$ssid'
  64.#wireless-mode managed
  65.wireless-key ''$key''
  66.wireless-channel '$chan'
  67.auto rausb0 ' >> $dir_interfaces
  68.       
  69.fi
  70.       
  71.       
  72.#implement in file resolv
  73.echo '
  74.#DNS resolving
  75.nameserver '$dns'
  76.nameserver '$dns2' ' > $dir_resolv
  77.       
  78.echo "Config Network finish done!"
  79.}
  80.config_mano()
  81.{
  82.#choose interface
  83.if [ $face == wifi ]
  84.then
  85.confirm=""
  86.ip=""
  87.mask=""
  88.pass=""
  89.dns=""
  90.dns2=""
  91.ssid=""
  92.key=""
  93.chan=""
  94.while [ -z $ip ]
  95.do
  96.    echo "Saisir address IP :"
  97.    read ip
  98.    echo "Saisir netmask :"
  99.    read mask
 100.    echo "Saisir gateway :"
 101.    read pass
 102.    echo "Saisir DNS :"
 103.    read dns
 104.    echo "Saisir DNS 2 :"
 105.    read dns2
 106.    echo "-- Config wireless --"
 107.    echo "Saisir essid :"
 108.    read ssid
 109.    echo "Saisir key :"
 110.    read key
 111.    echo "Saisir channel :"
 112.    read chan
 113.    echo "------------------------"
 114.    echo "Votre config est : "
 115.    echo "IP : $ip"
 116.    echo "Mask : $mask"
 117.    echo "Gateway : $pass"
 118.    echo "DNS : $dns"
 119.    echo "DNS 2 : $dns2"
 120.    echo "ESSID : $ssid"
 121.    echo "KEY WEP : $key"
 122.    echo "Channel : $chan"
 123.    echo "Confirmer ?(yes/no)"
 124.    read confirm
 125.    case $confirm in
 126.        y|yes)
 127.            config
 128.        ;;
 129.        n|no)
 130.            ip=""
 131.        ;;
 132.        *)
 133.            ip=""
 134.    esac
 135.done
 136.elif [ $face == eth ]
 137.then
 138.    confirm=""
 139.    ip=""
 140.    mask=""
 141.    pass=""
 142.    dns=""
 143.    dns2=""
 144.    while [ -z $ip ]
 145.    do
 146.        echo "Saisir address IP :"
 147.        read ip
 148.        echo "Saisir netmask :"
 149.        read mask
 150.        echo "Saisir gateway :"
 151.        read pass
 152.        echo "Saisir DNS :"
 153.        read dns
 154.        echo "Saisir DNS 2 :"
 155.        read dns2
 156.        echo "Votre config est : "
 157.        echo "IP : $ip"
 158.        echo "Mask : $mask"
 159.        echo "Gateway : $pass"
 160.        echo "DNS : $dns"
 161.        echo "DNS 2 : $dns2"
 162.        echo "Confirmer?(yes/no)"
 163.        read confirm
 164.        case $confirm in
 165.            y|yes)
 166.                config
 167.            ;;
 168.            n|no)
 169.                ip=""
 170.            ;;
 171.            *)
 172.                ip=""
 173.            ;;
 174.       esac
 175. done
 176.fi
 177.}
 178.       
 179.start_service()
 180.{
 181.     
 182.#stop service network
 183./etc/init.d/networking stop
 184.      
 185.#start service network
 186./etc/init.d/networking start
 187.       
 188.}
 189.       
 190.info_conf()
 191.{
 192.    REP=""
 193.    while [ -z $REP ]
 194.    do
 195.        echo "|-------------------------------|"
 196.        echo "| Menu |"
 197.        echo "|-------------------------------|"
 198.        echo "| 0 - Quit |"
 199.        echo "| 10 - Stop/Start service net |"
 200.        echo "| 1 - Wifi sable |"
 201.        echo "| 2 - Wifi paris |"
 202.        echo "| 3 - Wifi patou |"
 203.        echo "| 4 - Wifi config manuel |"
 204.        echo "| 5 - eth0 sable |"
 205.        echo "| 6 - eth0 config manuel |"
 206.        echo "|-------------------------------|"
 207.        echo "Choose config network :"
 208.        read REP
 209.        case "$REP" in
 210.            0)
 211.              exit 0;
 212.            ;;
 213.            1)
 214.              ip=192.168.x.x
 215.              mask=255.255.255.0
 216.              pass=192.168.x.x
 217.              dns=192.168.x.x
 218.              dns2=192.168.x.x
 219.              face=wifi
 220.              ssid=HOMESAB
 221.              key='s:xxxxxxxxx'
 222.              chan=10
 223.              config
 224.              REP=""
 225.            ;;
 226.            2)
 227.                ip=192.168.x.x
 228.                mask=255.255.255.0
 229.                pass=192.168.x.x
 230.                dns=212.27.54.252
 231.                dns2=212.27.53.252
 232.                face=wifi
 233.                ssid=gnulinux
 234.                key='xxxxxxxxxx'
 235.                chan=3
 236.                config
 237.                REP=""
 238.            ;;
 239.            3)
 240.                ip=192.168.x.x
 241.                mask=255.255.255.0
 242.                pass=192.168.x.x
 243.                dns=192.168.x.x
 244.                dns2=192.168.x.x
 245.                face=wifi
 246.                ssid=Alice-f9d5
 247.                key=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
 248.                chan=7
 249.                config
 250.                REP=""
 251.            ;;
 252.            4)
 253.                face=wifi
 254.                config_mano
 255.                REP=""
 256.            ;;
 257.            5)
 258.                face=eth
 259.                ip=192.168.x.x
 260.                mask=255.255.255.0
 261.                pass=192.168.x.x
 262.                dns=192.168.x.x
 263.                dns2=192.168.x.x
 264.                config
 265.                REP=""
 266.           ;;
 267.           6)
 268.                face=eth
 269.                config_mano
 270.                REP=""
 271.           ;;
 272.           10)
 273.                start_service
 274.                REP=""
 275.           ;;
 276.           *)
 277.                REP=""
 278.           ;;
 279.    esac
 280.done
 281.       
 282.}
 283.       
 284.#
 285.#Main
 286.#
 287.init_vars
 288.info_conf