Saturday, August 18, 2012

Interactive selection for ASCII lines using python

Recently I found this nice curses based directory bookmarking tool called cdargs.
I thought it will be nice to have interactive selection for the list. For my bash functions which shows list. Like I have a bash function which can show me a list of clearcase view and then I can choose one of them. That is all bash. But it would have been nice if the list was little interactive and I could have moved around. So I found curses to my help and found another nice tool called iselect.

Iselect does what I need but it does not show the line number like cdargs, so you cannot really select a line by pressing a number. I googled and realized that I can pretty much do the same stuff in python. Since there was no available open source python tool which I can customize to my need, I thought of writing my own. I call it pyselect. It is work in progress and tries to do things like cdargs and iselect. But more customizable and smaller, after all it is scripting in python.

You can checkout the git : https://github.com/jayrajput/pySelect

It is still a work in progress but does not look difficult till now.
The current version as of 18Aug2012, lets you interactively lines fewer than 30.

VIM: Cdargs

Cdargs is a curses based directory bookmarking tool. See http://www.skamphausen.de/cgi-bin/ska/CDargs/

This works nice on bash but I wanted something like that to work with VIM. Tried NERDTree bookmarking but still wanted something like curses. Press a key a list is displayed where you can move around. Something like screen window list shown on pressing C-A+"

I found it that it was way too easy to integrate cdargs with vim using a vim function. The function is self-explanatory. Read the see links and you will be able to decipher the function.

" See http://vim.wikia.com/wiki/Write_your_own_Vim_function
function! Cd()
    exe "!cdargs"
    let mydir = system("cat /home/jay/.cdargsresult")
    " See http://stackoverflow.com/questions/4596932/vim-cd-to-path-stored-in-variable
    cd `=mydir`
endfunction
" Alias to my function so you do not need to type call everytime.
command Cd call Cd()


There is also  VIM plugin which uses cdargs but it does not show the curses window. See: https://github.com/vim-scripts/cdargs

Sunday, August 12, 2012

Check files taking up space



There are multiple way to check for files taking space.  Linux command du seems to be the best.
Here is the du command from the LinuxCommandOrg


du -s * | sort -nr > $HOME/space_report.txt


du also has option which can skip NFS mounted dir. See du man page.

Monday, August 06, 2012

Vim spell check with PHP not working

Vim 7.0 has on the fly spell check which can be enabled by using ":set spell". You may also want to use "sell spelllang=en_us" but ":set spell" just works fine for me.

But this on-the-fly spell was not working with my .php files which have the PHP start tags (less than question mark sign). Without the PHP start tags everything was fine.

Here is the workaround to highlight bad spelling in .php file with PHP start tags:

:set filetype=cpp
:set spell

You lose some of the php filetype goodies but now you have the spell check.

I also found that the php filetype was not working for me as the comments environment variable was getting set to HTML comment style (less than exclamation dashes and dashes greater than) .

Monday, October 03, 2011

Office Communicator 2007 - send message

It would have been nice if there was a command line way of sending message from office communicator. Googled and did not found anything. Finally created a fragile scritp using WSHShell . It is fragile but it works and saves time for me.

option explicit

' Global variable to hold the initials and name mapping.
' Commandline may have initials in place of the name.
dim Names
Set Names = CreateObject("Scripting.Dictionary")
Names.Add "C1", "Contact1"
Names.Add "C2", "Contact2"
Names.Add "C3", "Contact3"

' Global variable used by mySendKeys function.
dim Wsh
set Wsh = Wscript.CreateObject("Wscript.Shell")

if (Wscript.Arguments.Count < 1) Then
Wscript.Echo "Usage: m "
Wscript.Quit(1)
End if

dim name
name = UCASE(Wscript.Arguments(0))
if (Names.Exists(name)) then
name = Names.item(name)
end if

'Wscript.echo name

Wsh.Run "communicator" ' Run communicator
Wscript.Sleep 500 ' Give time for communicator to open.
mySendKeys "{TAB}" ' TAB
mySendKeys "{DOWN}" ' Down Arrow
mySendKeys name ' Search for the contact in my list.
Wscript.Sleep 500 ' Wait for select to happen.
mySendKeys "{ENTER}" ' Enter
Wscript.Quit(0) ' Success

Function mySendKeys(key)
wsh.AppActivate "Office Communicator"
wsh.SendKeys key
End Function

Friday, September 30, 2011

How to get rid of REMOTE HOST IDENTIFICATION HAS CHANGED message

Ever irritated by the message (see below) due to the remote host public key change (remote host was re-installed).

If you are not worried about the security try these in your ~/.ssh/config .
NOTE: This is not secure so make your call. I am working on intranet and do not worry about man in the middle attack.

Host *
# Ignore Host ID changes.
StrictHostKeyChecking no
# Do not store the known hosts.
UserKnownHostsFile /dev/null
# -X option by default.
ForwardX11 yes



Irritating message:
~$ ssh -o "StrictHostKeyChecking no" username@host.domain
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
82:e7:bc:0c:6d:cc:3c:e2:c7:de:ee:2a:b2:af:31:f9.
Please contact your system administrator.
Add correct host key in /home/myacct/.ssh/known_hosts to get rid of this message.
Offending key in /home/myacct/.ssh/known_hosts:5
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.
Permission denied (publickey,gssapi-with-mic,password).

Tuesday, September 27, 2011

inetwiz automation

inetwiz is a tool which I have been using in Windows XP to update the IE proxy setting.
The manual process has been a pain and I wanted to automate it badly.
Experimented with multiple tools - Registry updates, Python. A friend suggested to use VBS.
The VBS did the trick and I was able to use Wscript Run, AppActivate and sendKeys method to automate the proxy update and opening the URL in internet explorer.

Here is the script

option explicit

' Global variable used by mySendKeys function.

dim Wsh

set Wsh = Wscript.CreateObject("Wscript.Shell")

if (Wscript.Arguments.Count < 2) Then

Wscript.Echo "Usage: i /auto|/man URL"

Wscript.Quit(1)

End if

'WScript.Echo (Wsh.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"))

if (Wscript.Arguments(0) = "/man") Then

' VB Script does not support double equal.

' If Proxy is not enabled, enable it.

if (Wsh.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable") = 0) Then

enableManProxy()

End if

Wsh.Run "iexplore " & Wscript.Arguments(1)

Elseif (Wscript.Arguments(0) = "/auto") Then

' If Auto Proxy is not enabled.

' VB Script does not support double equal.

if (Wsh.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable") = 1) Then

enableAutoProxy()

End if

Wsh.Run "iexplore " & Wscript.Arguments(1)

Else

Wscript.Echo "Usage: i /auto|/man URL"

End if

Function enableAutoProxy()

' Enable Automatic Proxy

Wsh.Run "inetwiz"

Wscript.Sleep 1000 ' Give time for application window to open.

mySendKeys "%n" ' Alt + n

mySendKeys " " ' Space

mySendKeys "{Tab}" ' Tab

mySendKeys " " ' Space

mySendKeys "{Tab}" ' Tab

mySendKeys "{Tab}" ' Tab

mySendKeys " " ' Space

mySendKeys "%n" ' Alt + n

mySendKeys "%o" ' Alt + 0

mySendKeys "%n" ' Alt + n

mySendKeys " " ' Space/Finish button

End Function

Function enableManProxy()

Wsh.Run "inetwiz"

Wscript.Sleep 1000 ' Give time for application window to open.

mySendKeys "%n" ' Alt + n

mySendKeys " " ' Space

mySendKeys "{Tab}" ' Tab

mySendKeys " " ' Space

mySendKeys "{Tab}" ' Tab

mySendKeys " " ' Space

mySendKeys "%n" ' Alt + n

mySendKeys "%n" ' Alt + n

mySendKeys "%n" ' Alt + n

mySendKeys "%o" ' Alt + 0

mySendKeys "%n" ' Alt + n

mySendKeys " " ' Space/Finish button

End Function

Function mySendKeys(key)

wsh.AppActivate "Internet Connection Wizard"

wsh.SendKeys key

End Function

Sunday, September 11, 2011

Nested screen sessions

I have been using GNU screens since few years but never thought of using nested screen SESSIONS. Nested screen sessions comes handy when you want to SSH to other server from screen WINDOWS. Here is my recipe for handling nested screen sessions.

* ssh jayrajput@serverA
* screen -e ^Ee # Change escape character from Ctrl-A to Ctrl-E. see escape function in man screen.
* screen -d -m -S serverA # Create a new detached screen session named "serverA" on serverA in detached mode.
* screen -x serverA # Attach to the "serverA" screen session.
* Ctrl-E + c # create a new screen window
* ssh jayrajput@serverB # ssh to serverB
* screen # Run screen on serverB


Use Ctrl-A as command leader in inner screen sessions .
User Ctrl-E as command leader in outer screen sessions.

Setting the hardstatus your .screenrc also comes handy

hardstatus alwayslastline
hardstatus string "%t"

This will show the hostname in the outer screen session status. Do not know why but it does show the hostname.

Tuesday, January 04, 2011

Continuous Ping

ping -t


This is helpful when you do not want to open cmd window and execute ping command from the run prompt.

Wednesday, December 22, 2010

Read File Names from ls -l

Command to print file names from ls -l output:

ls -l | while read a b c d e f g h i ; do echo $i; done

Saturday, October 02, 2010

Learn to Live

Learn to live, learn to let go & follow nature rule


I have always wondered at the almost robotic way we function. what’s the reason to continue a relationship or a venture when there is no emotion or heart in it? I see no reason to go on, if there is no emotion. One has to realise when to move on, when to let go. It’s best to live life as an honest free spirit and challenge those who infringe upon your joy. The conundrum of modern-day life has robbed us of compassion. We have been drained of love and humility. It’s good to remember that life is so easily destroyed, if we are not careful.


Dissatisfaction, sadness, hatred – these are the mind’s creations. You don’t need to serve your connection with the world, if you feel let down. To tackle that, it’s essential that you lead a normal life while trying to find out the root cause of dissatisfied mind. This approach is both realistic and practical and guarantees an answer to most of your deep-seated problems.


Being happy depends not so much on external circumstances as on your inner life. This means all your thoughts, perceptions, beliefs, emotions, desires, dreams – your entire mental and emotional scene. Happiness is about how you react inwardly to events, what you think and believe, how you feel, how problems affect you. It may sound obvious, but like many obvious things it’s something that is often forgotten when it matters most. We focus almost exclusively on or external lives, on getting and spending and having fun and then wonder why we are not happy. But it’s when our inner lives are tranquil that we are most happiest and we call this inner peace.


You can’t completely avoid problems, but you can change how you react to them by acquiring new habits that provoke peaceful inner responses. Training your inner life into different habits require learning skills of thinking, feeling and managing your beliefs and desires. These are very like the virtues many religions and philosophies advocate but if u think of them as skill rather than virtues, you benefit from an important and liberating shift. Instead of ‘ I must become a better person’ you can think ‘I would lie more happily if I work on my skills’. So the change in attitude becomes a choice, not a duty. This process is not something u can do overnight, it’s a whole new way of life but the reward is what we all want most – happiness. There are five main skills who need to cultivate.


Mindfulness:- this involves developing your ability to focus your thoughts in the present. The problem most of us have with thought is having too much of it – the working and non-stop mental chattering our minds are prone to. Mindfulness is a key in our skill because as it gets stronger, it lets you focus on your inner life and catch your habits in the act. Once you can see how u r rolled by them, the change u r seeking often happens of it’s own accord.

Compassion:- most religions rightly stress compassion. As well as being a virtue in its own right it is a practical skill that counter act negative emotions like anger and hatred, which are terrible wreckers of happiness. Tried the next time someone annoys you; put yourself what they might be thinking or feeling to behave like that. Even bad people, let alone people who just mildly annoy you, often have a mistaken view of the world which makes them do what they do.


Story skills:- these are very useful for problems with your inner belie system, as they let you stand back and explore alternative versions of reality. Believes have great power over your life because belief is something your take as fact. Start to think of your believes as stories and it is easier to accept that other things might be true as well. Even true stories only select a little bit of reality we are focusing on at the moment, no one story is the whole truth about any situation. From a different point of view we would see a different t story, sometimes a whole different world. This is not about make beliefs, its about reframing situations to look at them from a different perspective.


Letting – go techniques:- these are particularly helpful when we are unhappy not getting what we want. Generally, we are encouraged to keep wanting and to think that more will make us happier, whether its clothes or cars or even love. But wanting is a treadmill; as long as you have unsatisfied want and desires, or let go some of them. Letting-go skills also include forgiveness, which helps hugely with one of the things you think you want is revenge.


Enjoyment skills:- this last group includes skills such as patience humour and especially gratitude. You don’t have to be grateful to someone, it’s enough to cultivate gratitude or things. Our minds naturally scan the environment for dangers and resources, a useful mechanism when we were hunter-gathers. But it can make us unnecessarily pessimistic – focussing on 10% we lack rather than 90% we have cultivating enjoyment skills will help redress the balance.


Acquiring all his skills takes time and effort. The important thing is to practice them until they operate without you thinking about them. Your practice routine will be very individual because everyone needs to prioritise different skills depending on the specific issues that you are holding them back from being happy but keep the skills in mind and you will constantly find new ways to try them out.

Saturday, April 17, 2010

Skype automation

If you are one of the skype users like me who has to dial the Airtel access number. You know the pain and would like to automate that boring task. Here is the blog which tells more about the automation

http://stevenjoseph.blogspot.com/2008/07/skype-calling-card-script.html

Friday, November 13, 2009

Shell Standard I/O

Q: What is the difference between these two shell statements?

$cmd >> $logFile 2>&1
And
$cmd 2>&1 >> $logFile

Answer:
The first one is right and second one is wrong.

first one redirects any output from the $cmd to the $logFile. The second one just redirects the output and not the STDERR to the $logFile.

Reference:
http://www.linux-mag.com/id/5103

Sunday, November 01, 2009

Moral Dilemma

Q: How to decide if an action is morally right or wrong? E.g.
1. Taking a personal prinout using office printer?
2. Making a phone call using office phone ?
3. Stealing salad from the salad bar at office canteen?
4. Keeping the IPOD nano 4 GB found on a street at halloween pary on franklin street with urself?

Ans: If you can tell all your friends and family members about the action then the action should be morally right.
1. Should be OK.
2. Should be OK.
3. WRONG.
4. Dicey.

Thursday, October 15, 2009

It is Democracy...So why not create own phrases, sentences and syndromes like others. So here is one from me:

Finish it syndrome.

The syndrome is one in which the DIET conscious person end up eating more in following scenario:
1. Person end up taking more cereal as he wanted to finish the contents of the cereal box. Why not use it next item it is not going to get perished.
2. Person end up taking more serving as he did not wanted the food to be wasted. Why not use the zip- lock bags..:-)

Thursday, October 08, 2009

Motivation for fast running

I always wanted to run fast but was never motivated. But then I found something which motivated me.

I read somewhere that running fast makes your abs and who does not want to have six pack abs.
Six pack abs are enough reason for me to be motivated for fast running.

Sunday, July 05, 2009

GDB handy commands

Multi-threaded

info threads

Signals

info signals
// To pass the signal from GDB to the process.
handle SIGINT pass

To Debug a running process using GDB

gdb

Tuesday, May 19, 2009

VIM : Comments lines with a pattern in a file

The lines in a file containing a specific pattern can be commented in VIM using the :g command.
:g command is used in following form
:g/[patern]/[cmd]

Example:
:g/JR/normal I //

explanation
Pattern : JR
Cmd : normal I //

"I" is used in VIM to start insert mode at the start of line and "//" is the comment character used in PHP.

Enjoy VIM. Check out the help :g if you have more questions.

Monday, March 30, 2009

Toggle Comment in VIM in one line using perl

There are lot of scripts in VIM which can toggle comments for you. But interestingly there is not a one liner which I can execute on systems where I do not have my vimrc file.

try this mapping

:map F2 V:!perl -nle 'if (/^\s*\/\//) { s/\s*\/\/\s*(.*)/$1/g; print } else {print "//".$_}'^Mj

You can map Alt+C or Ctrl+T something which is faster than F2

This mapping will comment and uncomment the C-style comment. I have tried this on Ubuntu and VIM 7.1 and works fine for me

Note: The map command may not work on copying as the VIM may not recognize the Ctrl - M character. You have to Ctrl- V and then Enter to make it work

Friday, March 13, 2009

Singleton versus Static

These are rules which I use to decide where or not my class will be a Singleton or Static:
  1. Control over construction. Timing issues.
  2. Polymorphism.
  3. One versus multiple instances.