Naming convention script

OK, so a quick background:

I am a sys admin for a 1:1 deployment in academia with Macbooks, totaling around 6,000. Macbooks get shifted around from building to building and go to and from the repair center if hardware repair is needed. Often, some machines will get moved from one building to another and not be renamed. We have a standard naming convention that goes like this:

AAA_Asset#

Where AAA is the building abbreviation and the asset# is the actual asset tag of the machine, separated by an underscore.

Now, my network admin has done his job and chopped up every building into specific IPv4 VLANs. So, I know what subnets belong to what buildings. This is good.

So, if AAA_Asset# moved from it's current building to a building where BBB_asset# is the standard naming convention I would like to write a script that renames the machine to the proper naming convention.

I can determine what building it is by the second number of the IP address. So, 10.20.30.40 would tell me that the machine is in the 20 subnet and that belongs to building X.

I'd like to set up a shell script that parses the computer name and the second digit of the IP address and puts them into arguments and then depending on the IP address it will rename the first three initials of the computer name to the proper name reflecting what building it is in.

I know I can get the computer name and the IP by using the networksetup binary, but I am not quite sure how to pass all that info into a set of arguments that determines the proper convention and then applies it.

Hi, quick and dirty to get you started, output is:

sh money 10.192.0.0
IP is 10.192.0.0
the second octet is 192

code:

#!/bin/bash

IP=$1;
OCT2=$(echo $1 |awk -F . '{print $2}')

echo "IP is $IP"

echo "the second octet is $OCT2"

You can then build logic (such as a case statement) and take action based upon what is reported for the second number as you put it, or just use a few few if statements if there is not that many different choices.

Thank you I will start writing the script out tomorrow or as soon as time permits and come back here for help. Thanks again.

Anytime.

OK I think I go a working concept of what I may want to use

#!/bin/bash

# get the current IP address

IP=`networksetup -getinfo Ethernet | grep -v "IPv6" | awk '/IP address:/ { print $3 }'`

case $IP in

    10.20.* ) #subnet 20

                echo "Unit is in subnet 20, should be building A"

                echo "A" 1>&2 
               ;;

     10.30.* ) #subnet 30

                echo "Unit is in subnet 30, should be building B"

                 echo "B" 1>&2
  
                ;;
esac

So I can out put my desired results to a variable, be it A, B, C and so forth and then later call them and rename the computer accordingly?

Sorry, I am a novice to moderate scripter and still learning.

Thanks

I don't have apple machine, so I have to guess, that below command provides the IP address.

IP=`networksetup -getinfo Ethernet | grep -v "IPv6" | awk '/IP address:/ { print $3 }'`

Finially you export the build abbreviation to variable $building with above script.

So what's the command to show hostname?

For example, you get hostname with another variable,

hostname=`hostname`

then you can get new hostname by below command:

newhost="${building}_${hostname##*_}"

then you can set new hostname with variable $newhost

to get the computer name you use the same command, networksetup.

networksetup -getcomputername will get the name, the hostname command also works. In fact there are like 5 to 7 different ways to get the computer name from the command line.

I am just not sure how to take those variables and then plug them into arguments that match naming conventions.

Thanks

Sounds like you have a hash of:

hostname IP

parse out the hostname first, then run a loop for each one and then take action using the case statement.

Example:

for server_name in $hosts (where host is the hostnames)
do
....run

done

Sounds like this is vendor specific stuff so may be an existing way already.

Good luck.

So maybe, I should use the case statement as say, an argument and then call that argument later on? Then set, A=AAA B=BBB and so on, so it could eventually do

networksetup -setcomputername $A_asset#