1. #!/bin/bash
  2.  
  3. # Author: Ryan Lewis
  4. #         God
  5. # Date: Wed Mar 01, 2006
  6. # hostname-change: looks up the mac address for a computer and
  7. #                  changes the hostname appropreately.
  8.  
  9. LOCALMAC=`grep eth0 /var/log/dmesg | grep Ethernet | awk -F' ' '{print $10}'`
  10. COSI1="00:11:25:f6:70:7a"
  11. COSI2="00:11:25:f6:5d:7c"
  12. COSI3="00:11:25:f6:5b:2e"
  13. COSI4="00:11:25:f6:5d:c9"
  14. COSI5="00:11:25:f6:15:22"
  15. COSI6="00:11:25:f6:5c:13"
  16. COSI7="00:11:25:f6:52:95"
  17. COSI8="00:11:25:f6:67:9c"
  18. COSI9="00:11:25:f6:70:8c"
  19. COSI10="00:11:25:f6:2f:c1"
  20. NUM="0"
  21.  
  22. function create_networking_file {
  23.          echo "NETWORKING=yes" > /etc/sysconfig/network
  24.          echo "HOSTNAME=cosi-lab-$NUM" >> /etc/sysconfig/network       
  25. }
  26. function update_networking_scripts {
  27.         rm /etc/sysconfig/network-scripts/ifcfg-eth0
  28.         echo "DEVICE=eth0" > /etc/sysconfig/network-scripts/ifcfg-eth0
  29.         echo "BOOTPROTO=dhcp" >> /etc/sysconfig/network-scripts/ifcfg-eth0
  30.         echo "HWADDR=$LOCALMAC" >> /etc/sysconfig/network-scripts/ifcfg-eth0
  31.         echo "DHCP_HOSTNAME=cosi-lab-$NUM" >> /etc/sysconfig/network-scripts/ifcfg-eth0
  32.         echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
  33. }
  34. function update_etc_hosts {
  35.         echo "127.0.0.1 localhost.localdomain localhost" > /etc/hosts
  36.         echo "`/sbin/ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'` `hostname`" >> /etc/hosts
  37. }
  38. function dhclient_update {
  39.         echo "send host-name \"cosi-lab-$NUM\";" > /etc/dhclient-eth0.conf
  40. }
  41. if [ $COSI1 == $LOCALMAC ]; then
  42.    echo "cosi-lab-1 detected"
  43.         NUM="1"
  44. fi
  45. if [ $COSI2 == $LOCALMAC ]; then
  46.    echo "cosi-lab-2 detected"
  47.          NUM="2"
  48. fi
  49. if [ $COSI3 == $LOCALMAC ]; then
  50.    echo "cosi-lab-3 detected"
  51.         NUM="3"
  52. fi
  53. if [ $COSI4 == $LOCALMAC ]; then
  54.    echo "co-lab-4 detected"
  55.         NUM="4"
  56. fi
  57. if [ $COSI5 == $LOCALMAC ]; then
  58.    echo "cosi-lab-5 detected"
  59.         NUM="5"
  60. fi
  61. if [ $COSI6 == $LOCALMAC ]; then
  62.    echo "cosi-lab-6 detected"
  63.         NUM="6"
  64. fi
  65. if [ $COSI7 == $LOCALMAC ]; then
  66.    echo "cosi-lab-7 detected"
  67.         NUM="7"
  68. fi
  69. if [ $COSI8 == $LOCALMAC ]; then
  70.    echo "cosi-lab-8 detected"
  71.         NUM="8"
  72. fi
  73. if [ $COSI9 == $LOCALMAC ]; then
  74.    echo "cosi-lab-9 detected"
  75.         NUM="9"
  76. fi
  77. if [ $COSI10 == $LOCALMAC ]; then
  78.    echo "cosi-lab-10 detected"
  79.         NUM="10"
  80. fi
  81.  
  82. create_networking_file
  83. update_networking_scripts
  84. dhclient_update
  85. update_etc_hosts
  86.  
  87. /etc/init.d/network restart