Scripting the Unscriptable

October 11th, 2006

I’ve been looking for different ways to send keystrokes, mouse movements and clicks to GUI applications. Under Windows, this is straight forward with AutoIt. The BASIC-like language allows you to read files, start applications, send keystrokes move the mouse etc. A nice feature is to wait for certain dialogs, for example:

; wait for tightvnc window
WinWaitActive("New TightVNC Connection")
; send hostname and port number
Send($hostname & ":" & $displaynumber)

This makes sure that the keystrokes actually end up in the right window. Mouse coordinates can be specified relative to the application window. Using these features allows you to write ‘reasonably’ robust code. But don’t expect your AutoIt script to run flawlessly on other PC’s. There are a lot of dependencies, like dialogs that look different in another version. Internationalization might cause the window title to read “Nieuwe TightVNC Verbinding” (Dutch) and the above code snippet will fail. Even user preferences like font sizes, window themes and screen resolution might result in unexpected behavior.

Anyway, it’s just fun to figure out which sequences to send to an application and let it process a batch of documents!
Ok, this works pretty nice under windows. But what about my favorite platform?
Read the rest of this entry »

Seder’s grab bag

October 3rd, 2006

I was already very pleased with the sed one-liners, but today I found grab bag. A fully loaded bag of sed scripts, tutorials, etc.

Why didn’t I check http://sed.sourceforge.net before???

Does ‘mac os x sshfs’ exist?

September 28th, 2006

Answer
YES!!!!
Thanks for letting me know!
The project is called MacFuse and its hosted by google code.
For PPC users, there’s a binary, but there are also instructions to build it yourself.

Installation instructions:

$ wget http://macfuse.googlecode.com/files/fuse-binaries-0.1.0b006.tar.bz2
$ sudo tar xjf fuse-binaries-0.1.0b006.tar.bz2 -C /
$ echo "export PATH=$PATH:/usr/local/bin" >> /etc/profile

Mounting goes as follows, make sure that you first create a mount point (empty directory):

$ mkdir -p ~/mnt/host
$ sshfs host:remote_directory ~/mnt/host -oping_diskarb,volname=hostname

The first time you do this, the sshfs command will report something like:

fusefs.kext loaded successfully

That’s it, have fun!
Unfortunately not!
If it does, please let me know!
Until then, maybe nfs tunneling over ssh would be an alternative?

addsshuser

September 27th, 2006

When creating user accounts, it is tempting to use a default password and ask the user to change it right after logging in the first time. Unless the user is forced to do this by means of a password policy, most users are just happy with the supplied password.
I know about a company (a hospital!) that uses one and the same password for new email accounts, allowing (ex)colleagues to read each others’ email!

A nice utility to generate random passwords is pwgen. The passwords are semi-random: random enough to be safe, but not too difficult to remember. By default, pwgen presents you a list from which you can choose:

$ pwgen
Vahvoi5u Da5ahrah Ogo0voh0 Fo3rieji aiw5zeuR qui1aiYo
thae5Chu Ha8bohSu oa2Eu3go JooQuia8 Aroh7Ohc UCoo4Hah
Woocee1i Ahnein6s di6aGeph fi5Ahngu ooRe8eeW Aish0sha
aikei3Fa Abohhai3 aeNgoo6o yiw2keiS eiyo5geH To5we6ey

… and 16 more lines. The man page explains that this is done so that you can pick a password without being afraid of someone ‘shoulder surfing’ for the chosen password.
Used from within a script (non-interactively), pwgen will generate a single password:

$ pwgen | wc
      1       1       9

Using pwgen to create a new user would involve a number of steps:

  1. run pwgen and write down the generated password
  2. create user account
  3. hand over the note with the password
  4. instruct the user how to log in and refer to the documentation about the service

You can argue about weather it is safe to write down the password, that’s another discussion.

I’ve written a little script that combines these steps.
The most interesting part of the script is the way that the generated password is fed in to the useradd command. The useradd option -p allows you to supply the encrypted password. “…, as returned by crypt(3)”, the man-page says.
Well, that brings up a problem. Isn’t there a binary or built-in equivalent? I would rather see crypt(1) being referenced… but that tool doesn’t exist.
This is where openssl comes to help! It can generate a passwd style hash for a given password. For example, to generate a hash for ‘mypassword’, execute:

$ openssl passwd -1 mypassword
$1$xBN626Gk$vsK88ODo4CfQ3pBx9Z1vD0

This looks like something that we can feed to useradd!!

The minimalist version of the script now looks like:

#!/bin/sh

# first argument is the username
USER=$1

# generate password
PASSWORD=`pwgen -cn -1`

# generate password hash
PW_HASH=`openssl passwd -1 ${PASSWORD}`

# create the user account
useradd -M -p ${PW_HASH} ${USER}

# display username and password
echo Account created:
echo Username: ${USER}
echo Password: ${PASSWORD}

The full script adds some ‘fancy features’ like checking the input argument and printing a receipt with the username and password and a link to the documentation.
I’m using the script to add accounts to a server that acts as a SSH gateway. There’s no need for home directories on this computer. By using the -M option for useradd, no home directory will be created.
Maybe I should write my next Nerdnote(tm) about setting up an SSH server to use as a secure gateway to a private network.

Vncshare

August 23rd, 2006

I just wrote vncshare, a script that allows you to temporary share your vnc session. It changes the vnc password to ‘helpme’ for 30 seconds. Then it gets reverted to the original one, giving your colleague a time window to login to your vnc session. The script makes use of the ‘trap’ command to make sure the password is always reverted when the script is aborted.

Hoe doe je ‘alt print screen in mac’?

July 19th, 2006

Antwoord: Zie de nerd note over screen shots.

What is the ‘ubuntu live cd root password’?

July 15th, 2006

Answer 1: It is not set but you can set it:

ubuntu@ubuntu:~$ sudo passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
ubuntu@ubuntu:~$
[/courcecode]
<b>Answer 2:</b> It is not set but you don't need it:
1
ubuntu@ubuntu:~$ sudo cat /etc/shadow

Relevant nerd notes: Sudo and sshfs demo.

Combine PDFs

June 27th, 2006

Try the following Gostscript one-liner:

$ gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=combined.pdf article1.pdf article2.pdf notes.pdf

Typo3update

June 25th, 2006

Typo3update is designed to directly update content to a Typo3 website from the command line. It works as a network service and can be used from any machine that can connect to the server running the Typo3 website. For example, quota overviews and cluster news information that is available from command line programs or text files can be updated to Typo3 using simple bash scripts. Typo3update takes the actual content from standard input. Additionally, it requies two command line options: uid and the header.
Have a look at the following example:

$ cat textfile | typo3update 9533 "About"

This will insert the contents of textfile into content element 9533 that has the heading “About”. The heading parameter is to double check the content element id. If the id doesn’t match the supplied heading, typo3update refuses to update (overwrite) the content. Instead, it writes a line to an error log file.
The content element should be created first from the Typo3 backend. To identify the uid of the content element in a certain page, just hover your mouse pointer above the content element icon. The uid will show up in a tool-tip:
Typo3 uid
Read the rest of this entry »

Redirecting stdout & stderr notes

June 25th, 2006

Redirect stdout to stderr:

$ echo Oops, something went wrong >&2

Redirect stderr to /dev/null:

$ ls /home /qwerty 2> /dev/null
home:joe john

To get rid off all output:

$ ls /home /qwerty >/dev/null 2>/dev/null
# or
$ ls /home /qwerty >/dev/null 2>&1