Posted on 12-15-2019 09:02 PM
Hi,
need help to write script to set hostname based on some condition
1)check its start with MBP
2)if yes exit
3)if no get asset tag from user and set it as hostname
4)if no asset tag set serial number as hostname
new to Apple/bash script, need some help to write condition statement to compere it
if anyone have doc/link for script language please share it.
flow chart for my script is attached
thanks in advance
Solved! Go to Solution.
Posted on 12-16-2019 07:50 AM
Not tested fully but this should get you what you want.
#!/bin/sh
computerName=$(hostname)
echo "$computerlName"
beginHostname=${computerName:0:3}
echo "$beginHostname"
if [ "$beginHostname" = "MBP" ];
then
echo "Computer Name is already set to: $computerName"
exit 0
else
echo "Compuer Name is not set. Prompting to enter Asset Tag"
assetTag=$(/usr/bin/osascript <<EOT
tell application "System Events"
activate
set assetTag to text returned of (display dialog "Please type in Asset Tag if known. Otherwise click cancel" default answer "" with icon 2)
end tell
EOT
)
fi
if [ "$assetTag" == "" ]; then
echo "Asset Tag unknown. Setting Computer name to Serial number"
name=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
else
echo "Setting computer name to Asset Tag"
name="$assetTag"
fi
echo $name
networksetup -setcomputername $name
scutil --set LocalHostName $name
scutil --set HostName $name
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName $name
Posted on 12-16-2019 07:50 AM
Not tested fully but this should get you what you want.
#!/bin/sh
computerName=$(hostname)
echo "$computerlName"
beginHostname=${computerName:0:3}
echo "$beginHostname"
if [ "$beginHostname" = "MBP" ];
then
echo "Computer Name is already set to: $computerName"
exit 0
else
echo "Compuer Name is not set. Prompting to enter Asset Tag"
assetTag=$(/usr/bin/osascript <<EOT
tell application "System Events"
activate
set assetTag to text returned of (display dialog "Please type in Asset Tag if known. Otherwise click cancel" default answer "" with icon 2)
end tell
EOT
)
fi
if [ "$assetTag" == "" ]; then
echo "Asset Tag unknown. Setting Computer name to Serial number"
name=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
else
echo "Setting computer name to Asset Tag"
name="$assetTag"
fi
echo $name
networksetup -setcomputername $name
scutil --set LocalHostName $name
scutil --set HostName $name
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName $name
Posted on 12-17-2019 12:28 AM
Perfect! thank you very much.