Home | Tags

Generate hostname

. Permalink. Tags: , .

Generate a random string and use that as hostname for a Macbook after checking prior existence of the serial number in a .csv and if not exist add the new device to this list.

#!/bin/bash
hostname=$(uuidgen | sed 's/[-]//g' | head -c 15;)
model=$(system_profiler SPHardwareDataType | awk '/Model Name/' | cut -d ':' -f2- | xargs)
serial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | cut -d ':' -f2- | xargs)
if [[ ! -f "cmdb.csv" ]]; then
    printf "devicename;model;sn\n" > cmdb.csv
fi
while IFS=";" read -r c1 c2 c3
do
  if [[ $c3 == $serial ]]; then
    sne=true
  fi
done < <(tail -n +2 cmdb.csv)
if [[ "$sne" == true ]]; then
  echo "$serial -eq $sne"
else
  scutil --set ComputerName $hostname
  scutil --set LocalHostName $hostname
  printf "$hostname;$model;$serial\n" >> cmdb.csv
fi

Build with Jekyll. Find me on Mastodon. There is also an RSS feed.