krypted.com

Tiny Deathstars of Foulness

We tend to use a lot of commands in the Terminal app. That is, after all, what it’s there fore. And there’s a nice history of what we do. There are also a number of ways to view and manage the bash history. The simplest of which is the history command, which will show the previous commands run. Here, we’ll simply run it:

history

Keep in mind that this shows the history based on context, so if you sudo bash, you’ll potentially see a different history. You can also use the bash built-in fc command, which has the additional awesomeness of being able to edit and re-run commands from the history. To start, we’ll simply look at showing the last 16 commands using the -l option:

fc -l

You can also constraint entries in the output by specific line numbers. For example, to see lines 12 through 18, simply use them as the first two positions of the command after fc:

fc 12 18

You can load the history into an editor and remove or add entries using fc without any options:

fc

To exit the editor, hit control-z. I’ve written in the past about using substitution. For example, sudo !! to run the last command. fc can do some basic substitution as well. For example, use the -s to start substation and then enter a string, which will append whatever you like before a command. So the following would put sudo in front and re-run the previous command:

fc -s sudo

And let’s say that you were doing a find for a string of krypted. To then swap that string with charles:

fc -s krypted=charles

Overall, the bash history can be incredibly useful. I frequently pipe the output of a series of lines into a new file with a .sh at the end as a starting point for scripts and use these substitution options to save myself a bunch of time not retyping longer commands. Enjoy.

August 14th, 2015

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

Tags: , , , , , , , ,

Leave a Comment

The cd command has lots of fun little shortcuts. One I use frequently is the -. The ~ always takes you to your home directory, but using cd – will take you to the last directory you were in. For example, if you do the following on a Mac:

cd ~

Then you do .. (which is a shortcut for the directory above the one you’re in):

cd ..

Then pwd will show that you’re in /Users. But, if you cd to – again:

cd -

Now you’re back in your home folder. The – expands to OLDPWD. Quick tip. Nothing more to see here.

July 20th, 2015

Posted In: Mac OS X, Ubuntu, Unix

Tags: , , , , ,

The following command will remove all empty lines from a file called badcommand.sh:

sed '/^$/d' badcommand.sh

July 12th, 2015

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

Tags: , , , ,

The options for Open Directory continue to get more refined, aligning with opendirectoryd. The odutil command is becoming more and more useful with each version of OS X. Let’s inspect the directory service cache, using odutil with the show verb and the cache option:

odutil show cache

You can also view statistics for opendirectoryd using that show verb but with the statistics option:

odutil show statistics

And to see everything, use odutil with the show verb and the all option to get plenty of data to grep through:

odutil show all

The final show option we’ll look at is configuration. Here, you will also need to feed a directory nodename into the command:

odutil show configuration /Search

Now, /Search is a node but there are a lot. You can use show with nodes to see a listing of all the nodes:

odutil show nodes

You can then see which pids have references to opendirectoryd as well as the nodenames, reference IDs, and session IDs.

All of this can be very helpful when troubleshooting Open Directory issues. One thing I find I do pretty frequently is resetting statistics then repeating a process that is causing a problem so I can view only the updated statistics. To do so:

odutil reset statistics

You can also disable statistics (I’ve seen them create performance concerns:

odutil set statistics off

Or to turn them back on:

odutil set statistics on

Once upon a time you could killall DirectoryService with a -usr level to set various logging levels. With opendirectoryd, we can still do that, but it’s less cludgy with odutil. Here, we’ll set the logging level as detailed as we can get:

odutil set log debug

Other levels, in ascending order of verbosity, include alert, critical, error, warning, notice, and info.

July 10th, 2015

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

Tags: , , , , , , ,

When you run a kill command to stop a process from bash or the javax.realtime.POSIXSignalHandler class, you’re sending what’s known as a POSIX signal to the process. These signals can be called via their numeric representations or the signal (e.g. with the -s option of the kill command). Signals include the following:

  • 1: SIGHUP – Close the controlling terminal when the controlling process dies
  • 2: SIGINT – Send a keyboard interrupt
  • 3: SIGQUIT – Quit from a keyboard/terminal
  • 4: SIGILL – Terminate illegal instruction with a core dump and don’t restart
  • 5: SIGTRAP – Send a trace/break trap (with core dump)
  • 6: SIGABRT – Process an abort signal
  • 7: SIGEMT – Send the signal when emulator traps happen
  • 8:SIGFPE – Terminate floating point errors (erroneous arithmetic operations) with a core dump
  • 9: SIGKILL – Kill a signal outright (kill cannot be ignored)
  • 10: SIGBUS – Terminate access (some portion of a memory object) with a core dump
  • 11: SIGSEGV – Terminate with a core dump – Invalid memory reference
  • 12: SIGSYS – Bad system call
  • 13: SIGPIPE – Terminate and write on the pipe
  • 14: SIGALRM – Timed kill of a signal
  • 15: SIGTERM – Software termination of a signal
  • 16: SIGUSR1 – User defined signal 1, with SIGUSR2 as user defined signal 2
  • 17: SIGTERM – Software termination of a signal
  • 18: SIGCHLD – Child status change
  • 19: SIGPWR – Send the signal when the system encounters a power failure
  • 20: SIGWINCH – Send the signal to a process when the window changes
  • 21: SIGURG – Ignore signal, high bandwidth data
  • 22: SIGIO – Terminate pollable event
  • 23: SIGSTOP – Stop executing (cannot be ignored or caught by an exception)
  • 24: SIGTSTP – Terminate a stop signal.
  • 25: SIGCONT – If stopped, continue executing a process
  • 26: SIGTTIN – Background process is attempting to read
  • 27: SIGTTOU – Background process is attempting to write
  • 28: SIGVTALTM – Expired virtual timer
  • 29: SIGPROF – Terminate Profiling timer
  • 30: SIGXCPU – Past the CPU time limit, terminate with a core dump
  • 31: SIGXFSZ – Past the file size limit, terminate with a core dump
  • 32: SIGWAITING – Suspend execution of the process until a defined signal is sent
  • 33: SIGLWP – Send when the implementing threading requires a signal
  • 34: SIGFREEZE – Deprecated
  • 35: SIGTHAW – Deprecated
  • 36: SIGCANCEL – Deprecated
  • 37: SIGLOST – Send the signal when encountering a lost file lock

To put these in practice, let’s use the kill command from bash, with the -s option followed by SIGTERM and then the pid number:

sudo kill -s SIGTERM 20341

July 5th, 2015

Posted In: Mac OS X, Mac Security

Tags: , , , , , ,

In bash, you can run multiple commands in a single line of a script. You do so by separating them with a semi-colon (;). The great thing about this is that if you end up using a variable, you can pass it on to subsequent commands. Here, we’re going to string three commands together and then echo the output:

a=1;b=2;c=$a+$b;echo $c

because we told c to be $a + $b, the $a expands to 1 and the $b expands to 2, we throw them together and then echo out the contents of c$ which appears as follows:

1+2

Now, we could have this thing do math as well, by wrapping the mathematical operation in double-parenthesis, which bash treats as an arithmetic expansion:

a=1;b=2;c=(($a+$b));echo $c

The output this one is simply 3.

June 15th, 2015

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

Tags: , , , , , , , , ,

Popped in a list of Linux bash commands here: http://krypted.com/commands/linux-bash-commands/

January 24th, 2015

Posted In: Ubuntu

Tags: , , , , ,

Merry Christmas ya’ll!

On the first day of Christmas my true love gave to me one 32 gig iPad

On the second day of Christmas my true love gave to me two bash one-liners

On the third day of Christmas my true love gave to me three Red Hat servers

On the fourth day of Christmas my true love gave to me four email blasts

On the fifth day of Christmas my true love gave to me five retweets

On the sixth day of Christmas my true love gave to me six regular expressions

On the seventh day of Christmas my true love gave to me seven lines of perl

On the eighth day of Christmas my true love gave to me eight app store apps

On the ninth day of Christmas my true love gave to me nine AWS instances

On the tenth day of Christmas my true love gave to me ten Active Directory forests

On the eleventh day of Christmas my true love gave to me 11 crappy python scripts

On the twelfth day of Christmas my true love gave to me 12 craft brews

xmas-ornament-computer-ram

December 25th, 2014

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

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

I was recently building some preflight scripts and was looking to record some information about a machine live, before proceeding with a script. I found the cheapest way to determine information about architectures and chipsets when scripting preflight scripts for OS X to be the arch and machine commands respectively. For example, to verify the architecture is i386, use the arch command with no options:

/usr/bin/arch

Which simply outputs “i386”:

i386

To check the machine type, simply use the machine command:

/usr/bin/machine

Which outputs as follows:

x86_64h

December 14th, 2014

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

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: , , , , , , , , , ,

Next Page »