#!/usr/bin/perl -w ################################################################################ # Copyright (c) 2011 University of Utah Student Computing Labs. # All Rights Reserved. # # Permission to use, copy, modify, and distribute this software and # its documentation for any purpose and without fee is hereby granted, # provided that the above copyright notice appears in all copies and # that both that copyright notice and this permission notice appear # in supporting documentation, and that the name of The University # of Utah not be used in advertising or publicity pertaining to # distribution of the software without specific, written prior # permission. This software is supplied as is without expressed or # implied warranties of any kind. ################################################################################ use Foundation; use lib "/usr/local/lib/"; require "perlplist.pl"; use Getopt::Std; ##### # Get options # getopts( "zi:r:s:", \%options ); usage() if ! defined $ARGV[0]; my $set_ip = $ARGV[0]; my $networkPrefs = defined $ARGV[1] ? $ARGV[1] : "/Library/Preferences/SystemConfiguration/preferences.plist"; my $router; my $debug; my $subnetmask; my $interface_name; if ( defined $options{'z'} ) { $debug = 1; } else { $debug = 0; } if ( defined $options{'i'} ) { $interface_name = $options{'i'}; } else { $interface_name = 'Ethernet'; } if ( defined $options{'r'} ) { $router = $options{'r'}; } else { $router = ''; } if ( defined $options{'s'} ) { $subnetmask = $options{'s'}; } else { $subnetmask = ''; } print "Modifying $networkPrefs\n"; ##### # Load prefs # my $dict; if ( -e $networkPrefs ) { $dict = loadDefaults( $networkPrefs ); } else { die "The file: $networkPrefs not found"; } my $currentSet = perlValue(getPlistObject($dict, "CurrentSet")); $currentSet =~ s/\/Sets\///; print "currentSet $currentSet\n" if $debug; @serviceOrder = perlArrayFromNSArray(getPlistObject($dict, "Sets", $currentSet, "Network", "Global", "IPv4", "ServiceOrder")); my $ip; my $made_change = 0; ######## # Find the user defined name and change its IP # foreach $ii ( @serviceOrder ) { my $serviceKey = perlValue($ii); print "serviceKey $serviceKey\n" if $debug; my $userDefinedName = perlValue(getPlistObject($dict, "NetworkServices", $serviceKey, "UserDefinedName")); print "userDefinedName $userDefinedName\n" if $debug; if ( $userDefinedName eq $interface_name ) { if ( $set_ip eq lc( "dhcp" ) ) { setPlistObject($dict, "NetworkServices", $serviceKey, "IPv4", "ConfigMethod", "DHCP"); $made_change = 1; } else { my $configMethod = perlValue(getPlistObject($dict, "NetworkServices", $serviceKey, "IPv4", "ConfigMethod")); if ( $configMethod eq "Manual" ) { } $made_change = 1; setPlistObject($dict, "NetworkServices", $serviceKey, "IPv4", "ConfigMethod", "Manual"); setPlistObject($dict, "NetworkServices", $serviceKey, "IPv4", "Addresses", 0, $set_ip); if ( $router ne '' ) { setPlistObject($dict, "NetworkServices", $serviceKey, "IPv4", "Router", $router); } if ( $subnetmask ne '' ) { setPlistObject($dict, "NetworkServices", $serviceKey, "IPv4", "SubnetMasks", 0, $subnetmask); } } last; } } if ( ! $made_change ) { print "Cound not find $interface_name. No changes made.\n"; } ##### # Save file # saveDefaults( $dict, $networkPrefs ); sub usage { print <] [-s ] (12.34.56.78|dhcp) [path to preferences.plist file] If no path is given, it will use /Library/Preferences/SystemConfiguration/preferences.plist. EOF exit; } exit 0;