Friday, November 22, 2013

CHNTPW (Change Windows Password from Linux)

​This  chntpw is a tool to access windows registry from linux. This can edit windows login accounts and their passwords. This can also be used to edit the registry similar to regedit. First install this tool in your linux and follow the steps given below.

1. ”cd  /media/.../Windows/System32/config“
​    "/usr/sbin/chntpw -l SAM"
    This will print all user accounts in the windows system.

2. "chntpw  -u  Administrator sam"
     This will open interactive session to edit the user account Administrator.

3. "chntpw  -l  software"
     This will open interactive session to edit the registry software.
     For available commands in the interactive session type "?".

Control Flow Loops in Python

For Loop to reassign items in a list:

>>> s = [1,2,3]
​>>> for n in range(len(s)):
     s[n] = s[n]*2
>>> s
[2, 4, 6]

Call Python Within Shell Script

Shell scripts are easier to write but they lack the features provided by a programming language like python. But instead python can be used within shell scripts. Here are two examples.

​1. To get the file name ‘File’ from the full path ‘Path’:
File=`python -c "print '$Path'.split('/')[-1]"`

2. To get the directory ‘Dir’ from the full path ‘Path’ of file ‘File’:
Dir=`python -c "print '$Path'.rstrip(str('$File'))"`

Python Unicode

#In Python 2.X
#To convert Unicode to character:
​>>> for x in range(0x6000,0x6020,1):
          print unichr(x),  
怀 态 怂 怃 怄 怅 怆 怇 怈 怉 怊 怋 怌 怍 怎 怏 怐 怑 怒 怓 怔 怕 怖 怗 怘 怙 怚 怛 怜 思 怞 怟

​#In Python 3.X:
#To convert Unicode to character:
>>> for x in range(0x6000,0x6020,1):
          print(chr(x),end=' ')
怀 态 怂 怃 怄 怅 怆 怇 怈 怉 怊 怋 怌 怍 怎 怏 怐 怑 怒 怓 怔 怕 怖 怗 怘 怙 怚 怛 怜 思 怞 怟

#To convert character to Unicode:
>>> hex(ord('怀'))
’0×6000′
>>> for n in ‘सत्यमेव जयते’:
          print(ord(n), end=',')
2360,2340,2381,2351,2350,2375,2357,32,2332,2351,2340,2375,

Python Regular Expressions

Python has a ‘re’ module to handle regular expressions. Here are some examples.
​>>> import re
#Example 1: To extract the contents within double quotes in a string
>>> s = 'customer name is "FirstName LastName"'
>>> re.findall('".*"',s)
​['"FirstName LastName"']               #The result is a list

​#Example 2: To extract the individual words from a combined word like ‘FirstName’
>>> s = 'FirstName'
>>> re.findall('[A-Z][a-z]+',s)
['First', 'Name']  #The result is a list.
>>> ''.join(re.findall('[A-Z][a-z]+',s))
'FirstName'      #To get the string from the list​

​#Example 3: To replace all numbers with a ‘.’ suffix’
>>> s = '1 one 11 eleven'
>>> re.sub("(\d)( )","\\1.\\2",s)
​'1. one 11. eleven'