Latest Entries »

Port Forward via Tunnel in PuTTY

Okay so today were going to take a look at a great feature of PuTTY that maybe some of you may not have been aware of. Port forwarding of a remote machine through a secure SSH tunnel. There are many reasons for doing this the main being it is secure. Everything over SSH is encrypted that is why doing things via SSH takes little bit longer than with lets say an FTP connection, or a Telnet session. Another reason might be is a service your wanting to use on the remote machine is not being forwarded through the router.

With all the this in mind lets begin the tutorial…

  1. Open PuTTY. If your using a connection that is saved highlight it and select ‘Load’. Otherwise fill in the usual connection info. (Host Name, and Port).
  2. Go to the left in the ‘Category’ window and click the ‘+’ sign next SSH. You should see the sub menu ‘Tunnels’ click on it.
  3. Now you should see two empty fields that say ‘Source Port’ and ‘Destination’.
  4. In ‘Source Port’ your going to enter a random unused port number say ’8806′ (Eventually you are going to enter that port number in the client software connection info).

    In ‘Destination’ you are going to put ’127.0.0.1:3306′ or ‘localhost:3306′ (Sub out 3306 for the actual listening port of the server your trying to connect to in this case I am using MySQL).

  5. Click ‘Add’.
  6. Click ‘Open’ at the bottom.

If it works PuTTY will connect as normal, make sure you login.

Now to test it out you open the client software I will be using MySQL Administrator in my case. Enter ’8806′ in the port number and in the host information make sure you enter ’127.0.0.1′ or ‘localhost’.

Installing GD2 on Linux and Windows

If you have received this error: “PHP: GD2 Not installed:|| File and or directory is invalid”

Than your at the right place…

You may be wondering what GD2 is… the official explanation is
this:“GD is an open source code library for the dynamic creation of images by programmers.”

The reason you are getting this error is most likely because have not installed the GD2 extension in PHP when you first setup your
server. So I will let you know how install it and initialize it in Linux and Windows.

Under Linux::
The easiest way is to use your package manager to install it.
For example in Debian… “apt-get install php5-gd”
or Redhat… “—with-libdir=lib64 —with-gd —with-jpeg-dir —with-png-dir

Under Windows::
You will have to download the .zip archive version of PHP because the binary install doesn’t include most extensions.

Download it here: http://windows.php.net/download/ (Make sure if your using Apache to download the VC6 version).

Copy all the extensions from the “ext” directory from the download into your PHP ext directory (eg: “C:\PHP\ext”) .

You should notice a file called “php_gd2.dll”.

Now to initialize it (like any extension) you have to open your “php.ini” file search for “Dynamic Extensions” and just under that place this exact phrase “extension=php_gd2.dll”.

(Note: make sure that your php.ini file is configured to look in the “C:\PHP\ext” folder for the extensions. Search in the file for “extension_dir”.)

Restart Apache.

And there you have it you now have full GD2 support for PHP. You can now resize, scale and flip images on the fly.

Python on AIX 6.1

To start, I’m not an AIX expert. I’m just a hobbiest that wanted to get a python/django/apache/mysql (db2 down the road) environment work in AIX.
Installing python:

Download and compile the latest python source.

# cd ~/

# wget http://www.python.org/ftp/python/2.6.4/Python-2.6.4.tgz

# gunzip Python-2.6.4.tgz

# tar -xvf Python-2.6.4.tgz

# cd Python-2.6.4

# export PATH=/usr/bin:/usr/vacpp/bin

for 32bit:

# ./configure
for 64bit:

# ./configure —with-gcc=”xlc_r -q64” —with-cxx=”xlC_r -q64” —disable-ipv6 AR=”ar -X64”
# make

# su

# make install
If no problems crept up, the python executable should be available here:

/usr/local/bin/python
The apache installation to next to come!

HTML to PDF in Django

I’ve been involved in a number of project where I need to create pdf reports from a set of data using the Django framework. The easiest way for me to do this is generate the pdf directly from html.

I will provide a list of software required, and how to program a django view to generate the pdf.

Here’s the software you’ll need:

pisa: http://pypi.python.org/pypi/pisa/

ReportLab: http://www.reportlab.org/downloads.html

html5lib: http://code.google.com/p/html5lib/downloads/list

Here’s a simple view to generate the pdf.

#view.py

import cStringIO as StringIO
import ho.pisa as pisa
from django.http import HttpResponse
from django.template.loader import render_to_string

def my_view(request, *args, **kwargs):

   html = render_to_string('some_template.html', RequestContext(request, {
       'title': 'This is a test PDF document'
   }))
   result = StringIO.StringIO()
   pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
   response = HttpResponse(result.getvalue(), mimetype='application/pdf')
   response['Content-Disposition'] = 'attachment; filename=document.pdf'
   return response

Enjoy

Setup git on Centos 5.3, Centos 5.4 and make public over ssh

update oct 22. 2009:
This method also works in Centos 5.4. I’ll post the epel think below once they’re available

I setup git on a Centos 5.3 server the other day, accessible over ssh. Here are the steps I followed to get things up and running.

Centos’s yum repository does not have git in it, so setup fedoras epel (Extra Packages for Enterprise Linux) repo.

i386:

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm

x86_64:

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-3.noarch.rpm

As root run this command:

yum install git

Next I’m going to setup a new repository and make it accessible over ssh:

mdkir /home/username/repo #create directory for new repository
cd /home/username/repo
git init

We’ll create a dummy file to get started. If you trying to clone (checkout) an empty git repository, you’ll just get errors:

touch firstfile

Add all files in this directory to your git repository:

git add .

Commit the changes you’ve made to the repo:

git commit

Next we’ll create a clone of the repo and configure it to be public:

cd /home/username
git clone --bare ./repo repo.git
touch  repo.git/git-daemon-export-ok

you can copy your repo.git directory to where you want to make the repo public

Now we want to checkout a copy of the new repository from a different server.

git clone ssh://yourserveraddress/home/username/repo.git

You should now have a new directory labeled repo which contains the file ‘firstfile’

To add a new file to the repo:

cd repo
touch secondfile
git add .
git commit

Now we want to submit the changes back to the git server:

git push

You’ll be prompted for your password.

Enjoy.

Add confirm dialog to a link with jQuery

Here’s another simple jQuery snippet I like to add to my projects. This will allow you to present a javascript confirm dialog when a user clicks on a link or a submit button with a confirm attribute it. The value of the confirm attribute will be the text displayed in the confirm dialog box. I often use this for delete button so the user has a chance to back out.

$(function() {
   $("a[confirm], input[type=submit][confirm]").click(function() {
       conf = confirm($(this).attr("confirm"));
       if(!conf)
           return false;
   });
}

Turn table rows and buttons into clickable links with jQuery

This simple little script often comes in quite handy when I’m working on a web app. It allow you to add an href attribute to tr, td, or input[type=button] tags to make them clickable links

$(function() {
   $("input[type=button][href], tr[href], td[href]").click(function() {
       location.href = $(this).attr("href");
   });
});

Dynamically change field attributes in a Django ModelForm

I’ve encountered situations using Django’s ModelForm where I’ve needed the form to validate differently then the default behavior in various instances.
Here’s an example of how I changed all the fields from the default of not being required to all being required without much effort:

class AddressRequiredForm(forms.ModelForm):

   def __init__(self, *args, **kwargs):
       super(AddressForm, self).__init__(*args, **kwargs)
       for name, field in self.fields.iteritems():
           if name not in ['address2',]:
               field.required = True

   class Meta:
       model = Address

This is pretty simple, but it saved me a lot of time by not having to re-type each field name with a new set of properties.