hasenj blog

modifying plink to work with unison

Posted in hacking, webdev by hasen on 19/12/2008

Last time I talked about how to make unison work on windows with ssh.

Well, there was still one problem that remained unsolved, that is, password input.

I thought about the issue and figured I could hack plink’s source code to make the modifications necessary to make it work like unison expects it to.

The changes are simple:

  • make it accept -e none
  • make it print the password prompt to stderr instead of stdout

in winsoncs.h change hin and hout, around line 325:

    hout = GetStdHandle(STD_ERROR_HANDLE);

and in cmdline.c around line 438:

    //ignore "-e" option! (for unison compatibility)
    if( !strcmp(p,"-e") )

It’s not heavy tested, and frankly it’s a hack in a sense, I didn’t study plink’s source code in any kind of depth! Who knows, maybe my changes break the behavior in some (at least subtle) ways, but it works for me!

here is the result

This is not a wrapper; it’s a modified plink, and we call it ssh.exe so that it integrates with unison.

Integrating Jinja2 with django

Posted in webdev by hasen on 12/11/2008

(I have this posted on django snippets: http://www.djangosnippets.org/snippets/1112/ and it’s where you can view the code and download it)

Jinja2 is an alternative template system that can be plugged into django. It offers greator flexibility in presentation logic; if you find the django template system too restrictive, you should have a look at Jinja2 (The syntax is very similar).

In Jinja, you don’t need costum tags (most of the time), because you can call functions and pass them parameters too! The only problem is that you cannot “load” functions from the template, this “loading” must be done by the code that renders the template. Same goes for filters and tests.

If you need only two or three functions/filters, no problem, you can manually add them to the Environment object; but this method doesn’t really scale with real-life webapps.

This module/snippet offers a solution by allowing the user to define application-specific functions and filters and load them into the jinja2 environment automatically.
It also sets auto_escaping on (Jinja2 has it off by default).
It searches for templates in TEMPLATE_DIRS and also in the “templates” subfolder (if it exists) of each application folder.

The approach I’ve taken to specify functions and filters is like this:

To add test functions, put them in a file called “jtests.py” and let each function start with “is_test_name”; it will be available as “test_name” in your jinja2 environment, so you can test {% if var is test_name %}

The same idea applies for filters, put them in a file called “jfilters.py” and let each one start with “filter_name”, and it will be available as “name” in the environment, so you can say “{{var|name}}”

For functions, it’s a little different; you add them a file called “jtemp.py” (temp is my wacky abbreviation of template global functions), but they are put in a name space that’s named after the application. so if your application is called “app”, then any function you put in jtemp.py will be avaiable in the template environment as {{ app.function_name() }}

Here’s how to use this:

  1. Install Jinja2 (for a quick & dirty installation, you can just put the jinja2 folder in a folder that’s in PYTHONPATH)
  2. Save this python module somewhere and call it something meaningful (I have it as jinja.py in my project directory)
  3. Whenever you need to respond (an HttpResponse) with a template that’s rendered by jinja, import this module and call return jrespond( template_name, context )
  4. Any filters or functions you define in any of your applications will be readily available inside the template (see the documentation in code)
Tagged with: ,

unison on windows (ssh on windows)

Posted in webdev by hasen on 12/11/2008

[Update: this post discusses some outdated techniques, please see this newer post for a better technique to use ssh with unison on windows]

I’m running windows vista on a laptop, and need to synchronize files with a linux server (for a web development project). After googling for a while, I found two options: rsync and unison. rsync requires cygwin, and it’s for (uni-directional) mirroring. unison on the other hand does not require cygwin, and is designed for (bi-directional) synchronization. so I chose unison.

Unison seems easy enough to use, check out the tutorial on its manual page.

but I quickly discovered that it doesn’t really work on windows out of the box: it needs “ssh”, or an ssh-like tool to be present on the machine, and guess what, ssh requires cygwin!

Now, there’s “plink” which behaves pretty much like ssh, except for some subtle differences, but these “subtle” differences render it unusable with unison. The problem is that unison sends some command-line options to ssh that plink can’t understand.

I found this discussion page on google, and a solution is offered in the form of a bat file that removes the extra command-line arguments before passing the rest to plink


So you can save this file somewhere and use the -sshcmd option to make unison use this file as if it was “ssh”.

A problem arises when you connect to the server and you are prompted to enter your password: unison chocks and says “the ssh server is responding with something that I don’t understand, so I’ll quit”!! You’d expect a program that’s designed to connect to ssh servers and sync files with them to be smart enough to handle the password prompt; but then again, we’re using plink, where as unison expects “ssh”, and I guess this is also one of the “subtle” differences between ssh and plink.

so as a way around that we could pass the password on the command line using -sshargs option.
see, plink has this option “-pw password” which has the nice effect of allowing you to bypass the “password prompt”, and we can use it for this purpose. pass this to unison:

-sshargs “-pw password”

and it should do the trick (you need the quotes ..)
of course when you automate this, it raises the issue of having your password stored somewhere in a text file on your laptop; so you have to watch out and make sure no unauthorized person can read this file.

Now, if you actually google “window unison ssh”, you will see this page http://www.wischik.com/lu/programmer/unison-ssh.html

which provides a nice tool (conveniently named “ssh.exe”) that you can copy to a folder that’s included in your PATH environment variable, but it still suffers from a problem with the password prompt: the prmopt is broken; it doesn’t work (at least on my vista laptop), as soon as you type the first character, it presses enter for you and prompts you to enter the password again because the password that you just entered is incorrect :) (maybe this only happens in vista, but it happens for me, and that’s what matters).

Now, there’s another dependency that you will probably need: diff, as unison uses it (optionally) to show the differences between files. So you might consider downloading it if you don’t already have it.

Tagged with: , , , ,