Posts Tagged sourcecode

How to fix mangled HTML in WordPress posts using SQL

There are quite a few ways that source code and other preformatted text can get mangled in WordPress, such as a rich-text editor, or even by the export/import process.

When I speak of ‘mangled’, I mean unnecessarily HTML-encoded. Left and right angle brackets (the < and > signs), quotation marks (the ” symbol), and ampersands (the & symbol) get HTML-encoded to &lt;, &rt;, &quot;, and &amp;. This can seriously mangle sourcecode, as well as making other text seriously ugly.

It can become a huge time waste to try to go back and edit all your affected posts one at a time to fix mangled code, even with search-and-replace.

Instead, there’s an easy and straight-forward way to do it in SQL, that you can execute from phpMyAdmin, MySQL Workbench, or whatever you fancy.

Simply run each of the following queries on your WordPress database, and feel free to edit them as you like.

update wp_posts set post_content = replace(post_content, '&quot;', '"');
update wp_posts set post_content = replace(post_content, '&gt;', '>');
update wp_posts set post_content = replace(post_content, '&lt;', '<');

Have any others to suggest? Please feel free to do so in the comments below. Thanks!

, , ,

Leave a comment

How to install wget in Mac OS X

wget is a really handy command line utility, but unfortunately not included in OS X. Curl could be a suitable replacement, but frequently scripts are written with wget, and it can be difficult and time-consuming to convert them to using curl.

Users interested in installing wget should first install Homebrew and then run:

brew install wget

This will install wget from Homebrew.

The below steps are deprecated and likely no longer work at all:

Below are the steps required to install a working wget on Mac OS X. This has been tested on OS X 10.6 Lion.

Install XCode from (at this time, it’s a 1.5GB download.)

Launch XCode, updating if necessary.

Go to Preferences > Downloads, and install Command Line Tools

Now open a terminal and perform the following steps at the command line one at a time to download, extract, configure, compile, and install wget:

curl -O
tar xvzf wget-1.14.tar.gz
cd wget-1.14
./configure --with-ssl=openssl
sudo make install

You should now have a working wget installed in /usr/local/bin. Confirm by trying

$ wget
wget: missing URL
Usage: wget [OPTION]... [URL]...
Try `wget --help' for more options.

Feel free to comment below. Thanks!

2/7/2016: I got an email from someone who says this no longer works and gives the following message:

configure: error: –with-ssl=openssl was given, but SSL is not available.

If anyone has advice, please contact me. Thanks!


, , , ,

Leave a comment

Simple Linux BASH script to locate and delete duplicate photos

This is a quick bash script I wrote to walk through directories and delete duplicate photos based on MD5.

It was written for a specific scenario and I highly advise against using it as-is. Instead, read through it and tweak it to your own situation. I would appreciate any feedback.

echo HF=$HF
rm "$HF"
touch "$HF"
for x in `ls -bd *`; do
 echo x=$x
 cd $x
 for y in *; do
 m=`md5sum "$y" | awk -F '{print $1}'`
 echo $y: $m
 g=`grep $m "$HF"`
 echo g=$g
 if [[ "$g" != "" ]]; then
 echo "MATCH!!";
 echo rm "$y"
 echo "no match"
 echo $m >> "$HF"
 cd ..

, ,

Leave a comment

How to script a change to a static IP address or DHCP IP address in Windows using a batch file

More and more I find myself needing to temporarily set static IPs on my laptop to access equipment for configuration or management, and changing IP addressing in Windows is easy enough, but I often either forget the settings I need or need to switch back and forth quickly and it becomes veryb time-consuming or tedious.

After searching around for a way to do this I found that scripting the IP changes via netsh in a batch file was a great way to not only change the IPs quickly and easily, but relieve myself from having to remember the IP addresses — I could simply save each configuration in a seperate batch file and name it appropriately. I could then switch back and forth quickly.

So here’s how to do it:

First, you’re going to need to know the basics of creating a batch file. If you’re not sure how to do it, click here to read how; it’s actually quick and easy.

Next, I’m going to show you some examples and the parameters, and then further below there will be a template batch file you can copy and easily edit.

Syntax of setting IP address:

netsh int ipv4 set address name=<interface name> source=<'static' | 'dhcp'> [address=<IP address[/bitmask]>] [mask=<netmask>] [gateway=<gateway>]

Syntax of setting DNS server address:

netsh int ipv4 set dns name=<interface name> source=<'static' | 'dhcp'> [address=<address | 'none'>] [register=<'none' | 'primary' | 'both'>] [validate=<'yes' | 'no'>]

Here’s a short run-down of what the various parameters mean. For further reading, consult the Microsoft Technet article on netsh.

Frequently used ‘address’ parameters are…

  • name = Interface name or index.
    (If you’re setting this up on a wired LAN connection, it’s usually “Local Area Connection” (not case sensitive), check control panel if you’re unsure.)
  • source = ‘dhcp’ or ‘static’
  • address = IP address, optionally the subnet prefix length192.168.1.1 or
  • mask = The IP subnet mask for the specified IP address
    (use subnet prefix length in address -or- mask, not both)
  • gateway (optional) = ; or ‘none’ (default)

Frequently used ‘dns’ parameters….

  • name = The name or index of the interface. (See note above)
  • source = ‘dhcp’ or ‘static’
  • address = IP address or ‘none’
  • register = One of the following values:
  • none: Disables Dynamic DNS registration.
  • primary: Register under the primary DNS suffix only.
  • both: Register under both the primary DNS suffix, as well as under the connection-specific suffix.
  • validate = Specifies whether validation of the DNS server setting will be performed. The value is yes by default. (Passing ‘no’ is suggested to prevent scripting issues)

The following are a few examples of how to set addresses and DNS servers. Feel free to use them or change them however you like.

Example to set static IP, mask, and gateway address:

netsh int ipv4 set address name="Local Area Connection" source=static address= mask= gateway=

An example to set static DNS (you may have to use this in conjunction with the above)

netsh int ipv4 set dns name="Local Area Connection" source=static address= register=primary validate=no

So if you needed, you could combine the above two commands into a single batch file, like so:

netsh int ipv4 set address name="Local Area Connection" source=static address= mask= gateway=
netsh int ipv4 set dns name="Local Area Connection" source=static address= register=primary validate=no

Important: If you are using this on a Windows XP or another system without iPv6 support, change ‘ipv4’ to ‘ip’. Also, make sure your cable is unplugged before setting a static IP, else the command will freeze.

To set both address and DNS to DHCP:

netsh interface ipv4 set address name="Local Area Connection" source=dhcp
netsh interface ipv4 set dnsservers name="Local Area Connection" source=dhcp

Important: There is a confusing disconnect between netsh setting DHCP and what is reported in the network connection properties. If you set a static IP, then later set DHCP, the static IP will still appear in the network connection properties dialog. However, once you plug into a DHCP-enabled network, your network adapter will configure properly. It doesn’t look like it will work, but it does.

If you want to make easy-to-use static IP scripts, create some batch files with helpful prompts like the following:

@echo Be sure NIC cable is unplugged and
netsh interface ipv4 set address name="Local Area Connection" source=static address= mask=

An example of a user-friendly DHCP script

netsh interface ipv4 set address name="Local Area Connection" source=dhcp
netsh interface ipv4 set dnsservers name="Local Area Connection" source=dhcp
@echo It may take a few moments for changes to take effect. You may close this window or

Important: In order for any batch file changes to work, you MUST right-click and ‘Run as administrator’ on Windows Vista and Windows 7.

If you have any comments or feedback from the above, please feel free to share in the comments section below. Thank you!

, ,

Leave a comment

OpenVPN Connection to Synology NAS on Windows 7

Initially I had some trouble getting this to work, but figured this out and figured I would pass it on.

This guide assumes you are attempting to set up a VPN tunnel to your Synology NAS over WAN using OpenVPN. While a PPTP VPN connection is much easier to set up and doesn’t require third-party software, OpenVPN has been shown to be signifigantly more secure.

For this, I’m using Windows 7 64-bit. While file locations will likely differ on other OSes, the overall configuration is likely smiliar.

Base configuration

Log in to your Synology NAS using the admin account and install the OpenVPN server from within Package Center.

Once installed, start VPN Server and enable OpenVPN under OpenVPN Server > Settings > OpenVPN.

The following pop-up message will appear instructing you to make sure UDP port 1194 is open:

If your NAS is behind a router, make sure you have port forwarding set up to forward UDP port 1194 to your NAS.

If you are using the Synology Router Config tool, you can set the port forwarding from Control Panel > Router Configuration > Create. You’ll find the port setting under Built-In Applications as shown below:

If you’re setting up port forwarding in your router, then the Synology Router Configuration tool isn’t needed. Use one or the other, whichever you prefer.

Install OpenVPN

Download and install the OpenVPN application for your OS from OpenVPN community downloads. Install using the defaults.

Getting the configuration from the Synology OpenVPN server

Before the client software can be configured, a few files (specifically the OpenVPN configuration files and the certificate) need to be downloaded from the Synology NAS. from the NAS, go to OpenVPN Server > OpenVPN and click on Export Configuration. This will download a zip file containing the two needed files plus a third README file. You can either refer to the README for instructions or simply continue reading.

Configure the OpenVPN client software

Open windows explorer and navigate to “C:Program Files (x86)OpenVPNconfig”. Copy the openvpn.opvn and ca.crt files from the file you downloaded earlier to this directory.

Right-click on openvpn.ovpn and open it with notepad (or your favorite text editor) and make the edits explained below:

Change the line starting with remote to specify your or your server’s IP address or hostname. For example, if your OpenVPN server is at, change it as follows:

remote 1194

If your host’s IP address frequently changes, uncomment the float option, by changing




Or, you can specify an IP address, like so:

remote 1194

Also, if you want to redirect ALL traffic across the OpenVPN connection (strongly recommended), uncomment the redirect-gateway option by changing




Connecting to the OpenVPN Server

Right-click the OpenVPN GUI desktop icon and select “Run as administrator”. (You can edit the shortcut to always start with administrative privileges by right-clicking on it, selecting Properties, then Compatibility, then checking Run this program as an administrator.)

The OpenVPN GUI icon will appear in your taskbar, and it will appear red. Right-click on it and select Connect. You will be prompted for your username and password (as used on your Synology NAS) to connect.

If you’re having trouble authenticating make the account you are trying to connect as has access to the VPN server. Look in VPN Server > Privilege to verify account access.

That’s it! You should have a working OpenVPN tunnel connection working after following these steps. If you have any suggestions, comments, or feedback, or just want to share your thoughts, please do it in the comments section below. Thanks!

, , ,

Leave a comment

How to delete all Windows Event logs at once

When troubleshooting Windows it’s often very helpful to start with clean log files. This shows you what’s happening with a system in the moment and you don’t waste time digging through log files and waiting for them to load.

To clear all Windows events from the logs, simply open an administrator-level command prompt and enter:

for /f %x in ('wevtutil el') do wevtutil cl "%x"

After a few moments all of the log files will be cleared.

Batch file use: If you’re using this in a batch file, you need to add an extra % sign to the variable names. Also, make sure you’re running the batch file as an administrator or you will get a lot of access denied messages. %x is valid on the command line only; it needs to be %%x in batch file. Here’s the updated code:

for /f %%x in ('wevtutil el') do wevtutil cl "%%x"

Thanks to stefan for bringing this up in the comments.



Leave a comment

How to retrieve EXIF data from the command line in Ubuntu Linux

If you want to make use of the EXIF data stored in a JPEG (.jpg or .jpeg) or TIFF (.tif or .tiff) file from the command line in Ubuntu Linux — or most other Linux variants — here’s how.

This was tested on Ubuntu 11.10 (Oneiric Ocelot).

First, you will need to have imagemagick installed to provide the identify command.

sudo apt-get install imagemagick

Next, you can retrieve the data on an image with the following example:

identify -verbose /usr/share/backgrounds/WildWheat_by_Brian_Burt.jpg

If you want just the EXIF data, you can use something like the following:

identify -verbose /usr/share/backgrounds/WildWheat_by_Brian_Burt.jpg | grep "exif:"

, , ,

Leave a comment

Set up an encrypted VPN using DD-WRT

DD-WRT is feature-rich alternative firmware for a large number of home router models. It adds a wonderful array of new features, VPN being one of them. This walkthrough will show you how to quickly and easily configure a PPTP VPN server on your DD-WRT-powered router, so you can connect to your home network from afar, create a secure tunnel so you can safely use a public Wifi point with your laptop, or secure your iOS or Android device.

Setting up the VPN Server

So here’s how to get started. First, you’ll need a build of DD-WRT supported by your router which includes the VPN software. If you’re doing this on an Internet connection which has an IP address that changes periodically (i.e. residential), you’ll likely want a Free DynDNS hostname to point to your IP address. You’ll also need a basic familiarity of networking.

For the remainder of this guide, I will assume your router’s internal (LAN) IP address is

Start by going to and login to your router’s administration panel.

Go to Services > VPN and set PPTP Server to enable. After doing that, a few new options will appear. The only ones you need to set are Server IP, Client IP(s), and CHAP Secrets. Set them as follows:

Server IP: You can set this to your router’s LAN IP, i.e.

Client IPs: Set this to an IP range OUTSIDE your DHCP range (See Setup > Basic Setup to figure your DHCP range) A good example value would be for clients to receive addresses within that range.

CHAP Secrets: This is the username/password combinations for your VPN clients. Format is:
myname * mypassword *

Neither the username nor password can contain spaces, and must be all-lowercase.

You’re done with this page; Click Apply Settings.

Now go to Security > VPN Passthrough and make sure PPTP is set to Enabled. Click Apply Settings if you had to change the setting.

You should now be able to connect to your VPN using your Windows, Mac, or Linux computer by setting up a PPTP connection to your public (WAN) IP or hostname.


Can’t get connected? First, try setting up your connection to the router itself, using the LAN IP ( If that works, then the VPN server is set up correctly; the problem is likely on the WAN side. Keep reading for suggestions. If you weren’t able to get connected, go back to the top and double-check your settings.

iOS-Specific changes

You may need to make the following settings adjustment if you are having trouble connecting specifically from your iOS device running iOS 4.3 or above. Go to Administration > Commands and paste the following in the box. Click Save Startup.

echo "nopcomp" >> /tmp/pptpd/options.pptpd
echo "noaccomp" >> /tmp/pptpd/options.pptpd
kill `ps | grep pptp | cut -d ' ' -f 1`
pptpd -c /tmp/pptpd/pptpd.conf -o /tmp/pptpd/options.pptpd

(Source: DD-WRT Wiki)

If you can connect from the LAN side, but are still having trouble connecting from the WAN side, it’s likely your ISP or your gateway device (modem) is blocking the needed GRE protocol or the needed PPTP port or traffic. Contact your ISP for further assistance.

Do you have any experience or tips to share regarding VPN connections to a DD-WRT-powered router, or any suggestions in addition to the above? Please feel free to share them in the comments below. Thank you!

, , , , , , , , , ,

Leave a comment

Bash script to automatically sort photos into folders based on EXIF data for Ubuntu Linux

If you — like me — take a lot of digital pictures, you probably have a hundred folders full of images on your hard drive or external drives, and not nearly as sorted as you would like them to be. you have probably gotten to the point that you don’t know what’s in them or can’t find an image when you’re looking. I had around 10,000 images in over a dozen folders spanning 5+ years, and I had no intention of even trying to sort them manually :) So I wrote this script.

The following script was written in bash on Ubuntu Linux and automatically sorts your images into directories based on the date and time the photo was taken. How does it do this? By making use of the EXIF data your digital camera stores inside the image. The date and time the photo was taken is stored in that EXIF data. When an image doesn’t have EXIF data (such as when it was downloaded from the Internet, or taken from a camera that doesn’t support adding EXIF data), it will use the files last-modified time.

First, this should be run in the top-most directory of wherever your pictures are stored. If you have pictures/foldername/somepics/ and pictures/anotherfolder/morepics, run it from your pictures/ directory.

There are quite a few opportunities to improve this script — and some cautionary notes as well — marked within the script with FIXME tags. I’m already finished sorting my images, but anyone is welcome to contribute suggestions and improvements, which I’ll look into incorporating the next time I’m using this. You are welcome to include any suggestions or code improvements in the comments below using <code> and/or <pre> tags.
Usage: Copy the script into a file, editing options where appropriate, and save it. Make it executable and run it from the command line or window, from the directory where your pictures are stored. No command-line arguments. Back up your stuff first :)

UPDATE: Because WordPress keeps mangling this code, it has been moved to github, here.

Questions, comments, or feedback can be left in the comments below, or please use the contact form. Thank you!

, , , ,

Leave a comment

How to enable BitLocker TPM+PIN after encrypting hard drive

BitLocker by itself is great drive encryption, but unfortunately it has some shortcomings in its default configuration. Namely, there’s no safeguard at boot time preventing the drive from being accessed. If your computer is stolen or physically compromised, the drive is ready and willing to give access to your data.

Fortunately BitLocker supports a PIN code which would can be required to be entered at boot time to unlock the drive. To enable the BitLocker PIN, simply open an administrator-level command prompt and run the following:

manage-bde -protectors -add c: -TPMAndPIN

You should receive output similiar to the following, during which you’re prompted for your PIN (no confirmation of keystrokes will appear on the screen during PIN entry):

BitLocker Drive Encryption: Configuration Tool version 6.1.7601
 Copyright (C) Microsoft Corporation. All rights reserved.
Type the PIN to use to protect the volume:
 Confirm the PIN by typing it again:
 Key Protectors Added:

If you get the following error…

ERROR: An error occurred (code 0x80310060):
Group Policy settings do not permit the use of a PIN at startup. Please choose a
different BitLocker startup option.

… then you will need to edit the local computer policy to allow a PIN to be set by performing the following steps:

  1. Click Start > Run and type mmc
  2. If Local Computer Policy is not visible, or Group Policy Object is not already added, add it by going to File > Add/Remove Snap-In > Group Policy Object
  3. Browse to Local Computer Policy > Computer Configuration > Administrative Templates > Windows Components > Bitlocker Drive Encryption > Operating System Drives
  4. Open the key Require additional authentication at startup
  5. Enable that Key and set Configure TPM startup Pin to Require startup PIN with TPM

Now you can set the PIN by running the manage-bde command once more.

, , ,

Leave a comment