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.
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.
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.
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