Tuesday, August 11, 2020

#1 2020-07-06 02:21:33 pm

rdagel@gmail.com
Member
Registered: 2020-07-03
Posts: 1

Script to set computer name and IP based on serial number

I have a group of 400 Macs I take care of and I am trying to automate the naming and setting up the static ip based on the SN of the system with a first run script.  Here is a small sample of the script but it is giving me an end of file error when I try and run it.  Any ideas?

#!/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

Offline

 

#2 2020-07-06 09:52:48 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 957

Re: Script to set computer name and IP based on serial number

Hi. The most glaring issue is that the code sample is written for the Terminal. AppleScript shell calls use the syntax 'do shell script ' and then the command(s). To escape a special character—such as parentheses—you would use double backslashes, but the Awk portion can be simplified instead. With 400 items, you really need to iterate a list, rather than nest that many if considerations in the editor.


Applescript:

set serialList to {"C07M802Z4E825DY3J", "C07M803JDLSY3J"} --populate in order with the respective serial numbers
set serial to do shell script "system_profiler SPHardwareDataType | Awk '/Serial Number/ {print $NF}' "
repeat with counter from 1 to 2 --400, once the above list is populated
   tell serial to if it = serialList's item counter then
       do shell script ("scutil --set ComputerName " & ("qa-mac-" & counter)'s quoted form & " ; scutil --set LocalHostName " & ("qa-mac-" & counter)'s quoted form & " ; 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 ")
       exit repeat
   end if
end repeat

--edited to add some necessary spaces around a few semicolons

Last edited by Marc Anthony (2020-07-07 06:30:04 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)