Wednesday 28 November 2012

Solution: After Exception Catch in Python What will happen?

Exception Handling :
After Exception Catch Python will execute remaining code, See below Example 



#!/usr/bin/python3
# reading sitemap xml file
#

a=5

def diverror(a):
    a = a/0
    print(a)

try:
diverror(a)
except:
print("Division Error")
a = a+10
print(a)

Output:

Division Error
15





Read More

Break out of the function in Python

In Python if Any function return any thing or return none means the function is completed ie Break out of the function 

Example:

def test(a,b):
      return "HI Closed"
      a = b+10
      print(a)

test(10,11)
output:
HI Closed

Read More

Catch all exception in Python3

to catch all exceptions in python3 simple do like bellow

try:
    code
except:
    print("Unknown Exception found")

Read More

Sunday 25 November 2012

Python current timestamp

To get current time with date in python

from datetime import datetime
curtime = str(datetime.now())


output : '2012-11-25 14:58:30.465213'



Read More

Friday 23 November 2012

Solution: Extract tar.bz2 files in Linux

To Extract tar.bz2 files in linux use below command

root@system1~# tar -xjvf filename.tar.bz2
Read More

Wednesday 21 November 2012

Delete duplicate rows in mysql table

It is Possible to DELETE duplicate rows in MYSQL table based on Particular Field (Coloumn)

the SQL Query is

ALTER IGNORE TABLE table_name ADD UNIQUE INDEX(coloumn_name); 
Read More

Thursday 15 November 2012

IPtables: Saving firewall rules to /etc/sysconfig/iptables: /etc/init.d/iptables: line 268: restorecon: command not found

Solution to : IPtables: Saving firewall rules to /etc/sysconfig/iptables: /etc/init.d/iptables: line 268: restorecon: command not found

root@server1:~# ls -l /sbin/ | grep restore
lrwxrwxrwx 1 root root     14 May 23 17:52 iptables-restore -> iptables-multi

Means policycoreutils not Installed
So Install policycoreutils to get restorecon save Iptables


root@server1:~# yum install policycoreutils
root@server1:~# ls -l /sbin/ | grep restore

lrwxrwxrwx 1 root root     14 May 23 17:52 iptables-restore -> iptables-multi
lrwxrwxrwx 1 root root      8 Nov 15 17:00 restorecon -> setfiles



Read More

Tuesday 13 November 2012

Read Large Files Line by Line in Python - Best Way

The readlines() function loads the entire file into memory as it runs. A better approach for large files is to use the fileinput module, as follows:

import fileinput
for line in fileinput.input(['myfile']):
   print(line)
Read More

Monday 12 November 2012

Solution: Dovecot error : Unknown database driver mysql


Dovecot error : Unknown database driver mysql because Dovecot requires the dovecot-mysql package to run mysql authentication. So Install  dovecot-mysql
yum install dovecot-mysql
Read More

Friday 9 November 2012

Load Kernel Module in Linux Example Load IP table module

Below Instructions shows How to Load Kernel module in Linux

check whether module is Loaded or Not

if you don't know Exact Module Name then Check All Loaded Modules

root@system1:~# lsmod

Module                  Size  Used by
nf_conntrack_ipv4      18850  1 
nf_defrag_ipv4         12601  1 nf_conntrack_ipv4
xt_state               12514  1 
nf_conntrack_ftp       13039  0 
nf_conntrack           65203  3 nf_conntrack_ipv4,xt_state,nf_conntrack_ftp
xt_tcpudp              12531  1 
x_tables               20679  4 xt_state,xt_tcpudp,iptable_filter,ip_tables
sctp                  165508  2 
libcrc32c              12510  1 sctp
nls_iso8859_1          12617  0 
nls_cp437              12751  0 
vfat                   17166  0 
fat                    54411  1 vfat
snd_seq_dummy          12686  0 
usb_storage            42478  0 
uas                    17476  0 
i915                  395739  1 
drm_kms_helper         30737  1 i915
drm                   183453  3 i915,drm_kms_helper
i2c_algo_bit           12980  1 i915
video                  18434  1 i915
dm_crypt               22236  0 
snd_intel8x0           33017  2 
snd_ac97_codec        104623  1 snd_intel8x0
ac97_bus               12602  1 snd_ac97_codec
snd_pcm                72878  2 snd_intel8x0,snd_ac97_codec
snd_seq_midi           13132  0 
snd_rawmidi            24215  1 snd_seq_midi
snd_seq_midi_event     14076  1 snd_seq_midi
snd_seq                50403  3 snd_seq_dummy,snd_seq_midi,snd_seq_midi_event
snd_timer              23911  2 snd_pcm,snd_seq
snd_seq_device         13817  4 snd_seq_dummy,snd_seq_midi,snd_rawmidi,snd_seq
ppdev                  12840  0 
lp                     13321  0 
parport_pc             31867  1 
shpchp                 32187  0 
snd                    52787  11 snd_intel8x0,snd_ac97_codec,snd_pcm,snd_rawmidi,snd_seq,snd_timer,snd_seq_device
psmouse                72465  0 
parport                34960  3 ppdev,lp,parport_pc
soundcore              12534  1 snd
mac_hid                13037  0 
serio_raw              13027  0 
snd_page_alloc         13709  2 snd_intel8x0,snd_pcm
dm_mirror              21585  0 
dm_region_hash         15035  1 dm_mirror
dm_log                 17871  2 dm_mirror,dm_region_hash
aufs                  162169  0 
usbhid                 41119  0 
hid                    79842  1 usbhid
tg3                   133921  0 
floppy                 59430  0 

if you know what module you are Looking ex:ip_tables then Check whether that module loaded or not by below command

root@system1:~# lsmod | grep ip_tables
root@system1:~#

Not Loaded then Load Module

root@system1:~# modprobe ip_tables

Now check

root@bt:~# lsmod | grep ip_tables
ip_tables              17634  1 iptable_filter
x_tables               20679  4 xt_state,xt_tcpudp,iptable_filter,ip_tables

Success





Read More

Tuesday 6 November 2012

Solution to g++: Command not found in Fedora Problem

g++ command not found means gcc-c++ package not 

installed so install gcc-c++ package

command

root@system1:# yum install gcc-c++
Read More

Install deb packages

To install .deb packages use following command

root@system1:~# dpkg -i package.deb
Read More

SSH login without password in Linux

Hi to login into Linux System through SSH with out password Follow Below Steps
two systems (Below IP addresses are for Example)
system1 ip : 2.2.2.2
system2 ip : 3.3.3.3

Note: For Below Command don't type any thing Simply press Enter


root@system1:~# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): (Press Enter)
Enter passphrase (empty for no passphrase): (Press Enter)
Enter same passphrase again: (Press Enter)
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
d8:b7:3b:d8:12:19:47:de:43:e9:8b:54:ba:b4:af:c4 root@system1
The key's randomart image is:
+--[ RSA 2048]----+
|           ..    |
|          ...o   |
|          .+* .  |
|       o oo* .   |
|      . S ....    |
|         + +.+.   |
|         oE.o    |
|        ..oo     |
|          o.     |
+-----------------+

root@system1:~# ssh-copy-id  3.3.3.3
root@3.3.3.3's password: 

Now try logging into the machine, with "ssh '3.3.3.3'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

root@system1:~# ssh 3.3.3.3
(If Login success OK,if Login Fail and Got Below Error )

Agent admitted failure to sign using the key.
root@3.3.3.3's password: 

root@system1:~# ssh-add
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

Now
root@system1:~# ssh 3.3.3.3
Last login: Wed Nov  7 10:27:23 2012 from 2.2.2.2
root@system2:~#

Success









Read More

Monday 5 November 2012

convert to binary in PHP

In PHP decbin function is used to convert anything to binary

Example:
<?php
$a = "3";
$b = 4;
echo decbin($a)."<br>";
echo decbin($b);
?>

Output:
11
100
Read More
Here We Write The Problems we face at different situations and Solutions to those problems.