This New Years Day, Learn The Jot Command
The jot command is one I haven’t used in awhile. But it’s still useful. Let’s take a look at a few of the things you can do with it. First, let’s just print lines into a new file called “century.txt” which we can do by running with the number of increments followed by the starting number, and then redirecting the output into the file name:
jot 100 1 > ~/Desktop/century.txt
Or to do integers instead, simply put the decimals:
jot 100 1.00 > ~/Desktop/century.txt
Or in descending order,
jot – 100 1 > ~/Desktop/century.txt
Now we could change the output to be just 50 to 100, by incrementing 50 (the first position) and starting at 50 (the second):
jot 50 50
The jot command is able to print sequential data, as we’ve seen. But we can also print random data, using the -r option. Following that option we have three important positions, the first is the number of iterations, but the next two are the lower and upper boundaries for the numbers, respectively. So in the below command we’ll grab 10 iterations (or ten random numbers) that are between 1 and 1000:
jot -r 10 1 1000
Now if we were to add a -c in there and use a and z as the upper and lower bounds, we’d get… letters (this time we’re just gonna’ ask for one letter)!
jot -r -c 1 a z
Something I find useful is just to shove random data into a file infinitely. And by useful I mean hopefully not left running overnight on my own computer (been there, done that). To do this, just use a 0 for the number of iterations:
jot -r -c 0
Something that is actually useful is the basic ASCII set:
jot -c 128 0
We can also append data to a word using -w. So let’s say we want to print the characters aa followed by a through z. In the below we’ll define that with -w and then we’ll list those two characters followed by %c which is where the character substitution goes and then the number of iterations followed by the lower bound:
jot -w aa%c 26 a
You can also do stuttering sequences, useful for the occasional tango dancer, so here we’ll do a 5/3 countdown:
jot – 100 0 -.5
Or we could create a one meg file by creating 1,024 bytes:
jot -b 0 1024 > onemegfile.txt
Oh wait, that file’s two megs. Get it? 😉
And running strings teaches you that you can’t bound random (a good lesson for the New Year). Anything you use jot for?
Happy New Years!
krypted January 1st, 2018
Posted In: bash, Mac OS X, Mac OS X Server
command, howto, jot, MAC, macos, scripting
Server comes with a command called RoomsAdminTool located at /Applications/Server.app/Contents/ServerRoot/usr/bin/RoomsAdminTool. This tool can list available rooms using a -l flag:
You can also create new rooms, using the following format, where krypted is the name of the room, the persistent option means the room is, er, persistent. The description option indicates a description used for the room.
RoomsAdminTool -n krypted -c persistent yes description "This room is for friends of krypted only”
To then delete the room, use the -d option:
RoomsAdminTool -n krypted -d
Add the -v to do it all verbosely. There are lots of other options as well, as follows (from the man page):
Valid Configuration Keys and Values:
| KEY||VALID VALUES||DESCRIPTION
|description||string||A short description for the room
|password||string||Define a password for room entry. An empty string implies no password required.
|membersOnly||yes | no||Only room members are allowed to enter the room.
|subjectLocked||yes | no||Are non-moderators and non-admins prevented from setting the room subject
|logFormat||Disabled | Text | XHTML||Disable room logging, or enable it using Text or XHTML.
|maxUsers||integer; 0 for unlimited||Set the maximum allowed occupants for the room.
|moderated||yes | no ||Make the room "moderated".
|nonAnonymous||yes | no||If "yes", only moderators/owners can discover occupants' real JIDs.
|persistent||yes | no||Persistent rooms stay open until they are explicitly destroyed and their configuration survives service restarts, unlike non-persistent rooms.
|privateMessagesAllowed||yes | no ||Whether or not occupants can exchange private messages within the room.
|roomPublic||yes | no ||Defines whether the room be discovered by anyone
|subject||string||Set a room subject/topic
|usersCanInvite||yes | no ||Defines whether occupants can invite other users to enter the room
|addOwner||valid JabberID||Make the specified user a room owner (ex.: email@example.com). Rooms can have multiple owners.
|removeOwner||valid JabberID||Remove the specified user from the room owner list
|addAdmin||valid JabberID||Make the specified user a room admin
|removeAdmin||valid JabberID||Remove the specified user from the room admin list
|addMember||valid JabberID||Make the specified user a room member
|removeMember||valid JabberID||Remove the specified user from the room member list
|addOutcast||valid JabberID||Make the specified user a room outcast (banned from public rooms)
|removeOutcast||valid JabberID||Remove the specified user from the room outcast list
Ultimately, if you’d like to do Student Information System (SIS) integration, or wait for an AD/OD group and then programmatically generate rooms, this is how you’d do it. Also, it’s worth noting that Messages (and so Jabber if you’re running your own server) is a very basic instant messaging tool. There are more modern ways of interacting with others these days, including Slack and Confluence. Additionally, the Messages app can just use the phone number of people to let address books become a way of managing groups you’d like to message. These do not require a dedicated server, but most strategies will require a monthly fee that’s typically per-user.
krypted September 9th, 2017
Posted In: Mac OS X Server
Apple, create rooms, devices, jabber, MAC, scripting
Wordpress has an app. That means there’s an API to normalize communication using a predictable programmatic interface. In this case, as with many others, that’s done using a standard REST interface to communicate. The easiest way to interact with any API is to just read some stuff from the server via curl. You can feed curl the URL to the API by using your URL followed by /wp-json – as follows, assuming a URL of http://www.krypted.com:
To view header information:
curl -s -D - http://www.krypted.com -o /dev/null
In the below example we’ll ask for a list of posts by adding /wp/v2/posts to the URL:
You’ll see a list of some posts in the output along with a little metadata about the posts. You can then grab an ID and ask for just that post, using a post ID of 48390:
You can also see revisions that have been made to a post by appending the URL with /revisions
You can see comments with the comments route:
Or pages with the pages route:
Or users with the users route:
Or media that has been uploaded with the media route:
And the output of each can be constrained to a single item in that route by providing the ID of the item, which shows additional metadata about the specified item. And there are routes for categories, tags, etc.
There’s also some good stuff at https://github.com/WP-API
such as https://github.com/WP-API/Basic-Auth
which is a plugin that allows you to auth against the API.
curl --user admin:krypted http://www.krypted.com/wp-json/users/me
Not only can you look at user information, you can also add and remove posts. You would add by doing a -X followed by a POST and then feeding a file with the –data option
curl --user admin:password -X POST http://www.krypted.com/wp-json/posts --data @post.json
The output would then include the ID of your new post to wordpress. In the following example, we’ll get rid of the post we were looking at earlier using -X and DELETE in the URL, assuming a username of admin, a password of krypted, and a post ID of 48390:
curl --user admin:krypted -X DELETE http://www.krypted.com/wp-json/posts/48390
If successfully deleted the response would be as follows:
To dig in deeper, check out http://v2.wp-api.org/reference/posts/
where the whole schema is documented. You can also use the https://github.com/WP-API GitHub site to access a command called wp (as well as PHP, node, and java clients) that can be run at the command line for simple scripting interfaces. This could allow you to, for example, simply backup posts to json files, etc.
Also, it’s worth noting that various plugins will require their own interface (note there’s no themes or plugins route), such as woocommerce, interfacing with http://gerhardpotgieter.com/2014/02/10/woocommerce-rest-api-client-library/
krypted July 14th, 2017
Posted In: WordPress
API, Java, MAC, node, php, plugin, scripting, WordPress, wp
When you’re regression testing, you frequently just don’t want any delays for scripts unless you intentionally sleep your scripts. By default Safari has an internal delay that I’d totally forgotten about. So if your GUI scripts (yes, I know, yuck) are taking too long to run, check this out and see if it helps:
defaults write com.apple.Safari WebKitInitialTimedLayoutDelay 0
With a script I was recently working on, this made the thing take about an hour less. Might help for your stuffs, might not.
If not, to undo:
defaults delete com.apple.Safari WebKitInitialTimedLayoutDelay
krypted February 1st, 2017
Posted In: Mac OS X Server, Mac Security
Apple, bash, curl, defaults, MAC, safari delay, scripting, timeout, webkit
Dropping network connections can be incredibly frustrating. And finding the source can be a challenge. Over the years, I’ve found a number of troubleshooting methods, but the intermittent drop can be the worse to troubleshoot around. When this happens, I’ve occasionally resorted to scripting around failures, and dumping information into a log file to find the issue. For example, you may find that when a network connection fails, you have a very strong signal somewhere, or that you have a very weak signal on all networks.
I’ve found there are three pretty simple commands to test joining/unjoining, and using networks (beyond the standard pings or port scans on hosts). The first is the airport command, along with –disassociate. This just unjoins all networks:
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport --disassociate
The second is a quick scan. Here, I’ve grep’d out the network I’m after (aka SSIDofNetwork – a very likely wireless network name), but when looking for environmental issues, you might choose to parse this into a csv and output all networks:
sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s | grep SSIDofNetwork
Finally, you can join a network. You might have to escape out special characters in a password and it’s never wise to put a password into a script, etc. But, quick and dirty, this will join that SSIDofNetwork network:
sudo networksetup -setairportnetwork en0 "SSIDofNetwork" mysecretpassword
Anyway, loop it, invoke it however you invoke it, etc. Hope this helps someone, and if you have other tricks you’ve found helpful, feel free to throw them in the ‘ole comments!
How Users Feel About Intermittent Networking Issues
krypted August 26th, 2016
Posted In: Mac OS X, Mac OS X Server, Mac Security, Network Infrastructure, Programming
airport, Apple, bash, Join Network, scripting, testing dropped connections, unjoin network, wi-fi
Posted a new swift command line tool to accept serial number data from an Apple device and respond with warranty information about a device at https://github.com/krypted/swiftwarrantylookup
. This is based on pyMacWarranty, at https://github.com/pudquick/pyMacWarranty
krypted March 16th, 2016
Posted In: Mac OS X, Mac OS X Server, Mac Security, Mass Deployment, Swift
Apple, Command line, iPad, iPhone, look up warranty information for apple devices, MAC, programmatically, pyMacWarranty, scripting, swift
One of the options thats a tad bit hidden in OS X is the Secure Erase option, which runs a multi-pass erase on a volume. Additionally, there’s no option to Secure Erase free space on a volume. But you can still securely erase whatever you’d like (other than you boot volume obviously), when needed. To do so, use the diskutil command along with the secureErase option.
The format of the command to secureErase freespace is:
diskutil secureErase freespace [level] [device]
The levels are as follows (per the man page as not all of these are specified in Disk Utility):
- Single-pass zero-fill erase
- Single-pass random-fill erase
- US DoD 7-pass secure erase
- Gutmann algorithm 35-pass secure erase
- US DoE algorithm 3-pass secure erase
So for example, let’s say you had a volume called Seldon and you wanted to do a standard Single-pass zero-fill erase. In this example you would use the following:
diskutil secureErase freespace 0 /Volumes/Seldon
If you were to automate the command then you would want to dump the output into a log file. For example:
diskutil secureErase freespace 0 /Volumes/Seldon > /var/log/secureeraselog.tmp
You can also secureErase a volume itself. To erase a volume called /Volumes/Seldon, use the same structure of the command, but this time without the freespace option:
diskutil secureErase 0 /Volumes/Seldon
The latest update to Disk Utility removes a lot of options from the GUI, but overall, I have yet to find a scenario where a task I need to perform isn’t still available, if only from the command line.
krypted January 7th, 2016
Posted In: Mac OS X, Mac OS X Server, Mac Security, Mass Deployment
Apple, clear free space, DoD, file removal, MAC, scripting, secure erase, secureerase, seven pass erase
Pretty much every script I’m working on these days must be run as root. Checking what user is running something is pretty straight forward, as there’s a built-in shell variable for $USER that contains the user running a script. To see this real quick, simply run the following:
You can then put this into your scripts. I’ve been using the same block of code for decades, which can be run in a script by itself if you’d like to paste this into one.
if [[ $USER != "root" ]]; then
echo "This script must be run as root"
echo "You are root"
Note: Keep in mind that the built-in $USER variable is case sensitive.
Obviously, most people won’t keep the lines that contain the else and you are root echo statements. You can just remove these or replace them with the meat of your script that requires elevated privileges to run. Enjoy.
krypted December 21st, 2015
Posted In: Mac OS X, Mac OS X Server, Mac Security, Mass Deployment, Unix
bash, check that a script is run with elevated privileges, check user, MAC, os x, OS X Server, root, scripting, Shell
Next Page »
Someone hands you a USB drive. You put it in your computer and you can’t access anything on it. You are running an imaging lab and you want to backup or troubleshoot a device before you re-image it, but you can’t access certain files. Obviously, you can sudo. But, you can also simply disable permissions on that volume (which, like getting someone to make you a sandwich, requires sudo of course).
The command used to enable and disable permissions on a volume is vsdbutil, located at /usr/sbin/vsdbutil. And there’s a LaunchDaemon at /System/Library/LaunchDaemons/com.apple.vsdbutil.plist that interacts with diskarbitrationd so that when a volume is mounted, it is marked as having permissions activated or deactivated (which is basically “Ignore Permissions” at the Finder).
To use vsdbutil to enable “Ignore Permissions”, use the -d flag followed by the path to the volume:
sudo /usr/sbin/vsdbutil -d /Volumes/Myvolume
To then enable (or activate, thus the a) permissions again, use the -a flag:
sudo /usr/sbin/vsdbutil -a /Volumes/Myvolume
You can also run the -c to see the status for a given path:
sudo /usr/sbin/vsdbutil -c /Volumes/Myvolume
And last but certainly not least if you’re working on a lot of volumes, the -i option will enable permissions on all mounted HFS and HFS+ volumes:
sudo /usr/sbin/vsdbutil -i
Overall, it’s very easy to send these commands using a positional parameter (e.g. $1) to a script, performing a mount, some operation (backup, reimage, restore, repair some corrupted data, etc).
Note: You can’t Ignore Permissions of FAT or FAT32 volumes using the command line or a Finder Get Info screen.
krypted December 1st, 2015
Posted In: Mac OS X, Mac Security, Mass Deployment
Apple, HFS+, ignore permissions, MAC, script, scripting, vsdbutil