Author Archive

View string including special characters like newlines in Python

After spending a frustrating hour or two trying to format a string, i stumbled on ’s repr() funcion. repr() allows you to inspect a string object when you’re troubleshooting your code. For example:

I was trying to understand a string that was shown in the terminal like this:

print mystring
Node - 192.168.1.104
ERROR:
Description = Generic failure

Seems pretty simple to remove the extra right? no. doing a string.replace(“\n”, ” “) did not fix it, but actually mangled the string. So here comes repr() to the rescue…

print repr(mystring)
'Node - 192.168.1.104\r\r\nERROR:\r\r\nDescription = Generic failure\r\r\n'

\r\r\n… i’ve never heard of it before, but knowing that it’s there, i could easily change my replace to:

print mystring.replace("\r\r\n", " ")
Node - 192.168.1.104 ERROR: Description = Generic failure 

Pstools: Access Denied in a Domain Environment

After upgrading to a 7 VM at work, I was having trouble getting commands to authenticate on remote machines. After much trial and error, I realized some curious behaviour with . Obviously, when connecting to a remote machine, I would try to use the ‘-u’ switch to specify my administrative account, but would always get ‘access is denied’. Of course, all of the normal things should be checked: simple sharing turned off, $ADMIN share working… you know…

The issue was apparently that if I log into my workstation as a non-administrative user, but try to issue pstools commands as an administrator,  it fails because Microsoft wants me to log into my workstation and work logged with my domain admin account.

Take a look at this example using psexec:
On windows 7, running ‘cmd’ as your non-admin user, if you type in the command:

c:\Program Files (x86)\PsTools>psexec -u domain\domainadmin \\targetmachine cmd

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

Password:
Could not start PsExec service on targetmachine:
Access is denied.

If you:

  1. Hit start
  2. Type “cmd”
  3. Hold down ‘shift’ and right-click on the ‘cmd’ in the start menu
  4. Select ‘run as different user’.
  5. Type in your administrative credentials. Use the same ones you will use in the psexec command.

Now you should have your command line window open. If you run the same command as earlier:

c:\Program Files (x86)\PsTools>psexec -u domain\domainadmin \\targetmachine cmd

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

Password:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:\Windows\system32>

Now you’re in!

ntpq timed out on freeBSD

I was running locally on a server and wanted to check in on ’s status. ntpq -p was not producing any output even though was running.

# ntpq -p
localhost: timed out, nothing received
***Request timed out

This is because I had the option “restrict default ignore” set in /etc/.conf. ntpq. This makes ntpd ignore EVERYTHING, even queries to the loopback interface. Ntpq queries ntpd over the loopback interface at 127.0.0.1. To allow these local queries, add:

 restrict 127.0.0.1

to /etc/ntp.conf, then also add restrict lines for your other upstream ntp servers.

restart ntpd:

/etc/rc.d/ntpd restart

Now ntpq -p will show you status of it’s peers

# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 dns3.untangle.c .INIT.          16 u    -   64    0    0.000    0.000   0.000
 mirror          .INIT.          16 u    -   64    0    0.000    0.000   0.000
 153.16.4.133    .INIT.          16 u    -   64    0    0.000    0.000   0.000

Cannot delete ZFS snapshot

While Deleting to delete a , i typed:

zpool destroy zfsdiskpool@Sat

and got:

cannot open 'zfsdiskpool@Sat':  '@' in pool name

This is because to delete snapshots in , you should use ‘ destroy’ instead of ‘zpool destroy’. DOH!

zfs destroy zfsdiskpool@Sat

Script to run handbrake recursively through a folder tree

is a fantastic tool for (among other uses), converting videos into mobile formats. It is extremely easy to use and can usually get a video properly converted in just a few clicks. To get Videos encoded for use on my phone, I use the “iPhone & iPod Touch” preset. It will convert your video to .mp4 as well as scale it down to a consumable size. This format will work on any smartphone I’ve seen.

My issue is that when converting a folder full of videos, I don’t want to have to use the gui to add a bunch of videos to the queue one by one. Its a very clicky process. This simple bash will walk handbrake through all the files in a selected folder (and its subfolders). This uses the find command to traverse recursively through a directory structure. It will place the transcoded file in the same folder as its source and change its extension to “.mp4″.

Usage: handbrakefolder.sh [FOLDER]
Run handbrake on all the files contained in [FOLDER]. (the current directory by default)

#!/bin/bash
#
# Change this to specify a different handbrake preset. You can list them by running: "HandBrakeCLI --preset-list"
#
PRESET="iPhone & iPod Touch"
if [ -z "$1" ] ; then
	TRANSCODEDIR="."
else
TRANSCODEDIR="$1"
fi
find "$TRANSCODEDIR"/* -type f -exec bash -c 'HandBrakeCLI -i "$1" -o "${1%\.*}".mp4 --preset="$PRESET"' __ {} \;

Save that into a .sh file like “handbrakefolder.sh” and grant it the execute permission (chmod +x handbrakefolder.sh).

Thanks to Vinnie and http://mywiki.wooledge.org/UsingFind for their help in this.

Now you can simply execute this script against a folder containing your video files. like this:


For a folder structure like this:
Videos
-> show1
    vid1.avi
-> show2
    -> season 1
        ep1.avi
        ep2.avi
    -> season 2
        ep1.avi
        ep2.avi
-> show3
    vid1.avi
    vid2.avi

hostname% handbrakefolder.sh Videos
...
....
...
When done, it will look like this:
Videos
-> show1
    vid1.avi
    vid1.mp4
-> show2
    -> season 1
        ep1.avi
        ep1.mp4
        ep2.avi
        ep2.mp4
    -> season 2
        ep1.avi
        ep1.mp4
        ep2.avi
        ep2.mp4
-> show3
    vid1.avi
    vid1.mp4
    vid2.avi
    vid2.mp4