Search This Blog

Friday, October 31, 2008

How to write & read a dictionary in File

Using Pickle in Python for dumping and reading a Dictionary from a file



D1 = {'I':1,'II':2,'III':3,'IV':4,'V':5,'VI':6,'VII':7,'VIII':8,'IX':9,'X':10}

# to save a Python object like a dictionary to a file
# and load it back intact you have to use the pickle module

import pickle

print "The original dictionary:"
print romanD1

file = open("abc.dat", "w")
pickle.dump(D1, file)
file.close()

# now load the dictionay object back from the file ...
file = open("abc.dat", "r")
D2 = pickle.load(file)
file.close()

print "Dictionary after pickle.dump() and pickle.load():"
print D2

Saturday, October 25, 2008

To Format a UsB pen drive using Linux terminal

Insert your USB Pendrive. Let it get detected and mounted. Open Terminal. Type The Following commands 1. Unmount your pen drive by using umount /dev/sda

(In your case, please substitute sda with the appropriate device, listed above.

2. use the mkfs.vfat command to format to FAT32 filesystem, or mkfs.ext3 to format to ext3 filesystem

mkfs.vfat -n ‘Label’ -I /dev/sda

Replace Label with the name you want the pen drive to have.

3. That’s it! When done formatting, you’ll be returned to the prompt temp@HACKHOME:~$ mkfs.vfat -n ’hack’ -I /dev/sda mkfs.vfat 2.11 (12 Mar 2005) sathya@shaman:~$

Remove and insert the pen drive to have mounted again!

Friday, October 17, 2008

Troubleshooting Linux with Syslog

syslog

syslog is a utility for tracking and logging all manner of system messages from the merely informational to the extremely critical. Each system message sent to the syslog server has two descriptive labels associated with it that makes the message easier to handle.

  • The first describes the function (facility) of the application that generated it. For example, applications such as mail and cron generate messages with easily identifiable facilities named mail and cron.
  • The second describes the degree of severity of the message. There are eight in all and they are listed in Table 5-1:

You can configure syslog's /etc/rsyslog.conf configuration file to place messages of differing severities and facilities in different files. This procedure will be covered next.

Table 5-1 Syslog Facilities

Severity LevelKeywordDescription
0emergenciesSystem unusable
1alertsImmediate action required
2criticalCritical condition
3errorsError conditions
4warningsWarning conditions
5notificationsNormal but significant conditions
6informationalInformational messages
7debuggingDebugging messages

The /etc/rsyslog.conf File

The files to which syslog writes each type of message received is set in the /etc/rsyslog.conf configuration file. In older versions of Fedora this file was named /etc/syslog.conf.

This file consists of two columns. The first lists the facilities and severities of messages to expect and the second lists the files to which they should be logged. By default, RedHat/Fedora's /etc/rsyslog.conf file is configured to put most of the messages in the file /var/log/messages. Here is a sample:

*.info;mail.none;authpriv.none;cron.none           /var/log/messages 

In this case, all messages of severity "info" and above are logged, but none from the mail, cron or authentication facilities/subsystems. You can make this logging even more sensitive by replacing the line above with one that captures all messages from debug severity and above in the /var/log/messages file. This example may be more suitable for troubleshooting.

*.debug                                          /var/log/messages 

In this example, all debug severity messages; except auth, authpriv, news and mail; are logged to the /var/log/debug file in caching mode. Notice how you can spread the configuration syntax across several lines using the slash (\) symbol at the end of each line.

 *.=debug;\        auth,authpriv.none;\        news.none;mail.none     -/var/log/debug 

Here we see the /var/log/messages file configured in caching mode to receive only info, notice and warning messages except for the auth, authpriv, news and mail facilities.

*.=info;*.=notice;*.=warn;\        auth,authpriv.none;\        cron,daemon.none;\        mail,news.none          -/var/log/messages 

You can even have certain types of messages sent to the screen of all logged in users. In this example messages of severity emergency and above triggers this type of notification. The file definition is simply replaced by an asterisk to make this occur.

*.emerg                         * 

Certain applications will additionally log to their own application specific log files and directories independent of the syslog.conf file. Here are some common examples:

Files:

/var/log/maillog             : Mail /var/log/httpd/access_log    : Apache web server page access logs 

Directories:

/var/log /var/log/samba                      : Samba messages /var/log/mrtg                       : MRTG messages /var/log/httpd                      : Apache webserver messages 

Note: In some older versions of Linux the /etc/rsyslog.conf file was very sensitive to spaces and would recognize only tabs. The use of spaces in the file would cause unpredictable results. Check the formatting of your /etc/rsyslog.conf file to be safe.

Thursday, October 16, 2008

To edit and understand /etc/fstab

How to edit and understand /etc/fstab

There's a file called /etc/fstab in your Linux system. Learn what its contents mean and how it's used in conjunction with the mount command. When you learn to understand the fstab file, you'll be able to edit its contents yourself, too.

I assume you already know how to mount filesystems and partitions with the mount command.

What is fstab and why it's useful

fstab is a configuration file that contains information of all the partitions and storage devices in your computer. The file is located under /etc, so the full path to this file is /etc/fstab.

/etc/fstab contains information of where your partitions and storage devices should be mounted and how. If you can't access your Windows partition from Linux, aren't able to mount your CD or write to your floppy as a normal user, or have problems with your CD-RW, you probably have a misconfigured /etc/fstab file. So, you can usually fix your mounting problems by editing your fstab file.

/etc/fstab is just a plain text file, so you can open and edit it with any text editor you're familiar with. However, note that you must have the root privileges before editing fstab. So, in order to edit the file, you must either log in as root or use the su command to become root.

Overview of the file

Of course everybody has a bit different /etc/fstab file because the partitions, devices and their properties are different on different systems. But the basic structure of fstab is always the same. Here's an example of the contents of /etc/fstab:

[HiTMAN ~]# cat /etc/fstab LABEL=/1 / ext3 defaults 1 1 LABEL=/boot1 /boot ext3 defaults 1 2 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 LABEL=SWAP-sda3 swap swap defaults 0 0 /dev/sdb /backup ext3 defaults 1 2

What does all this gibberish mean? As you see, every line (or row) contains the information of one device or partition. The first column contains the device name, the second one its mount point, third its filesystem type, fourth the mount options, fifth (a number) dump options, and sixth (another number) filesystem check options. Let's take a closer look at this stuff.

1st and 2nd columns: Device and default mount point

The first and second columns should be pretty straightforward. They tell the mount command exactly the same things that you tell mount when you mount stuff manually: what is the device or partition, and what is the mount point. The mount point specified for a device in /etc/fstab is its default mount point. That is the directory where the device will be mounted if you don't specify any other mount point when mounting the device.

Linux distros create special directories for mount points. Most distros create them under /mnt, but some (at least SuSE) under /media. As you probably noticed when looking at the example fstab, I use SuSE's mount points as an example.

What does all this mean? If I type the following command: $ mount /dev/fd0 ... my floppy will be mounted in /media/floppy, because that's the default mount point specified in /etc/fstab. If there is no entry for /dev/fd0 in my fstab when I issue the command above, mount gets very confused because it doesn't know where to mount the floppy.

You can freely change the default mount points listed in /etc/fstab if you're not satisfied with the defaults your distro has given you. Just make sure the mount point is a directory that already exists on your system. If it doesn't, simply create it.

Some partitions and devices are also automatically mounted when your Linux system boots up. For example, have a look at the example fstab above. There are lines that look like this:

LABEL=/1 / ext3 defaults 1 1 LABEL=/boot1 /boot ext3 defaults 1 2

As you've learned, these lines mean that /dev/hda2 will be mounted to / and /dev/hdb1 to /home. This is done automatically when your Linux system boots up... if it wouldn't, you'd have a hard time using your cool Linux system because all the programs you use are in / and you wouldn't be able to run them if / wasn't mounted! But how does the system know where you want to mount /dev/hda2 and /dev/hdb1? By looking at the /etc/fstab file of course.

3rd column: Filesystem type

The third column in /etc/fstab specifies the filesystem type of the device or partition. Many different filesystems are supported but we'll take a look at the most common ones only.

ext2 and ext3 Very likely your Linux partitions are Ext3. Ext2 used to be the standard filesystem for Linux, but these days, Ext3 and ReiserFS are usually the default filesystems for almost every new Linux distro. Ext3 is a newer filesystem type that differs from Ext2 in that it's journaled, meaning that if you turn the computer off without properly shutting down, you shouldn't lose any data and your system shouldn't spend ages doing filesystem checks the next time you boot up.

reiserfs Your Linux partitions may very well be formatted as ReiserFS. Like Ext3, ReiserFS is a journaled filesystem, but it's much more advanced than Ext3. Many Linux distros (including SuSE) have started using ReiserFS as their default filesystem for Linux partitions.

swap The filesystem name is self-explanatory. The filesystem type "swap" is used in your swap partitions.

vfat and ntfs Your Windows partitions are probably either Vfat or NTFS. The 9x series (95, 98, ME) all use Vfat (more widely known as FAT32), and the NT series (NT, 2000, XP) use NTFS. In 2000 and XP you can choose the filesystem type, so 2000 and XP partitions may be formatted as Vfat, too. If you want to be able to write to your Windows partitions from Linux, I suggest formatting them as Vfat, because Linux's support for writing to NTFS partitions is a bit shabby at this moment.

auto No, this isn't a filesystem type :-) The option "auto" simply means that the filesystem type is detected automatically. If you take a look at the example fstab above, you'll see that the floppy and CD-ROM both have "auto" as their filesystem type. Why? Their filesystem type may vary. One floppy might be formatted for Windows and the other for Linux's Ext2. That's why it's wise to let the system automatically detect the filesystem type of media such as floppies and cdroms.

4th column: Mount options

The fourth column in fstab lists all the mount options for the device or partition. This is also the most confusing column in the fstab file, but knowing what some of the most common options mean, saves you from a big headache. Yes, there are many options available, but I'll take a look at the most widely used ones only. For more information, check out the man page of mount.

auto and noauto With the auto option, the device will be mounted automatically (at bootup, just like I told you a bit earlier, or when you issue the mount -a command). auto is the default option. If you don't want the device to be mounted automatically, use the noauto option in /etc/fstab. With noauto, the device can be mounted only explicitly.

user and nouser These are very useful options. The user option allows normal users to mount the device, whereas nouser lets only the root to mount the device. nouser is the default, which is a major cause of headache for new Linux users. If you're not able to mount your cdrom, floppy, Windows partition, or something else as a normal user, add the user option into /etc/fstab.

exec and noexec exec lets you execute binaries that are on that partition, whereas noexec doesn't let you do that. noexec might be useful for a partition that contains binaries you don't want to execute on your system, or that can't even be executed on your system. This might be the case of a Windows partition.

exec is the default option, which is a good thing. Imagine what would happen if you accidentally used the noexec option with your Linux root partition...

ro Mount the filesystem read-only.

rw Mount the filesystem read-write. Again, using this option might cure the headache of many new Linux users who are tearing their hair off because they can't write to their floppies, Windows partitions, or something else.

sync and async How the input and output to the filesystem should be done. sync means it's done synchronously. If you look at the example fstab, you'll notice that this is the option used with the floppy. In plain English, this means that when you, for example, copy a file to the floppy, the changes are physically written to the floppy at the same time you issue the copy command.

However, if you have the async option in /etc/fstab, input and output is done asynchronously. Now when you copy a file to the floppy, the changes may be physically written to it long time after issuing the command. This isn't bad, and may sometimes be favorable, but can cause some nasty accidents: if you just remove the floppy without unmounting it first, the copied file may not physically exist on the floppy yet!

async is the default. However, it may be wise to use sync with the floppy, especially if you're used to the way it's done in Windows and have a tendency to remove floppies before unmounting them first.

defaults Uses the default options that are rw, suid, dev, exec, auto, nouser, and async.

5th and 6th columns: Dump and fsck options

Dump and, uh, what options? Well, dump is a backup utility and fsck is a filesystem check utility. I won't discuss them in great length here (they would both need their own tuXfile), but I'll mention them, because otherwise you'd spend the rest of the day wondering what on God's green Earth do these things mean.

The 5th column in /etc/fstab is the dump option. Dump checks it and uses the number to decide if a filesystem should be backed up. If it's zero, dump will ignore that filesystem. If you take a look at the example fstab, you'll notice that the 5th column is zero in most cases.

The 6th column is a fsck option. fsck looks at the number in the 6th column to determine in which order the filesystems should be checked. If it's zero, fsck won't check the filesystem.

Example /etc/fstab entries

As an example, we'll take a look at a couple of fstab entries that have been a source of endless frustration for new Linux users: floppy and CD-ROM (although these days floppies aren't that important anymore).

/dev/fd0 /media/floppy auto rw,noauto,user,sync 0 0

This line means that the floppy is mounted to /media/floppy by default and that its filesystem type is detected automatically. This is useful because the type of the floppy may wary. Note especially the rw and user options: they must be there if you want to be able to mount and write to the floppy as a normal user. If you have trouble with this, check your fstab file to see if these options are there. Also note the sync option. It can be async just as well, but it's sync because of reasons discussed a bit earlier.

/dev/cdrom /media/cdrom auto ro,noauto,user,exec 0 0

Note, again, the user option that enables you to mount the CD as a normal user. The CD-ROM has the ro option because it's no use mounting a CD-ROM read-write because you wouldn't be able to write to it anyway. Also note the exec option. It's especially useful if you'd like to be able to execute something from your CD.

Also note that the noauto option is used with the floppy and CD-ROM. This means that they won't be automatically mounted when your Linux system boots up. This is useful for removable media, because sometimes there won't be any floppy or CD-ROM when you boot up your system, so there isn't any reason to try to mount something that doesn't even exist.

To add a New Hard Disk in LiNUX

There are total 4 steps involved for hard disk upgrade and installation procedure:

Step #1 : Partition the new disk using fdisk command

Following command will list all detected hard disks: # fdisk -l | grep '^Disk'

Output: Disk /dev/sda: 251.0 GB, 251000193024 bytes Disk /dev/sdb: 251.0 GB, 251000193024 bytes A device name refers to the entire hard disk.

To partition the disk - /dev/sdb, enter: # fdisk /dev/sdb The basic fdisk commands you need are:

  • m - print help
  • p - print the partition table
  • n - create a new partition
  • d - delete a partition
  • q - quit without saving changes
  • w - write the new partition table and exit

Step#2 : Format the new disk using mkfs.ext3 command

To format Linux partitions using ext2fs on the new disk: # mkfs.ext3 /dev/sdb1

Step#3 : Mount the new disk using mount command

First create a mount point /disk1 and use mount command to mount /dev/sdb1, enter: # mkdir /disk1 # mount /dev/sdb1 /disk1 # df -H

Step#4 : Update /etc/fstab file

Open /etc/fstab file, enter: # vi /etc/fstab

Append as follows: /dev/sdb1 /disk1 ext3 defaults 1 2 Save and close the file.

Task: Label the partition

You can label the partition using e2label. For example, if you want to label the new partition /backup, enter # e2label /dev/sdb1 /backup

You can use label name insted of partition name to mount disk using /etc/fstab: LABEL=/backup /disk1 ext3 defaults 1 2

Friday, October 10, 2008

To add a user in CVS server & to add

[root@localhost ~]# useradd gaurav -d /web/ftp/test/gaurav -g test -s /bin/bash [root@localhost ~]# cat /etc/passwd | grep gaurav gaurav:x:8015:514::/web/ftp/test/gaurav:/bin/bash Run this command in client
# CVSROOT=:pserver:YOUR_USER_HERE:PASSWORD@localhost:/root; export CVSROOT

Tuesday, October 7, 2008

Sockets In Python

Sockets in Python

Sockets are the lead pipes of computer networks: they let you connect with other devices so that information can flow freely. As you might expect, they're widely used on the Internet. Peyton McCullough explains how to code sockets in Python.

Introduction

One of the must-know features of any language is sockets, and, no, I’m not talking about the ones positioned on your ceiling. While many novice scripters will run away at the sound of the word, sockets in Python are not hard to learn and work with at all. In this article, I will explain the basics and even apply sockets in a real-world situation.

Please note that this article assumes that you have a basic command of the Python language. If not, you can either read the official tutorial or buy a book – or both. If you’re looking for a good book, you should head over to O’Reilly & Associates.

With that stated, let’s get started.

The Anatomy of a Socket

Let’s start off simple: what is a socket? The easiest way to learn is to compare computer sockets to lead pipes. Let’s say you have some sort of machine that has two pipes. One pipe pumps in water and the other pipe pumps out water. This will be your server. Now, imagine you have a simple box that has one pipe going through it. Let’s say that you hook it up to your pump machine. It will receive the water from the machine and then give water back. This will be your client. If you wanted to, you could hook up more machines and pipes to this to form a network of clients and servers.

Sockets are useful. The Web browser that you are viewing this page with is connecting to the server that this page is in by way of a socket. If you were to connect to one of your favorite online games or chat networks, you would be using sockets as well. As you can see, sockets are both both powerful and widely-used.

Using sockets in Python is quite easy. Open up your Python command line, and let’s get to some code.

The first thing we must do is import the socket library:

>>> import socket

The socket library contains all the tools we need to work with sockets. The next thing we need to do is create a socket. This is simple. Execute the following code:

>>> mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )

This creates a stream socket. You can also work with datagrams by replacing SOCK_STREAM with SOCK_DGRAM. A socket stream is where there is a constant connection between the client and server that stays alive until it is closed, and both the client and the server know if the connection is still alive. With datagrams, however, that is not the case. The connection is not kept alive, and your data might not even be received. Although datagrams can sound like a bad idea at first, they have their purposes. It might be easier and faster to use datagrams in certain situations.

Writing a Simple Server

Let’s write a simple server. If you’ll remember, a server is anything that receives connections from other computers, clients. Create a new Python file named server.py and insert the following code into it:

import socket mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM ) mySocket.bind ( ( '', 2727 ) ) mySocket.listen ( 1 ) while True: channel, details = mySocket.accept() print 'We have opened a connection with', details print channel.recv ( 100 ) channel.send ( 'Green-eyed monster.' ) channel.close()

That’s quite a mouthful, so let’s split it up into something we can understand. The first two lines should already look familiar. We create a new socket to use. In the third line, we open port 2727 for connections.

To understand what a port is, let’s go back to our analogy. Picture the pump machine with thousands of pipes leading in and out of it. Each pipe would be a port, and clients would have the option of connecting to different ports. However, each port would be different – some might pump out green water, and others might pump out orange water.

The next line tells our socket to wait, or listen, for clients. Following that, there is an infinite loop. In this loop, we accept client connections, print out the client's address, print out the client's message and finally send a message back, closing the connection when we're done.

While the socket library is good for many things, there are several libraries built on the socket library that make development easier and faster. Say you wanted to connect to the official Python site to retrieve a piece of information. You could accomplish this task with the socket library, but there's no use working at such a low level when other libraries could do this in a shorter amount of lines. There are libraries for HTTP, FTP, Gopher, telnet and more.

A Real-World Problem and a Pythonic Solution

Let's say that you need to convert United States Dollars into Euros. This may seem complex at first, but it really isn't. It can be accomplished with sockets. We'll use the urllib library to connect to 'http://xe.com' and convert ten dollars into Euros.

Open up the Python command line and import the urllib module:

>>> import urllib

Connecting to the currency converter website I mentioned is simple with the urllib library -- a lot simpler than it would have been if I had used the socket library alone. It can be accomplished with the following line of code:

>>> currency = urllib.urlopen ( 'http://xe.com/ucc/convert.cgi?Amount=10&From=USD&To=EUR' ) >>> data = currency.read()

If you attempt to print out the data variable, you will get a good bit of junk that is not necessary in our script. However, here are the key lines:

<TD ALIGN=RIGHT><FONT FACE="Arial,Helvetica"> <FONT SIZE=+1><B>10.00 USD</B></FONT><FONT SIZE=-1><BR>United States Dollars </FONT></TD> <TD> </TD> <TD ALIGN=CENTER><FONT FACE="Arial,Helvetica" SIZE=+3><B>=</B></FONT></TD> <TD> </TD> <TD ALIGN=LEFT><FONT FACE="Arial,Helvetica"> <FONT SIZE=+1><B>7.46213 EUR</B></FONT><FONT SIZE=-1><BR>Euro </FONT></TD>

( Note that the values above are outdated. )

We can use regular expressions to sort through all the junk and take out the figures we need from the above code:

>>> import re >>> euros = re.search ( '(\d*)\.(\d*) EUR', data ) >>> print '10 United States Dollars are equal to...' >>> print euros.group ( 1 ) + '.' + euros.group ( 2 ), 'Euros.'

We also need to clean up and close the connection like we did earlier:

>>> currency.close()

That's it, we've solved our problem in only a few lines! If we were to do it with the socket library, however, it would have required a number of more lines to accomplish the same result.

You have learned that sockets are a valuable tool in the Python scripting language. They enable you to communicate with other computers, clients and servers, and to exchange data. Sockets are used to accomplish many common computer-related tasks, such as browsing Web pages. Though many novices assume that sockets are difficult, the opposite is true. Python also includes a number of libraries built on top of the socket library that allow you to easily write networking applications.

You now have the knowledge and tools to construct scripts that use sockets. Where you choose to go from here is entirely up to you. However, you may wish to browse through the standard library to find modules to aid development using sockets. Some modules that may help you include the select, asyncore, asynchat, ftplib ( FTP ), poplib ( mail ), imaplib ( mail ), smtplib ( mail ), telnetlib ( telnet ), nnttplib ( news ), SocketServer ( server ), BaseHTTPServer ( HTTP server ), SimpleHTTPServer ( HTTP server ) and CGIHTTPServer ( HTTP server ) modules.