Posts Tagged ‘script’

Add OS X Network Settings Remotely (Without Breaking Stuff)

Monday, September 23rd, 2013

So you’re going to send a computer off to a colocation facility, and it’ll use a static IP and DNS when it gets there, the info for which it’ll need before it arrives. Just like colo, you access this computer remotely to prepare it for its trip, but don’t want to knock it off the network while prepping this info, so you can verify it’s good to go and shut it down.

It’s the type of thing, like setting up email accounts programmatically, that somebody should have figured out and shared with the community as some point. But even if my google-fu is weak, I guess I can deal with having tomatoes thrown at me, so here’s a rough mock-up:

 

#!/bin/bash
# purpose: add a network location with manual IP info without switching 
#   This script lets you fill in settings and apply them on en0(assuming that's active)
#   but only interrupts current connectivity long enough to apply the settings,
#   it then immediately switches back. (It also assumes a 'Static' location doesn't already exist...)
#   Use at your own risk! No warranty granted or implied! Tell us we're doing it rong on twitter!
# author: Allister Banks, 318 Inc.

# set -x

declare -xr networksetup="/usr/sbin/networksetup"

declare -xr MYIP="192.168.111.177"
declare -xr MYMASK="255.255.255.0"
declare -xr MYROUTER="192.168.111.1"
declare -xr DNSSERVERS="8.8.8.8 8.8.4.4"

declare -x PORTANDSERVICE=`$networksetup -listallhardwareports | awk '/en0/{print x};{x=$0}' | cut -d ' ' -f 3`

$networksetup -createlocation "Static" populate
$networksetup -switchtolocation "Static"
$networksetup -setmanual $PORTANDSERVICE $MYIP $MYMASK $MYROUTER
$networksetup -setdnsservers $PORTANDSERVICE $DNSSERVERS
$networksetup -switchtolocation Automatic

exit 0

Caveats: The script assumes the interface you want to be active in the future is en0, just for ease of testing before deployment. Also, that there isn’t already a network location called ‘Static’, and that you do want all interface populated upon creation(because I couldn’t think of particularly good reasons why not.)

If you find the need, give it a try and tweet at us with your questions/comments!


Create Empty Files Of Arbitrary File Sizes In Windows

Wednesday, November 21st, 2012
The fsutil command in Windows can be used to create empty files of arbitrary sizes. To create a file to a path, use fsutil along with the file option, followed by create new, then the path and then the size. For example, to create a 100MB file called myfile.txt at c:\testfiles:
fsutil file createnew “c:\testfiles myfile.txt” 100000000

Enable SNMP On Multiple Mac Workstations Using A Script

Monday, November 12th, 2012

SNMP can be a valuable tool for monitoring the health of unattended Mac workstations acting as a farm to process information for remote users. If the health of a farm member degrades because its hard drive gets full or a process gets stuck then SNMP can send traps to a Network Management Station to alert the administrator.

Before SNMP will return any useful information an administrator must configure the Mac using the snmpconf command. By default this command runs interactively and prompts him for basic information to create the /usr/share/snmp/snmpd.conf file. However, he can use this file to script the same configuration for other machines without interaction. The script can also run a simple launchd command afterward to start the snmp service.

Create the snmpd.conf file

Creating the snmpd.conf file is as simple as running a command in the Terminal and answering a few questions.

  1. Launch the Terminal application found in /Applications/Utilities.
  2. The Terminal defaults to the current user’s home folder. Verify this using the pwd command. This is where the snmpconf command will create the snmpd.conf file.
  3. Enter snmpconf in the Terminal and press return.
  4. This begins a series of simple questions. The first question is:

    The following installed configuration files were found:

    1: /etc/snmp/snmpd.conf

    Would you like me to read them in? Their content will be merged with the output files created by this session.

    Valid answer examples: “all”, “none”,”3″,”1,2,5″

    Read in which (default = all):

    Press return to accept the default answer “all”.

  5. The next question is:

    I can create the following types of configuration files for you.
    Select the file type you wish to create:
    (you can create more than one as you run this program)

    1: snmpd.conf
    2: snmptrapd.conf
    3: snmp.conf

    Other options: quit

    Select File:

    Enter 1 to choose to create the snmpd.conf file.

  6. Next, choose 1 for Access Control Setup. This will set the community name for both read/write as well as read access. For monitoring purposes an administrator should configure read-only communities such as talkingmoose-read. Set the community name for both SNMPv3 read-only user as well as SNMPv1/SNMPv2 read-only access community name. These may be the same name.
  7. When the read-only communities are set then type finished to exit the access control setup and proceed to the rest of the sections.

Some questions will be for more advanced snmp settings, which some administrators will want to partially or fully customize. For basic snmp functionality either accept the defaults or don’t answer the questions. At minimum, though, complete the Access Control Setup and System Information Setup sections.

After answering the questions and returning to the top level section type quit to complete creating the snmpd.conf file. The snmpconf command places this file in the current working directory in Terminal.

Load snmpd.conf onto another Mac

Loading these settings on another machine requires the same snmpconf command but with some instructions to use the newly created file. Do the following:

  1. Copy the snmpd.conf file to the new machine.
  2. Run the following command on the new machine:sudo snmpconf -R /path/to/snmpd.conf -a -f -i snmpd.conf

This snmpconf command takes the supplied snmpd.conf file (-R /path/to/snmpd.conf) to quietly configure a new one (-a) overwriting anything already configured (-f) and places it in the correct location (-i), which is /usr/share/snmp/.

Start SNMP

After the settings are loaded and a newly created snmpd.conf file exists in /usr/share/snmp/, start the SNMP service:

sudo launchctl load -w
/System/Library/LaunchDaemons/org.net-snmp.snmpd.plist

Test using snmpwalk

To verify the settings are applied correctly use the snmpwalk command to read SNMP data from the Mac using the read-only user or community name created when completing the Access Control Setup section earlier:

snmpwalk -v1 -c talkingmoose-read localhost

This should return a lengthy amount of information that begins with something like:

SNMPv2-MIB::sysDescr.0 = STRING: Darwin TMServer.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.255
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (751563) 2:05:15.63
SNMPv2-MIB::sysContact.0 = STRING: "William Smith"
SNMPv2-MIB::sysName.0 = STRING: TMServer.local
SNMPv2-MIB::sysLocation.0 = STRING: "Saint Paul"
SNMPv2-MIB::sysServices.0 = INTEGER: 12

Deployment

The most efficient deployment method for current and future Mac farm machines is an Apple Installer package. Add the snmpd.conf file as a resource file to the package and add a postflight script to load the file and start the SNMP service.

Basic Script for Creating Mirrors

Monday, September 12th, 2011

Moving a volume to a mirror is often the first things people do to a new server that shows up out of the box. While this script reads input about two volumes and creates a mirror based on that input, it’s easily migrated into something akin to a DeployStudio or scripted workflow:

#!/bin/bash
#Converts a standalone disk to a RAID 1 and automates adding the second member.
clear
echo -n "Enter the name of the first volume to be placed in the mirror: "
read disk_1
export disk_1nv=`echo $disk_1 | sed 's:/Volumes/::g'`
echo "
creating the $disk_1 mirror"
sleep 2
export disk_1slice=`diskutil list "$disk_1" | grep -m 1 "$disk_1nv" | grep -o "disk..."`
diskutil appleRAID enable mirror $disk_1slice
echo -n "Enter the name of the second volume to be placed in the mirror: "
read disk_2
export disk_2nv=`echo $disk_2 | sed 's:/Volumes/::g'`
export disk_2root=`diskutil list "$disk_2" | grep -m 1 "$disk_2nv" | grep -o "disk."`
export raid_uuid=`diskutil info $disk_1slice | grep "Parent RAID Set UUID" | sed -e 's_Parent RAID Set UUID:__g;s_^[ \t]*__'`
diskutil AppleRAID add member $disk_2root $raid_uuid

Install Powerchute Using a Script

Friday, February 11th, 2011

Here’s a little shell script that can be deployed from ARD to install and configure APC’s Powerchute Network software for Mac OS X clients. It’s currently only been tested with 2.2.4, but was used it to deploy Powerchute to 7 servers and can be quite a time saver. The only prereq is that the APC tar file be located at the path specified by variable ‘apcfile’ and the other variables in the script be completed.

Let us know if you have any questions!

### sends keystrokes to configure APC Powerchute software.

apcfile='/tmp/pcns224Mac.tar'
localadminpassword='Creative1'
nictoregister='en0'
apcip='192.168.11.220'
apcadmin='apc'
apcpassword='apc'
apcsharedsecret='apcismyfavoriteperson'

## start script
mkdir /tmp/apc_temp &> /dev/null
cd /tmp/apc_temp
tar -xf "$apcfile"

## get our IP
IP="$(ifconfig $nictoregister | awk '/inet / {print $2}' | head -1)"

open /tmp/apc_temp/install.command
sleep 3

osascript < tell application "System Events"
keystroke "$localadminpassword"
delay .2
keystroke return
delay 2
keystroke space
delay 1
keystroke space
delay 1
keystroke space
delay 1
keystroke space
delay 1
keystroke "$apcip"
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke space
delay 1
keystroke tab
delay .1
keystroke "$IP"
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke space
delay 1
keystroke "$apcadmin"
delay .1
keystroke tab
delay .1
keystroke "$apcpassword"
delay .1
keystroke tab
delay .1
keystroke "$apcsharedsecret"
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke tab
delay .1
keystroke space

end tell
EOL