Thursday, December 31, 2009

Using django-admin.py within cygwin

Sometimes I still work in windows environment. I always have cygwin installed in windows environment since I use a lot of *nix tools and scripts.
Currently, I am working a lot with python and django, very nice scripting language and powerful web framework. I used native windows python installation, instead of installing it within cygwin.
The problem occur when I tried to invoke django-admin.py script in cygwin's bash shell. It won't run. It is due to windows version of python expecting windows path of the django-admin.py script as the argument.
Here is the exact situation. Windows python expecting, something like this:

python c:\Python26\Scripts\django-admin.py


but since I run that command within cygwin, it will become like the following:

python /cygdrive/c/Python26/Scripts/django-admin.py


and throws some error messages.

So, I searched a bit and found out that cygwin has a tool that converts *nix path to windows path. The name of the tool is cygpath.

And here is what I did to solve the problem.
I created the following file:
/usr/local/bin/django-admin.py

which contains:
python.exe "$(cygpath -aw "/cygdrive/c/Python26/Scripts/django-admin.py")"

Of course you need to make /usr/local/bin/django-admin.py executable

chmod +x /usr/local/bin/django-admin.py


and you can run django-admin.py from anywhere.


Check out the following links for more information:
http://www.cygwin.com/cygwin-ug-net/using-effectively.html
http://www.cygwin.com/cygwin-ug-net/using-utils.html#cygpath

Wednesday, December 16, 2009

multipart sftp download and resume using lftp

Previously I wrote about how to resume scp download using rsync. It is pretty nice. After working a lot with scp and sftp, I was looking for a way to perform multipart download using sftp. It is pretty easy to resume sftp download, since there is already 'reget' command. Then I read some article about lftp. I thought that it was just another ftp client. I was wrong. Taken from the website itself, 'LFTP is sophisticated ftp/http client.' Truly sophisticated. Check more information about lftp from wiki.
One of the coolest feature is the ability to resume and multi-part sftp download. The command itself is pretty simple. Here is how one can do it:

$ lftp -e 'pget -c -n 5 /path/to/file' sftp://username@server


short explanation:
-e: lftp option to execute command
pget: is the command for partial download
-c: pget option to resume
-n: pget option for number of parts

Enjoy!

Wednesday, July 29, 2009

XML Document processing comparison between java and python

I had a project that requires xml document processing. At first, I was thinking to do it using Java, but since the project was not so big, I decided to use Python. I really have to say that it was a good decision. In my opinion, xml doc processing feels more natural in Python compare to Java.

Let's go straight to the code comparison.
There are many steps involved in java before actually getting the xml doc elements that match with xpath expression defined.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("books.xml");

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//book[author='Neal Stephenson']/title/text()");

Object result = expr.evaluate(doc, XPathConstants.NODESET);

NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}


I took the example from IBM website, here. You should check out the link if you are interested using xpath in java.

Now, in Python, using lxml library.

doc = etree.parse("books.xml")
nodes = doc.xpath("//book[author='Neal Stephenson']/title/text()")
print(nodes)


I mostly use java for my projects, pretty much happy with it, even though sometimes I wish that java has some syntactic sugars like in C#.
So, if you have the option to choose which programming language for your xml projects, give Python a try. I am sure you will like it.

Saturday, July 18, 2009

Set super+l shortcut key for locking screen in gnome

The default shortcut keys to lock screen in gnome is <ctrl><alt>l (it is small caps 'L'). I do not like that shortcut and prefer to use the combination of "super key" and "l", which actually defined as <Mod4>l. The keybinding GUI setting somehow does not accept any combination of super key with other keys. So, to be able to do that, you need to use gconf-editor.
Perform the following:

run gconf-editor,
option 1, from CLI:
$ gconf-editor


option 2, from GUI:
press <Alt>F2 to get the 'run application' dialog and type in gconf-editor and click 'run'

In gconf-editor window, go to the following key location, by expanding the keys tree:
/apps/gnome_settings_daemon/keybindings


then, double click 'screensaver' key and change the value to: <Mod4>l

and you are done. Test it by pressing super key and l.

There is also feature to search the key name and value, by pressing <Ctrl>f in gconf-editor. Actually, I found the correct location of the 'screensaver' key by searching it.

Friday, July 10, 2009

backup and restore MBR

Partition Table backup

Perform the following command to backup the partition table:
sfdisk -d /dev/hdx > /part/to/pt_info.sfdisk



Partition Table restore

Perform the following command to restore the partition table:
sfdisk -d /dev/hdx < /part/to/pt_info.sfdisk



MBR backup

Perform the following command to backup the MBR:
dd if=/dev/hdx of=/path/to/image count=1 bs=512



MBR restore

Perform the following command to restore the MBR:
dd if=/path/to/image of=/dev/hdx count=1 bs=446

Notice that you need to include the option "count=1 bs=446", otherwise your partition table will be overwritten.

Wednesday, July 8, 2009

Install chromium browser with flash plugin in ubuntu

Install chromium browser with flash plugin in ubuntu

Add the following repository in your apt's sources.list
deb http://ppa.launchpad.net/chromium-daily/ppa/ubuntu jaunty main


Add the key for it
$ sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 4E5E17B5


Perform the following
$ sudo aptitude update
$ sudo aptitude install chromium-browser


Chromium browser should be installed and ready to be used. If you already installed flashplugin you can add the plugin to chromium-browser. I noticed the browser takes a lot of resource after adding flashplugin, so I no longer user. To set flashplugin perform the following.
$ cd /usr/lib/chromium-browser/plugins
$ sudo ln -s /usr/lib/flashplugin-installer/libflashplayer.so

Friday, June 19, 2009

Auto disable touchpad while typing using syndaemon

Auto disable touchpad while typing using syndaemon

Often time, when working on laptop, my hands touch (tap) the touchpad while typing, which is really annoying, cause I end up writing in different location. Disabling the touchpad permanently is not a good idea, specially if you don't have a mouse with you. Fortunately, there is a very neat tool that monitors keyboard activity and has the ability to disable the touchpad during that event. The tool is syndaemon.

usage example:
$ syndaemon -d -i 1

That would execute syndaemon as daemon and set 1 second as the delay period.

To automatically run this daemon, for example in KDE desktop environment, I created a script which contains the code above, make it executable, and put it in ~/.kde/Autostart folder.

Also check out synclient, taken from the 'man page', synclient - commandline utility to query and modify Synaptics driver options.

Friday, June 12, 2009

Victor Wooten Introduction

Victor Wooten Introduction

My favorite jazz bassist. Enjoy!

Processing xml document in python using lxml

Processing xml document in python using lxml

In previous entry, I discussed about libxml2. Now, I am going to discuss about lxml, which in my opinion is awesome. I really appreciate the developer team that create lxml. It makes processing xml document easier.
The best way to get it by going to this link and choose the appropriate version. As soon as you installed lxml, try to use it. Here is a sample of how to use lxml.

from lxml import etree
import sys

def getChildInfo(child):
return "id: " + child.get("id") + ", data: " + child.text + "\n"

xmlString = "c1c2"
tree = etree.fromstring(xmlString)
children = tree.xpath("//child")
sys.stdout.write(str(len(children)) + "\n")
for el in [ getChildInfo(child) for child in children ]:
sys.stdout.write(el)

childList = tree.xpath("//child[@id='1']")
sys.stdout.write(getChildInfo(childList[0]))
child = tree.xpath("//child[@id='2']")
sys.stdout.write(getChildInfo(childList[0]))

For more examples and other information about lxml, check out this link.

Thursday, June 11, 2009

Processing xml document in python using libxml

Processing xml document in python using libxml

I had to spent sometimes to gather information about processing xml document in python. There are several different libraries to use, such as minidom, libxml, etree, etc. During my work, I had been working with xpath, so I need xpath support in the built-in library that I work with, so I chose libxml. It is difficult for me to gather information on how to use that API. There are minimum documentations, guides, examples, and tutorials out there.
After spending sometimes, I became familiar on using it. I am writing this, so other people could simply learn on how to use libxml in more realistic situation. Many examples I found in the internet is way too basic, and does not suit my need.


example 1:
--------------------------------------

import sys
import libxml2

def main():
doc = libxml2.parseFile("data.xml")
ctxt = doc.xpathNewContext()
# get student elements in xml doc
res = ctxt.xpathEval("//student")
# output all properties for "student" element using xpath
sys.stdout.write(res[0].get_properties().content + "\n")

for chld in res[0].children:
if chld.type == "element":
# output all information within the child element
sys.stdout.write(chld.content + "\n")

doc.freeDoc()
ctxt.xpathFreeContext()

if __name__ == '__main__':
main()


--------------------------------------


example 2:
--------------------------------------

import sys
import libxml2

def main():
doc = libxml2.parseFile("layout.xml")
ctxt = doc.xpathNewContext()
# get all "property" element that has attribute "Location",
# which parent is "object" element that has attribute "pickList*"
res = ctxt.xpathEval("//Object[starts-with(@name,'pickList')]/Property[@name='Location']")
for r in res:
sys.stdout.write(r.content + "\n")

sys.stdout.write(str(len(res)))

doc.freeDoc()
ctxt.xpathFreeContext()

if __name__ == '__main__':
main()


--------------------------------------


It is also useful to see the source code of libxml.py, just to see what's going on under the hood.

Not long after writing this post, I found even better xml API, which is lxml. I will make separate post about this wonderful xml API for python.

Monday, June 8, 2009

"service" like redhat

"service" like redhat

One of many things that I like in Redhat is "service" tool to start, stop registered service (daemons). I realised that in my current ubuntu 8.10, this tool is already installed by default, which is really good. I am pretty sure that it will still available in newer release.
What about in Debian? To have such tool, you need to install "sysvinit-utils" package.

# aptitude install sysvinit-utils


to use it, simply type like the following
# service  start
# service stop
# service restart
# service status


In ubuntu (also in debian, if I'm not mistaken), you can use "tab" command completion, when you are trying to enter the . Very neat!

Saturday, June 6, 2009

Trim Filename Extension

Trim Filename Extension

There is built-in functionality in Linux shell to trim extension of a file name.
${filename_env_var%.ext} --> note that dollar ($) sign in the beginning is NOT shell prompt.

Usage example:
$ filename=trace_20090606.txt
$ echo ${filename%.txt}
trace_20090606 --> this is the output of previous command

Broadcast Ping

Broadcast Ping

Perform the following commands:


$ ifconfig eth0 | grep -i bcast
inet addr:10.10.44.44 Bcast:10.10.255.255 Mask:255.255.0.0

$ ping -b 10.10.255.255


You should see several IP addresses respond to ping.

Problem with Java SocketChannel

Problem with Java SocketChannel

I created a simple java network application and wanted to put it as a service (windows and linux). To do that, I had to implement shutdown mechanism that releases all resources in proper manner for my java app. Obviously, I used Socket (and ServerSocket) class, which runs in separate thread, and used InputStream and OutputStream to read and write data.
So, the problem comes when I need to stop the thread that has Socket which block for incoming data (by invoking read method for example). Clearly, I need to be able to interrupt that blocking point and exit the thread gracefully.
After searching a bit in the internet, I found out about SocketChannel, which implements InterruptibleChannel. This is good, so I could simply send interrupt signal to the thread and the SocketChannel will also get it and quit from blocking state.

But there is a problem (perhaps bug) with the implementation of SocketChannel, that would prevent concurrent read and write to it. Please refer to sun java bug id 4774871 and 4509080.

The work around is like the following (taken from bug #4774871 details):

CUSTOMER WORKAROUND :
The above program works if you change the wrapChannel() method to wrap the SocketChannel to a dummy channel that only forwards the calls to it. It works because it is not an instance of SelectableChannel:


private static ByteChannel wrapChannel(final ByteChannel channel) {

return new ByteChannel() {
public int write(ByteBuffer src) throws IOException {
return channel.write(src);
}

public int read(ByteBuffer dst) throws IOException {
return channel.read(dst);
}

public boolean isOpen() {
return channel.isOpen();
}

public void close() throws IOException {
channel.close();
}
};
}

Friday, June 5, 2009

LiveCD PXE Network Boot

LiveCD Network Boot (pxe boot)

Server Side:
In case you are using VirtualBox, make sure you set the network to internal network.

On Server (Fedora 10):

turn off selinux
method 1: from GUI and reboot
method 2: $ sudo vi /etc/selinux/config
edit --> SELINUX=disabled
save and reboot

turn off firewall
method 1: from GUI (need to click apply button)
method 2: $ sudo iptables -F

configure the IP for NIC (assumption NIC to be configured is eth0):
method 1:use GUI and set the ip to 192.168.0.15 for eth0
method 2:
$ sudo ifconfig eth0 down
$ sudo ifconfig eth0 192.168.0.15 netmask 255.255.255.0 up


mount kubuntu to /mnt/kubuntu
method 1: mount kubuntu_desktop_9.04.iso to /mnt/kubuntu folder
$ sudo mount -o loop kubuntu_desktop_9.04.iso to /mnt/kubuntu
method 2: mount cdrom to /mnt/kubuntu
$ sudo mount /dev/sr0 /mnt/kubuntu

install syslinux:
method 1: from GUI install syslinux package
method 2: $ sudo yum install syslinux

install tftp server:
method 1: from GUI install tftp-server package
method 2: $ sudo yum install tftp-server

add necessary files for network boot in tftp share folder:
$ sudo mkdir -p /var/lib/tftpboot/livecd/pxelinux.cfg
$ sudoe cp -p /usr/lib/syslinux/pxelinux.0 /var/lib/tftpboot/livecd/
$ sudo cp -p /mnt/kubuntu/casper/vmlinuz /var/lib/tftpboot/livecd/
$ sudo cp -p /mnt/kubuntu/casper/initrd.gz /var/lib/tftpboot/livecd/
$ sudo vi /var/lib/tftpboot/livecd/pxelinux.cfg/default
add the following lines to the file:
############## start of pxe default ##############
default kubuntu
timeout 30

label kubuntu
kernel vmlinuz
append boot=casper netboot=nfs nfsroot=192.168.0.15:/mnt/kubuntu initrd=initrd.gz --
############## end of pxe default ##############

activate tftp server:
$ sudo chkconfig tftp on

restart xinetd:
method 1: from GUI
method 2: $ sudo service xinetd restart

install dhcp server:
method 1: from GUI install dhcp package
method 2: $ sudo yum install dhcp

configure dhcp server:
$ sudo vi /etc/dhcpd.conf

replace the content with:
############## start of dhcpd.conf ##############
allow booting;
allow bootp;
ddns-update-style interim;
ignore client-updates;

subnet 192.168.0.0 netmask 255.255.255.0 {
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.0.255;
range dynamic-bootp 192.168.0.200 192.168.0.240;
next-server 192.168.0.15;
filename "livecd/pxelinux.0";
}
############## end of dhcpd.conf ##############

restart dhcp server:
method 1: from GUI
method 2: $ sudo service dhcpd restart
method 3: if you have more than one NICs, and you want to use dhcp only for eth1 perform the following,
$ sudo dhcpd -d eth1

install nfs server:
method 1: from GUI install nfs-utils package
method 2: $ sudo yum install nfs-utils

configure nfs server:
$ method vi /etc/exports
add the following line:
############## start of /etc/exports ##############
mnt/kubuntu 192.168.0.0/255.255.255.0(async,no_root_squash,no_subtree_check,ro)
############## end of /etc/exports ##############

restart nfs server:
method 1: from GUI
method 2: $ sudo service nfs restart

check nfs export:
$ sudo showmount -e 192.168.0.15


Client Side:
In case you are using VirtualBox, make sure you set the network to internal network.
Set BIOS boot option to PXE Network Boot
Boot the computer

You should be able to see the client PC getting DCHP and loading the LiveCD from network.

Thursday, February 26, 2009

yesterday's date in linux

To get yesterday's date in linux, simply use the following command:
$ date --date='1 day ago'

Sunday, February 22, 2009

Servlet unit testing using Mockrunner

Servlet unit testing using Mockrunner (container free)

I had to write a servlet earlier this month and was looking for a way to do unit test for it. I was hoping that there is a way to do unit test for servlet. Right away, I knew it would not be straight forward as unit testing a regular simple java class. If you are familiar with servlet, of course you know that it depends on some other classes, such as HttpServletRequest, HttpServletResponse, etc.

So, one way of testing it, we need mock object. There is an excellent information about what mock object is all about by Martin Fowler, in this link.
While there are many other framework for JEE unit testing, I chose Mockrunner as the framework for my unit testing.

Download Mockrunner from the site, and include the library in your project. After that, create a test class which extends BasicServletTestCaseAdapter, so your test class would be something like the following:


public class MyServletTest extends BasicServletTestCaseAdapter {
// ...
}

then create an instance of the servlet you want to test, in this example would be MyServletTest:
protected void setUp() throws Exception
{
super.setUp();
createServlet(MyServletTest.class);
}



if your servlet requires parameter(s) then use addRequestParameter() method to set each one:
addRequestParameter("id", 1234);

The servlet that I created need to check the requested URL, so I had to find a way how to set it up. Fortunately, it is possible to do. Here's an example:
getWebMockObjectFactory().getMockRequest().setRequestURL("subscribe");

After all the setup done, it's time to test the servlet, by calling doPost() method, and check all actions that have been done.
I needed to check whether my servlet redirecting to the proper page afterwards. Here's how to do it:


MockHttpServletResponse response = getWebMockObjectFactory().getMockResponse();
assertEquals("redirect_location", response.getHeader("Location"));



I am glad I found this neat testing tool.

Saturday, February 21, 2009

ACT!2009 SDK

ACT!2009 SDK

Nice thing about the recent version of ACT is that they provide a way to extend the functionality using either C# or VB.NET.

You can either create a plugin (dll) to extend the existing functionality of ACT, or create a separate stand alone application that uses the ACT Framework.

I haven't done much with the SDK, still trying to get a feel with the whole Framework thing. Besides, I am also not so sure it's a good idea to create many plugins and applications, since there are some nasty bugs in ACT!2009, like I mentioned in my earlier post.

In order to use the SDK, you need to have the original Installer CD. Go to the following folder to find all necessary dll's ACT!2009\PremiumEX\ACTWG\GlobalAssemblyCache. This the case where you don't have the ACT!2009 installed in your system.
If you have ACT installed in your system, you can go to C:\WINDOWS\assembly\GAC_MSIL and find all the required dll's. Notice that you can not see GAC_MSIL folder from explorer, but instead you can see it from CLI.

ACT!2009 Backup

ACT!2009 Backup

Do you backup your ACT!2009 DB regularly? If not, you should configure automatic backup.
Use ACT Scheduler to schedule automatic backup. Refer to this link on how to set it up.

I really suggest to store the backup in different place then where the ACT!2009 data is located. You might face some problems if you put in the default backup folder. Refer to this link for more information about this.

If somehow you decided to store the backup in other computer/server that needed login validation, you need to configure the ACT Scheduler Service to use proper login account. Click here to see how to do it.

List of problem with ACT!2009 + Outlook 2007

List of problem with ACT!2009 + Outlook 2007

List of ACT!2009 and Outlook 2007 known problem (so far) as per 20090221

1. After performing lookup by example in ACT, the information shown in the fields are from the previous contact. This can be quite confusing for user. Until now, there is no solution for this issue. The only thing we can do is, pressing "F5", to refresh the page, after performing any lookup.

2. Several fields are not shown in import/export wizard in ACT. I consider this issue as the worst one. I was facing a lot of trouble when trying to import some contacts from other database. Too many missing data, which really makes the import/export process is useless. True, we can always use the quick export to excel, but the number of the fields is then limited to what is supported by excel. No good.
Click here to get more information about the problem (but not the solution).

3. Again in ACT. If you happened to have date or annual event field type in your layout, be very careful. Usually, after I make any change to the date field, I click outside, such as the layout background, so when I use the scroll wheel in my mouse, the date will not get changed. Surprisingly, the date still get changed. So, either use the scroll bar, or click on text field then use the scroll wheel.

4. When performing mail merge to a lot of contacts, for example a thousand, outlook crashes. Not good. Solution? Use different email client that can handle big number of emails.

5. I created a simple vba script to perform mail merge from outlook. I was hoping that using vba script, I could perform the mail merge without outlook crashing, and the email would be attached in history. Emails went through just fine, but this time ACT.Outlook.Service.exe crashed.

vnc over ssh

For those who like to use vnc to work with remote computer, you should be aware that the traffic is not encrypted. Please refer to tightvnc faq page for information about this. Just like what recommended in the faq, ssh is required to secure the traffic.

Create a tunnel between your local and remote computer, and one way to do this is:
$ ssh -CNT username@remote_computer -L 5901:127.0.0.1:5900

Now, simply set your vnc viewer to access localhost with port 5901.

An example of using vncviewer is:
$ vncviewer localhost:5901

Friday, February 20, 2009

ecryptfs == privacy

ecryptfs == privacy

Simple and nice way to protect your documents in linux.
Install ecryptfs by executing the following command:
$ sudo apt-get install ecryptfs-utils

Create a directory where you want to put those private documents. For example I will create a folder in my home, named "Personal", and set the permission only to owner.
$ mkdir ~/Personal
$ chmod 700 ~/Personal

Mount that directory as eCryptfs filesystem.

$ sudo mount -t ecryptfs ~/Personal ~/Personal
I chose passphrase, aes, and 16-bit. For plaintext passthrough I chose "no" (no non-ecrypted files allowed in the mount).

Move, copy, create all private documents into ~/Personal directory.

When done, umount ~/Personal by executing:
$ sudo umount ~/Personal

All the files inside that directory can not be opened unless you mount it as ecryptfs filesystem.

Next time you mount, use the following command, so it will only ask for passphrase.
$ sudo mount -t ecryptfs ~/Personal ~/Personal -o key=passphrase,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_passthrough=n

Thursday, February 19, 2009

ACT + Outlook 2007 + PST over the LAN = Huge Mess

ACT + Outlook 2007 + PST over the LAN = Huge Mess

I had some problems with ACT!2009 and Outlook 2007. From Outlook, I kept getting pop-up error message saying that the pst file was locked, probably used in some other terminal. ACT!2009 kept giving me pop-up error message saying something like Act! Value cannot be null. Parameter name:pUnk. What an error message, and who is this pUnk guy, anyway.

So, initially the pst file was stored in the server, so different user can take care the incoming file at a time. True, that Exchange server should be used in this particular case, but somehow they don't want to use it, with many different reason, which I don't agree. The user insisted that they want to have convenience of not having to switch back and forth using different computer. Therefore, pst file stored in server.

When outlook showed error saying that the pst file was locked and being used in other terminal, one thing I had in my mind was, probably that's true. So, I made sure no one else is using it. Still, the error occurred many times.
The error message from ACT was really gave me no clue on what's going on.

After spending a lot of time searching here and there, I came across the following information: Personal folder files are unsupported over a LAN or over a WAN link, http://support.microsoft.com/kb/297019

My intuition told me that this must have something to do with the problems we were having. Well, it turned out to be that storing pst file in the server and accessed over the network is a terrible idea.

Once I convinced users that since they don't have Exchange server and pst file is not designed to be used over network, they agree to move the pst files locally. Problem solved.

ACT!2000 to ACT!2009 Migration

ACT!2000 to ACT!2009 Migration

Currently, I work a lot with ACT!2009, Contact and Customer Relationship Management Software. This is pretty new thing for me. Interesting and troublesome at the same time.

I had to migrate all data from ACT!2000 to ACT!2009. According to the ACT Knowledge Base, the procedure should be simple, and straight forward. The only problem is, the database in ACT!2000 was corrupted, quite badly. ACT diagnostic tool could not fix the problem, and the only way to fix this is by using import/export features. Please go here to get detailed information on how to use import and export to repair ACT!2000 database.

The process was time consuming and when there were duplicate contact records found during import, user interaction was needed. Apparently the there were a lot of duplicate contact found in the database.
One lesson learned here, which suppose to be done by previous user, is to resolve duplicate contact record.

Once the repair process done, I simply opened the previous version of ACT database in the new ACT, and the conversion process begins.
After the conversion done, there are many fields that are not converted to the properly type. For example, date field become annual event. Memo files become regular text fields. No good.

ACT Knowledge Base is a place to go for ACT users.

ssh tunneling

ssh tunneling

In the client execute:
$ ssh -N -D user@CompB

where CompB is the server

To use this ssh tunneling with internet browser, for example firefox, change the "Connection Settings", under "Manual proxy configuration", update the "SOCKS Host:" to localhost:any_free_port. Notice that any_free_port here must be the same one as the one used in ssh.

Opera browser does not have socks proxy configuration. One way to do it is by using "tsocks."
Install tsocks:
$ sudo apt-get install tsocks

edit /etc/tsocks.conf, and change
server = 127.0.0.1 # this is localhost
server_port = # again, this must match with the port used in ssh.

screen

I work remotely a lot, and mostly using ssh, since it's more secure than telnet. There's a time when the process takes very long time, and I don't want to always be connected the whole time.
The problem is, when I put the process in the background (using ctrl-z and bg, or using "&" when executing), when I exited the remote shell, all processes belong to that remote shell will be terminated.

One way to resolve this issue is using "nohup". This is very useful when the process does not require any interaction. Still I'm not satisfied with "nohup."

Then I came across this very neat tool, "screen." Very elegant way to do things remotely.

Do the following:

$ screen

it will give you the session, and start to do any activity. So, when I want to leave the remote shell. Press --> ctrl-a, d. That command will detach the screen session, and it's save to exit the remote shell.

To resume that particular session. Login again to the remote computer, then look at the detached session, by:
$ screen -ls
$ screen -r <session_id>

rsync + ssh => resumable download

rsync + ssh => resumable download

Sometimes ago, I felt the necessity of being able to resume my download from another computer, specially if the file is quite big. In this case, it's time to use rsync. Since mostly I use ssh to access remote computer, so I combine rsync with ssh.

Here's how to do it:
$ rsync --partial --progress --rsh=ssh user@server:/serverpath/to/file /localpath/to/file

Now, what if you need to use non-standard ssh port? Let's say you need to connect to ssh via port 7890. The command will be like the following:
$ rsync --partial --progress --rsh='ssh -p7890' user@server:/serverpath/to/file /localpath/to/file

The neat thing is that there is also progress indicator shown.

Tuesday, February 17, 2009

Resolve Windows hostname in Linux

Resolve Windows hostname in Linux

I work a lot in heterogeneous environment, and ofcourse, windows is one of the system in that network. So, I would like to be able to resolve windows hostname (netbios) and access it's resource. In order to do that, there are packages needed to be installed first.

$ sudo apt-get install winbind

after that, edit /etc/nsswitch.conf configuration file.
Search for word "hosts", and add "wins" at the end of the line. Below is an example of "hosts" configuration after update.

# /etc/nsswitch.conf
#
# ....

hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4 wins

#....

after that, restart winbind service (if necessary). Now, windows computers should be able to be accessed using it's windows hostname. Pretty convenient way to access those windows computer in the network.

If you are still not able to resolve windows hostname, perhaps there is a firewall running in your computer. In that case you need to add extra rule to allow udp.

I use firestarter for my personal firewall, and what I did was edit the /etc/firestarter/inbound/setup configuration file.
Search for:
$IPT -A INBOUND -p udp -m state --state ESTABLISHED,RELATED -j ACCEPT
and put the following rule right after it:
$IPT -A INBOUND -s $NET -p udp -m state --state NEW -j ACCEPT

restart firestarter, to load the new config. Try to access windows computer using it's hostname again.


I just found out a way to configure UFW for this matter.
First edit: /etc/default/ufw
Search for:

IPT_MODULES="nf_conntrack_ftp nf_nat_ftp nf_conntrack_irc nf_nat_irc"

and change it to:
IPT_MODULES="nf_conntrack_ftp nf_nat_ftp nf_conntrack_irc nf_nat_irc nf_conntrack_netbios_ns"


Then: reload ufw
# sudo ufw reload

Next step: Configure UFW Rules
Allow outgoing UDP to port: 137, 138
Allow outgoing TCP to port: 139, 445


Sunday, February 15, 2009

convert bin to iso (linux)

convert bin to iso (linux)

To convert bin file to iso format, simply use bchunk tool in linux.
Check whether it's already in your system (ex. $ which bchunck).
If not, install the tool first by executing:
$ sudo apt-get bchunk


after that, let's convert bin to iso, by executing:
$ bchunk binimage.bin binimage.cue isoimage


Notice that bchucnk needs cue file (as second argument).
If there is no cue file, do not panic, since it's easy to create the cue file.

Below is a simple shell script to create cue file and convert bin to iso:
#!/bin/bash
printf "FILE $1.bin BINARY\nTRACK 01 MODE1/2352\nINDEX 01 00:00:00\n" > $1.cue
bchunk $1.bin $1.cue $1_
rm $1.cue


save the script to a file, don't forget to chmod +x, and run it.