krypted.com

Tiny Deathstars of Foulness

Namespace conflicts can be interesting. Especially with multiple local domains. To grab the path of a directory domain of a currently logged in user (when running as the user) using a script, you can run the following:

dscl . -read /Users/`whoami` | grep AppleMetaNodeLocation | awk '{print $2}'

You can then replace the string we’re using with grep if you’d like to pull a different attribute from the user record, you’d use the following:

dscl . -read /Users/`whoami` | grep UniqueID | awk '{print $2}'

August 22nd, 2016

Posted In: Mac OS X, Mac OS X Server, Mac Security

Tags: , , , ,

Just a quick one-liner. Enjoy.

profiles -Cv | grep Enrollment | awk '{ s = ""; for (i = 5; i <= NF; i++) s = s $i " "; print s }'

August 20th, 2016

Posted In: Mac OS X, Mac OS X Server, Mac Security, Mass Deployment

Tags: ,

You see a lot of entries for various things in log files. Here, we’re going to print out the number of entries with backupd in them:

awk '/backupd/{print NR}' /var/log/system.log

July 11th, 2015

Posted In: Mac OS X, Ubuntu, Unix

Tags: , , , , , ,

Recently I was working on a project where we were isolating IP addresses by country. In the process, I found an easy little tool built right into OS X called ip2cc. Using ip2cc, you can lookup what country an IP is in. To do so, simply run ip2cc followed by a name or ip address. For example, to lookup apple.com you might run:

ip2cc apple.com

Or to lookup Much Music, you might run:

ip2cc muchmusic.ca

The output would be:

IP::Country modules (v2.28)
Copyright (c) 2002-13 Nigel Wetters Gourlay
Database updated Wed May 15 15:29:48 2013

Name: muchmusic.com
Address: 199.85.71.88
Country: CA (Canada)

You can just get the country line:

ip2cc apple.com | grep Country:

To just get the country code:

ip2cc apple.com | grep Country: | awk '{ print $2 }'

Finally, ip2cc is located at /usr/bin/ip2cc so we’ll complicate things just a tad by replacing the hostname with the current IP (note that private IPs can’t be looked up, so this would only work if you’re rocking on a wan ip or feeding it what a curl from a service like whatismyip brings back):

ip2cc `ipconfig getifaddr en0` | grep Country: | awk '{ print $2 }'

December 13th, 2014

Posted In: Mac OS X, Mac OS X Server

Tags: , , , , , , , , , ,

The number of characters n a line of text can be a difficult thing to calculate in a given app. If you have data in a text file, you can use awk to view the number of characters in a given line of the file. This is very helpful, for example, if you have code that you’ve put into documentation that exceeds the character maximum and therefore wraps. When going to print, you need to split these lines up.

Here, we’re going to use the awk command to review all lines that exceed 56 characters:

awk 'length($0) > 56' ~/Repo/Chapter1.xml

October 15th, 2013

Posted In: Mac OS X, Mac OS X Server, Mac Security, Unix

Tags: , , , ,

Yesterday I showed a way to get the serial number from a Mac OS X machine. However, as a couple of people pointed out, Apple will soon be adding another character to the serial number. This means that rather than use cut I should have used awk to allow for either serial number length. To grab the serial this way:

ioreg -l | grep IOPlatformSerialNumber | awk ‘{print $4}’

Or without the quotes:

ioreg -l | grep IOPlatformSerialNumber | awk ‘{print $4}’ | sed ‘s/”//g’

May 13th, 2010

Posted In: Mac OS X, Mac OS X Server, Mass Deployment

Tags: , , ,

A number of commands available for finding positions that you want in a line and extracting only a certain amount of text can be pretty cumbersome in terms of learning curve. This isn’t to say that once you get the hang of them that they’re terribly complicated but it can take a little while to get the hang of them. And when you need something fast, you might want an easy command for extracting text from lines. In these cases, consider cut. The cut command doesn’t do regular expressions (I guess you could argue that its ability to use a delimiter can be used as a regular expression) and so it’s really easy to use.

Basically, you feed cut some data and then tell it which characters in the line that you want to keep. It then gets rid of the rest. The easiest use of this is to look at a list of data. For example, let’s saw we have a file called test.txt with the following contents:

abc123
abc124
abc134
abc234
abd234
acd234

Now we’re going to cat the file (which just reads the file contents) and then pipe the output of reading that file into a cut command (which is done by simply adding a pipe character at the end of the first part of the command. Then we’re going to use the -c option of cut (which looks at character positions) to simply grab the first three positions (1-3) of the lines. The command would end up looking as follows:

cat test.txt | cut -c 1-3

And the output would look as follows (this output could then be redirected into a new file btw):

abc
abc
abc
abc
abd
acd

You can also specify multiple ranges of characters (or single characters for that matter). For example, to see only characters 1-2 and 5-6:

cat test.txt | cut -c 1-2,5-6

Overall, cut is a very easy to use tool, with a limitation that your pattern that you are looking to maintain must be consistent in terms of the character position that you are using in each line. It also uses every line in a file; however, to go another step and look for all positions in a line only if the line has a pattern that it can match you could simply add a grep in the middle. For example, if you’re looking for each line of our sample text file that has the number 4 then you could do:

cat test.txt | grep 4

This would show you only the last five lines of the file since those are the only lines that have that number in them. You could then pipe the output of that file into your cut and, let’s say, look for characters 1-3 and 6 in the output:

cat test.txt | grep 4 | cut -c 1-3,6

Your result would then be the following:

abc4
abc4
abc4
abd4
acd4

Finally, there are going to be times when you’re not looking for a specific character position in a line but instead a character position or a pattern that begins with another pattern. For this you’re going to end up needing to use a more advanced tool, such as awk or if you’re feelin’ frisky (maybe I’m speaking for myself there) regex. These tools will have a steeper learning curve, but ultimately be far more useful.

January 17th, 2010

Posted In: Mac OS X, Mac OS X Server, Mac Security, Unix

Tags: , , , , , , , , ,

For some reason the uninstaller from Symantec doesn’t work in removing Norton (NAV 10). My guess, without delving into their uninstaller too deeply is that they ran into what I ran into, which is that the com.symantec.* processes are prefixed by a bracketed alphanumeric sequence. To get around this I listed them and used grep to grab each one, then awk to grab the label and did a launchctl stop against the label name once I had it. The rest of this script is pretty straight forward forcing the rm of each of the contents of the items from the snapshot plus the items from the pkg BoM.  Here’s the script, or you can download it here:

#! /bin/bash
launchctl stop `launchctl list | grep com.symantec.SymSecondaryLaunch | awk ‘{print $3}’`
launchctl stop `launchctl list | grep com.symantec.scanNotification | awk ‘{print $3}’`
launchctl stop `launchctl list | grep com.symantec.diskMountNotify | awk ‘{print $3}’`
launchctl stop `launchctl list | grep com.symantec.quickmenu | awk ‘{print $3}’`
kextunload -b com.Symantec.SymEvent.kext
kextunload -b com.Symantec.SymOSXKernelUtilities.kext
kextunload -b com.Symantec.kext.KTUM
rm /etc/liveupdate.conf
rm /etc/Symantec.conf
rm /usr/bin/symsched
rm /usr/bin/navx
rm ~/Library/Preferences/com.Symantec.Scheduler.plist
rm /Users/Shared/snorosx
rm -rfd /Library/Contextual Menu Items/NAVCMPlugin.plugin
rm -rfd /Applications/Symantec Solutions
rm -rfd /Applications/Norton AntiVirus
rm -rfd /Library/Receipts/NAVContextualMenu.pkg
rm -rfd /Library/Receipts/NAVEngine.pkg
rm -rfd /Library/Receipts/Norton AntiVirus.pkg
rm -rfd /Library/Receipts/SymEvent.pkg
rm -rfd /Library/Receipts/SymOSXKernelUtilities.pkg
rm -rfd /Library/Receipts/NortonQuickMenu.pkg
rm -rfd /Library/Receipts/SymSharedFrameworks.pkg
rm -rfd /Library/Receipts/Norton AutoProtect.pkg
rm -rfd /Library/Recepits/Symantec Scheduled Scans.pkg
rm -rfd /Library/Recepits/Symantec Scheduled Scans.pkg
rm -rfd /Library/Recepits/Symantec Scheduled Scans.pkg
rm -rfd /Library/Receipts/navx.pkg
rm -rfd /Library/Receipts/LiveUpdate.pkg
rm -rfd /Library/Receipts/Symantec Scheduler.pkg
rm -rfd /Library/Receipts/Stuffit.pkg
rm -rfd /Library/Receipts/SymInstallExtras.pkg
rm -rfd /Library/Receipts/SymHelpScripts.pkg
rm -rfd /Library/Receipts/SymantecUninstaller.pkg
rm -rfd /Library/Receipts/Symantec Alerts.pkg
rm -rfd /Library/Application Support/Norton Solutions Support
rm /Library/Application Support/NAV.history
rm -rfd /Library/Application Support/Symantec
rm -rfd /Library/PreferencePanes/SymantecQuickMenu.prefPane
rm -rfd /Library/PreferencePanes/APPrefPane.prefPane
rm -rfd /Library/PrivateFrameworks/SymAppKitAdditions.framework
rm -rfd /Library/PrivateFrameworks/SymBase.framework
rm -rfd /Library/PrivateFrameworks/SymNetworking.framework
rm -rfd /Library/PrivateFrameworks/SymSystem.framework
rm -rfd /Library/PrivateFrameworks/SymScheduler.framework
rm -rfd /Library/StartupItems/NortonAutoProtect
rm -rfd /Library/StartupItems/NortonMissedTasks
rm -rfd /Library/Documentation/Help/Norton Help Scripts
rm -rfd /Library/Widgets/Symantec Alerts.wdgt
rm -rfd /System/Library/Extensions/SymEvent.kext
rm -rfd /System/Library/Extensions/SymOSXKernelUtilities.kext
rm -rfd /System/Library/Extensions/KTUM.kext
rm /System/Library/Extensions.mkext.NxdE

Oh, since most everything I do on this site requires elevated privileges I usually forget to mention it, but this script will require those…

May 5th, 2009

Posted In: Mac OS X, Mac Security, Mass Deployment

Tags: , , , , ,

At a terminal prompt, it is really straight forward to grab the date, simply use the date command, with no arguments and you will get something similar to the following, including the day, date, time (with seconds), time zone and year:

Tue Apr 15 00:40:07 CDT 2009

In a script this can choose fairly challenging, especially in cases where you just need the date stamp without the time and time zone, etc. Here we’re going to grab the current system date from ESX, OS X or Linux (or whatever OSen really) and then use a variable, currentdate, to put that date, formatted into a pretty standard format, YYYYMMDD:

currentdate=`date ”+%c%m%d” | awk ‘{printf $5}’`

Which will output the date as follows:

20090415

Now, in our shell script we can create files, add lines to files, etc, with the shortened date stamp. Some of you will be using log analyzers that depend, for example, on unix epoch time. To grab the date formatted as such, the following command can be used:

date -j -f “%a %b %d %T %Z %Y” “`date`” “+%s”

The date command isn’t used as much to set time any more, since most systems rely on a Network Time Protocol (NTP) server to supply date and time information. However, it is worth noting that the date command can also be used to set the time on a computer.

April 15th, 2009

Posted In: Mac OS X, Mac OS X Server, Mass Deployment, Ubuntu, Unix, VMware

Tags: , ,