ImageMagick Tips and Tricks

TerminalI’ve been using ImageMagick in one of my projects and I’ve had to do quite a bit of Googling. Below are some tips and tricks I’ve collected that might be useful to others.

Trim Whitespace From Your Image

$ convert -trim img.png

Create A New Image From Text

Note: You can combine this with the ‘trim’ tip above by passing the -trim argument.

$ convert -background white -fill black -pointsize 72 label:"Sean Fox" output.png

Output:
ImageMagick Converted Text

List Fonts ImageMagick Can Use

$ identify -list font

Normalizing Line Endings Across Multiple Files

Terminal Recently I’ve been noticing some of my C# files have inconsistent line endings.

Visual Studio: Inconsistent Line Endings

The Problem

The line endings in the following file are not consistent. Do you want to normalize the line endings?

This probably means some of the developers have been editing our source code using a text-editor (instead of Visual Studio) and they had their line endings set to “Unix” or “Mac” instead of “Dos/Windows”. This isn’t a big deal, though it is annoying.

Vim: Convert All Files to Dos/Windows Line Endings

Since I don’t know which files have this problem, I decided to just normalize all the line endings. There are various ways to do this, but I chose to use vim since the documentation on this topic is excellent.

To convert from any mixture of CRLF endings and LF-only endings, to CRLF endings:

:set ffs=dos
:args **\*.cs
:argdo w

We’re basically asking vim to assume Dos/Windows line endings, having it open all *.cs files (C# code files), then having it write the line-ending changes.

Downsides

This does change every single file that was found, regardless of the original line endings. It’s best to do this as one commit in source control to avoid any confusion.

Alternatives

You can also use unix2dos to do this conversion or any scripting language (python, perl, powershell).

Some people also let their source control handle their line endings. Git can enforce these line endings by updating them for you if you prefer. However, at my office, we choose not to let our source control alter our code in any way; we prefer to do that ourselves. :)

Now get back to coding!

Regenerating Your SSH Public Key from Your SSH Private Key

Terminal Have you ever found yourself in a situation where you had your SSH private key, but not your public key? Maybe you copied your private key to a new laptop, but then realized you need your public key so your coworker can add you to the new Git repo. Or maybe you just plain lost your public key or have no idea what happened to it. But guess what, that’s not a problem! Because you can regenerate it!

Using ssh-keygen

The ssh-keygen command allows you to regenerate a public key using the -y flag. Using the -t flag you can tell it whether the key is rsa or dsa.

Is my key RSA or DSA?

Chances are it doesn’t matter; ssh-keygen will try to guess based on the input key. However, if you don’t know your key encryption, it’s probably rsa since that’s the default. The filename will also typically tell you, since it’s usually either id_rsa or id_dsa. And even beyond that, if you look at the text in the file, it should be present there as well.

Regenerating a Public Key

Here’s an example:

# ssh-keygen -y
Enter file in which the key is (/home/username/.ssh/id_rsa):
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr3T65FaSononqBjGEZXMg8x0U3ZjYvZxAUZQA7H27VtPgrn9FhsP8Jn+sp0zOi2nFjDsbXWM5L6OPVg1N0OHpiNcg7I
lrc83GiqVGg2AWeHWWolnOwXIsrfwybVcS6ZSCGbGKVWWL5VB/mt/zzF5WD6bhU+TZXYLq8fZC4sa0sapqVccubKw2YbjA53n0wKxrYLfOjP1k56EfkHzm4n7fmlyFi
3kaCvPo31yaMD3zIVJnl/4wMntnnxqFkG7mEtQ29ngkc5ocgRvSbNNvD9IFNvL/9BqlUtiOUcV790cdoLyd0o1mFV8sGPY3zsL6l3lTkjYDmSXTTnxavjHEudC5w==

BAM! There’s your public key!

Is this safe?

Yes, regenerating your key is completely safe and there’s no reason why you wouldn’t want to do this. Public keys are public, just as their name implies. You can give your public key to anyone.

The private key is the one you should keep to yourself and safe-guard. You should be the only one with your private key, and there’s no reason you should not be able to get a copy of your own public key.

Now save that public key somewhere safe so you don’t have to generate it every time!

Happy authenticating!

Enabling SSH/SFTP Updates in WordPress on Amazon EC2 and CentOS

WordpressThe WordPress blogging platform does support installing and updating your plugins using SSH/SFTP! So, why is it that it doesn’t show up as an option when updating your plugins?

Well, as it turns out, your PHP installation must be configured to support SSH before this option will show up in your WordPress dashboard. In this article I’ll explain how to set this up using just a few simple commands or a plugin.

Continue reading

Installing build-essentials in CentOS (make, gcc, gdb)

CentOS IconOnce upon a time I was a very avid desktop user of Ubuntu Linux. As a software developer, I would usually need the standard build tools installed on my machine.

Installing build tools in Debian/Ubuntu

In Debian/Ubuntu, you can install the typical build tools by installing the package build-essentials, which is just a pseudo-package that downloads all the popular development packages:

# apt-get install build-essentials

Installing build tools in CentOS

Since I prefer CentOS as my server platform, I also occasionally need to install packages using yum.

To install the common build tools using yum in CentOS you’ll want to install the group package “Development Tools”, which is similar to build-essentials in Debian/Ubuntu flavors of linux. You’ll probably also want to install kernel-devel and kernel-headers if they’re not already installed:

# yum groupinstall "Development Tools"
# yum install kernel-devel kernel-headers

This should give you a copy of make, gcc, gdb, and all those other tools you were looking for.

Happy hacking!

Using xargs for file and directory recursion

Terminal Occasionally I’m away from the bash shell for too long and I forget some things. This is just a quick reminder to myself regarding the syntax and usefulness of xargs.

Why?

xargs takes a file list as input and performs some arbitrary operation on it. This is obviously useful for scripting and bulk file operations. Why use xargs? Well, quite frankly, it’s faster than using find -exec because it splits the file list into sublists and calls the command once for every sublist instead of calling the command once for every file. Also, because it uses sublists you never run into problems when trying to run commands with very large argument lists (it is actually possible to run a command and have bash reply that the maximum line length is exceeded). Below is example usage.

Example: fixing permissions

Using xargs we can easily correct permissions recursively. For instance, let’s improve our web security by locking down write privileges on our directories and files to only the owner:

find . -type d -print0 | xargs -0 -I {} chmod 755 {}
find . -type f -print0 | xargs -0 -I {} chmod 644 {}

Here’s what’s happening:

  • we use find and -type d to locate all directories under the current directory
  • we use -print0 so find will print the directories and separate the directory names by null characters instead of newlines; delimiting this way improves xargs ability to handle special characters in file names
  • then we pipe this list to xargs and use -0 to tell xargs the list is null-character delimited
  • we also use -I {} to tell xargs to replace any occurrence of {} with each filename as it runs the following command
  • ultimately the command chmod 755 {} tells xargs to chmod 755 each directory

Of course we then do the same by running chmod 644 on all files under the current directory.

Efficiency

Using xargs this way is faster than running these equivalent find commands:

find . -type d -exec chmod 755 '{}' \;
find . -type f -exec chmod 644 '{}' \;

Using find this way will run chmod individually for each directory and file, which is less efficient. The general rule of thumb is to always opt for xargs.

You can use xargs for several other things, but this example is my most frequent use-case. Happy hacking!

How to pass the ‘Yii Requirement Checker’ in CentOS 5

YiiRecently I’ve been doing some PHP web development and I decided to check out the Yii Framework. They have a great 4-part screencast tutorial from Jeffery Winesett that gets you up and running fast and Yii looks really cool!

During the installation Yii uses a ‘Requirement Checker’ webpage that verifies you have the correct PHP version and the necessary plugins. If you’re like me, you like to see all your boxes turn green just to be sure you can get the full functionality out of your apps and frameworks instead of having to debug stuff like this later and going down a rabbit hole.

Install All The Packages

To make the best use of Yii (and several other things), you’ll need these packages: GD, mcrypt, MySQL, PDO, PEAR, APC, Memcache, PgSQL, SOAP, and XML. To get proper packages for these extensions on CentOS 5, I recommend adding the IUS Community Repository. Here’s a one-liner you can run with sudo or as root after you’ve set up that repo:

yum install php53u php53u-cli php53u-common php53u-devel php53u-gd php53u-mcrypt php53u-mysql php53u-pdo \
php53u-pear php53u-pecl-apc.x86_64 php53u-pecl-memcache.x86_64 php53u-pgsql.x86_64 php53u-soap.x86_64 php53u-xml.x86_64

Install all the packages

Have fun with Yii!

Installing Flash in BackTrack x64

Adobe FlashFlash doesn’t work out-of-the-box on the 64-bit version of BackTrack 5. Unfortunately this just won’t do since Nessus uses a Flash web interface! Luckily it’s easy enough to install it.

Pre-installation

First close Firefox! It’s safer to perform these instructions with all browsers closed.

Next, remove any existing Flash installations or files:

# apt-get -y purge flashplugin-nonfree flashplugin-installer gnash gnash-common mozilla-plugin-gnash swfdec-mozilla
# rm -f /usr/lib/firefox/plugins/libflashplayer.so
# rm -f /usr/lib/mozilla/plugins/libflashplayer.so
# rm -f /usr/lib/mozilla/plugins/flashplugin-alternative.so
# rm -f /usr/lib/mozilla/plugins/npwrapper*flash*so
# rm -f ~/.mozilla/plugins/*flash*so

Installation

Now download Flash.

Finally, extract and install the Flash player plugin you just downloaded:

# tar xvfz install_flash_player_10_linux.tar.gz
# mkdir -p ~/.mozilla/plugins
# mv -f libflashplayer.so ~/.mozilla/plugins/

Start up Firefox and you should have a working Flash installation! You can visit Adobe’s website to be sure it’s installed correctly.

Adobe Flash Installed

Have fun using Nessus and enjoying the rest of the internet. :)

How to Download Recursively from FTP at Command Line (mget and wget)

TerminalSometimes I want to download recursively from an FTP server and I don’t want to leave the command line. Depending on your FTP client, you may be lucky enough to have an option built-in for this but in many cases you don’t. FTP clients often feature mget, which is good for downloading files based on a glob filter such as *.txt or *.c. But still, this isn’t recursive.

Wget

Surprise! Wget supports FTP and can download recursively! You could download the full contents of an FTP server like this:

wget -r 'ftp://user:pass@host'

Or just grab a single directory like this:

wget -r 'ftp://user:pass@host/dir'

Cool, huh?

How to fix Error: suffix or operands invalid for ‘push’ or ‘pop’

TerminalIf you compile a 32-bit assembly program (or a C program that contains assembly instructions) on a 64-bit machine you may see the following error:

# as -o example32bit.o example32bit.s
example32bit.s: Assembler messages:
example32bit.s:10: Error: suffix or operands invalid for `pop'

To fix this problem you need to pass the compiler or assembler a flag telling it you want to compile the software as 32-bit and not 64-bit.

The fix for C: Compiling with the -m32 flag

If you’re using gcc to compile a C program, pass in -m32. There’s a complete example here on my blog.

The fix for assembly: Compiling with the –32 flag

If you’re using as to compile an assembly program, pass in –32. Then when you link with ld pass in -m elf_i386. There’s a complete example here on my blog.

Happy hacking!