Skip to main content
Question

Script to set host name and static IP based on SN


Forum|alt.badge.img+2

I run a farm of 150 Mac minis. I am trying to create a first run script that will set the host name and static IP based on the SN. I am getting an end of file error when I am trying to run it. Here is a small sample. Any help would be great!

!/bin/sh

serial=/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'

if test "$serial" == "C07M802Z4E825DY3J"
then

scutil --set ComputerName "qa-mac-1"

scutil --set LocalHostName "qa-mac-1"

networksetup -setproxyautodiscovery "Ethernet" on

networksetup -setmanual Ethernet 10.1.1.1 255.255.255.128 10.1.1.129

networksetup -setdnsservers Ethernet 10.2.76.98 10.2.76.97

networksetup -setsearchdomains Ethernet mycompany.com mycompanycorp.com us.mycompany.com

else

if test "$serial" == "C07M803JDLSY3J"
then

scutil --set ComputerName "qa-mac-2"

scutil --set LocalHostName "qa-mac-2"

networksetup -setproxyautodiscovery "Ethernet" on

networksetup -setmanual Ethernet 10.1.1.2 255.255.255.128 10.1.1.129

networksetup -setdnsservers Ethernet 10.2.76.98 10.2.76.97

networksetup -setsearchdomains Ethernet mycompany.com mycompanycorp.com us.mycompany.com

if test "$serial" == "C0737951JDLSY3J"
then

scutil --set ComputerName "qa-mac-3"

scutil --set LocalHostName "qa-mac-3"

networksetup -setproxyautodiscovery "Ethernet" on

networksetup -setmanual Ethernet 10.1.1.2 255.255.255.128 10.1.1.129

networksetup -setdnsservers Ethernet 10.2.76.98 10.2.76.97

networksetup -setsearchdomains Ethernet mycompany.com mycompanycorp.com us.mycompany.com

fi

exit 0

2 replies

Forum|alt.badge.img+19
  • Honored Contributor
  • 582 replies
  • July 7, 2020

Use elif instead of else if. Otherwise you are creating a bunch if blocks, but you are not closing the if blocks with fi statements. Try writing it like this:

#!/bin/sh

if [ $serial == "<<SERIAL1>>" ]; then 
    <<code for SERIAL1>>
elif [ $serial == "<<SERIAL2>>" ]; then
    <<code for SERIAL2>>
elif [ $serial == "<<SERIAL3>>" ]; then
    .
    .
    .
fi 
exit 0

If you need to look for errors in your code, try Shellcheck. It can help identify errors in your code.


Forum|alt.badge.img+2
  • Author
  • New Contributor
  • 1 reply
  • July 7, 2020

Thanks that worked!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings