Wednesday, November 30, 2011

Sample configuration files - vimrc, emacs & Xdefaults

Sample ~/.vimrc file
set nocompatible
set nu incsearch ic
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
colorscheme desert
set formatoptions=t
set textwidth=190
set encoding=utf-8
set termencoding=latin1
set fileformat=unix
"set guifont=courier_new:h10
set guifont=Courier\ New:h10,Courier,Lucida\ Console,Letter\ Gothic,
 \Arial\ Alternative,Bitstream\ Vera\ Sans\ Mono,OCR\ A\ Extended
set nowrap
set shiftwidth=2
set vb t_vb=
set novisualbell
set number
set autoindent
set ruler
set expandtab
set whichwrap=<,>,h,l
set guioptions=bgmrL
set backspace=2
set history=50
set backup
set wildmenu
set nrformats=
set foldlevelstart=99
if has("unix")
  set shcf=-ic
endif

let mapleader = ","
let $ADDED = '~/.vim/added/'
if has("win32")
  let $ADDED = $VIM.'/added/'
endif

map <Leader>cd :exe 'cd ' . expand ("%:p:h")<CR>
nmap <F1> :w<CR>
imap <F1> <ESC>:w<CR>a
map <F8> gg"+yG

set diffexpr=MyDiff()
function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let eq = ''
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      let cmd = '""' . $VIMRUNTIME . '\diff"'
      let eq = '"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
VIM Screenshot -
Sample ~/.emacs file
(global-set-key "\C-xv" 'quoted-insert)
(global-set-key "\C-xc" 'compile)
(global-set-key "\C-xt" 'text-mode);
(global-set-key "\C-xa" 'repeat-complex-command);
(global-set-key "\C-xm" 'manual-entry);
(global-set-key "\C-xw" 'what-line);
(global-set-key "\C-x\C-u" 'shell);
(global-set-key "\C-x0" 'overwrite-mode);
(global-set-key "\C-x\C-r" 'toggle-read-only);
(global-set-key "\C-t" 'kill-word);
(global-set-key "\C-u" 'backward-word);
(global-set-key "\C-h" 'backward-delete-char-untabify);
(global-set-key "\C-x\C-m" 'not-modified);
(setq text-mode-hook 'turn-on-auto-fill)

;; to enable line numbers
(global-linum-mode 1)
;; do off wrapping lines
(setq-default truncate-lines t)
;; to disable start up message
(setq inhibit-startup-message t)
;; to enable column mode
(setq column-number-mode t)

;; for jsp and html mode open with nxml mode
(setq auto-mode-alist (cons '("\\.html$" . nxml-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.jsp$" . nxml-mode) auto-mode-alist))

;; To remove duplicate lines
(defun uniquify-all-lines-region (start end)
      "Find duplicate lines in region START to END keeping first occurrence."
          (interactive "*r")
              (save-excursion
                   (let ((end (copy-marker end)))
                        (while
                           (progn
                             (goto-char start)
                               (re-search-forward "^\\(.*\\)\n\\(\\(.*\n\\)*\\)\\1\n" end t))
                               (replace-match "\\1\n\\2")))))

  (defun remove-duplicate-lines ()
        "Delete duplicate lines in buffer and keep first occurrence."
            (interactive "*")
                (uniquify-all-lines-region (point-min) (point-max)))

Sample ~/.Xdefaults file
emacs*pointerColor: Orchid
emacs*bitmapIcon: on
emacs.geometry: 180x50

Emacs.background: ivory
Emacs.foreground: MidnightBlue

Emacs.modeline*attributeForeground:     MidnightBlue
Emacs.modeline*attributeBackground:     Bisque

Emacs.text-cursor*attributeBackground:  Red3
Emacs.pointer*attributeForeground:      Red3
Emacs.pointer*attributeBackground:      Red3
Emacs*menubar*Foreground:               MidnightBlue
Emacs*menubar*Background:               Bisque1
Emacs*popup*Foreground:                 MidnightBlue
Emacs*popup*Background:                 Bisque1

Emacs.default.attributeFont:  -adobe-courier-medium-r-*-*-12-*-*-*-*-*-iso8859-*

xterm*geometry:        130x40
xterm*faceName:        monofur
xterm*faceSize:        12
xterm*background:    #222222
xterm*foreground:    #ddccbb
xterm*metaSendsEscape:    true
xterm*colorBD:        #ffffff
xterm*colorBDMode:    true
xterm*cursorColor:    #ff9900
xterm*veryBoldColors:    14
xterm*loginShell:    true
xterm*charClass:    33:48,35:48,37:48,43:48,45-47:48,64:48,95:48,126:48,35:48,58:48
xterm*jumpScroll:    true
xterm*multiScroll:    true
xterm*boldMode:        true

Emacs screenshot

xterm screenshot



VIM vs EMACS

Comparison between VIM and EMACS commands.
In EMACS - C means "Control" key and M means "Alt" key.
In VIM - most of the commands are done in escape mode unless specifically mentioned.

VIM EMACS
Navigation
j - move one line down
k - move one line up
l - move cursor right
h - move cursor left
w - move cursor one word forward
b - move cursor one word backward

H moves cursor to HOME of the window
M moves cursor to MIDDLE of the window
L moves cursor to BOTTOM of the window

gg - go to beginning of the buffer
G - go to end of the buffer

0 or ^ - go to start of line
$ - go to end of line

Ctrl-f to forward one page
Ctrl-b to backward one page

ESC - to kill current command
n <cmd> to repeat command n times

:set number to show line numbers
To load by default (:set number) in .vimrc

Wrap lines
:set nowrap - to remove wrapping of lines
:set wrap - to enable wrapping of lines

:shell or sh go to shell
exit to come back to vi

:n to jump to line n

% find matching parenthesis


(insert mode)Ctrl+p or Ctrl+n for word completion

:sort to sort

:set textwidth=190
(come to next line once 190 column is reached


Navigation
C-n move one line down
C-p move one line up
C-f move cursor right
C-n move cursor left
M-f move cursor one word forward
M-b move cursor one word backward

M-0 M-r moves cursor to HOME of the window
M-m M-r moves cursor to MIDDLE of the window
M-- M-r moves cursor to BOTTOM of the window

M-< or ESC-< go to beginning of the buffer
M-> or ESC-> go to end of the buffer

C-a go to start of line
C-e go to end of line

C-v to forward one page
M-v to backward one page

C-g to kill current command
M-n <cmd> to repeat command n times

M-x global-linum-mode
To load by default (global-linum-mode 1) in .emacs

Wrap lines
M-x toggle-truncate-lines
(setq-default truncate-lines t)

M-x shell go to shell
Use C-x right or left to go next or previous buffer

M-g g n to go to line n

C-M-f find matching parenthesis forward
C-M-b find matching parenthesis backward

M-/ for word completion

M-x sort-lines to sort

M-x line-number-mode
M-x column-number-mode
C-x f to set column width
M-q to wrap the current paragraph with column width 
Undo
u - undo
Ctrl-r - redo
Undo
C-_  or C-x u or C-/ to undo

Search 
:set incsearch to set increment search
:set ignorecase to set ignore case while search

/word to search forward
n- go to next match
N- go to previous match

?word to search backward
n- go to next match
N- go to previous match

By default regexp search


* or gd - to search word under cursor forward
# - to search word under cursor backward
Search
By default increment search
By default ignore case while search

C-s to search forward
C-s go to next match
C-r go to previous match

C-r to search backward
C-r go to next match
C-s go to previous match

Use C-M-s or C-M-r for regexp search.
or Esc-C-r or Esc-C-s

C-s C-w to search word under cursor forward
C-r C-w to search word under cursor backward
Column editing 
Ctrl+q or Ctrl+v to start column editing
I <any-character> ESC - to start insert at start of the block
A <any-character> ESC - to start insert at end of the block
h/j/k/l to select the block.
d - cut the selected region
y - yank the selected region
gv - to select the previous visual selection.












Column editing
M-x cua-mode to enable column editing (toggles)
C-RET to start the selection (toggles)
once mark is set traverse using C-n/p/f/b/a/e to select region
After selection
C-x to cut the selected region
C-c or M-w to copy the region
C-y or C-v to paste the region
Start typing having mouse at the start of the
   region to insert at the start of region
Same goes to end of the region
Ret to move around the corners of the region.

Rectangles -
C-Space to set mark
move to any other region,
C-x r k to kill the region
C-x r y to paste that region
C-x r c to clear the region with spaces
C-x r t String to replace rectangle block with String
C-x r r a to copy the rectangle area to register a
C-x r i a to paste the rectangle area to register a
Replace
:%s/xyz/abc/g  - to replace xyz with abc
:%s/xyz/abc/gc - to replace xyz with abc with confirmation.





Replace
M-x replace-string RET xyz RET abc RET to replace xyz with abc
M-x query-replace RET xyz RET abc RET to replace xyz with abc
by asking user
<SPACE> to replace
<DEL> to skip
! to replace others

M-% to replace same as M-x query-replace
Indenting XML
:set ft=xml
:%s/>\s*</>\r</g
ggVG=
J - to join line with one space
gJ - to join lines with no space
Indenting XML
M-x nxml-mode
M-x replace-regexp RET  > *< RET >C-q C-j< RET
C-M-\ to indent
M-x replace-regexp RET C-q C-j RET Space RET join lines 1 space
M-x replace-regexp RET C-q C-j RET RET to join lines no space
Cut, Copy and Paste
0D or ^D - cut from cursor to end of line (no newline)
dd or Vd - cut current cursor to end of line (including current line)
D - cut from current cursor to end of line (no newline)
x  - to delete character under cursor
dw - delete next word
yy or Vy - to copy current line (including current line)
ddp - swap current line and next line







Cut, Copy and Paste
C-k cut from current cursor to end of the line (no newline)
C-k C-k to cut the whole line (including new line)
C-Shift-n C-w to cut the current line (including new line)
M-k cut from current cursor to end of sentence
C-d to delete next character
<Delete> to delete previous character
M-d to delete next word
M-<Delete> to delete previous words
C-y paste
C-@ or C-Space mark set
C-w cut from mark set to cursor
M-w Copy from mark set to cursor
C-a C-Space C-n M-w to copy whole line  (including new line)
C-Shift-n M-w to copy the whole line (including new line)
C-a C-Space C-n C-w C-n C-y swap current line and next line
Dealing with case
gUU - to make whole current line to upper case
guu - to make whole current line to lower case
ggVGU - to make whole file to upper case
ggVGu - to make whole file to lower case
~ - to invert case.

Dealing with case
C-a C-Space C-n C-x C-u - to make whole current line to uppercase
C-a C-Space C-n C-x C-l - to make whole current line to lowercase
M-< C-Space M-> C-x C-u - to make whole file to upper case
M-< C-Space M-> C-x C-l - to make whole file to lower case
M-l to make next word lower case
M-u to make next word upper case
Dealing with files & Buffers
:e filename/vi filename to open file in vi
:view filename to only view the file. restricted to write
:set ro readonly mode
:set ro! readonly mode
:enew to edit new file
:sp to edit same file in H split
:vsp to edit same file in V split
:new to edit new file in H split
:vnew to edit new file in V split
:tabe to edit new file in new tab
:e! return to unmodified file
:q to quit
:q! to quit without save
:wq to save and quit
:w to write and be there in program
:x to write and exit
:x! not to write and exit
ZZ to save and exit in command mode (capital Z)
:w filename to write the content on file
:w! filename2 to overwrite the existing filename to filename2

:ls to list all the buffers
:buffers to list all buffers
:buffer n to jump to buffer n
:bp to move to previous buffer
:bn to move to next buffer
:bd to remove from buffer
:args **/* to keep all files in the current directory in vim (SUPER)
:rew to first file in args
:te Buffers to show the menu buffer
:badd add file to buffer
:b# to alternate buffer
Ctrl-^ is to switch alternate buffers
:tab ball to open all buffers in tabs
:ball to open all buffers Horizontally
:vert ball to open all buffers vertically

ctrl+w o keep open only the current window
ctrl+w w to switch between windows
ctrl-w-v to open a new window vertically
ctrl-w-n to open a new window horizontally

ctrl-w + increase the size of window
ctrl-w - decrease the size of window
ctrl-w = to equalize the size of each split window
ctrl-w | to maximize in vertical split
ctrl-w _ to maximize in Horizontal split
ctrl-w h to move to the window left
ctrl-w j to move to the window below
ctrl-w k to move to the window above
ctrl-w l to move to the window right
ctrl-w H to move current window to far right
ctrl-w J to move current window to far Down
ctrl-w K to move current window to far Up
ctrl-w L to move current window to far left
ctrl-w T to move current window to new tab
ctrl-w n to open new file in H split
ctrl-w q to close
ctrl-w o to close all splits except current window (:only also do the same)
Dealing with files & Buffers
C-x C-f to find the file to load into buffer
C-x C-q to only view the file. restricted to write (toggles)
C-x b *scratch* to edit new file
C-x 2 to split horizontally same file
C-x 3 to split vertically same file
C-x 4 C-f *scratch* to open file in H split
C-x 4 b buffer to open buffer in V split
C-x 3 C-x b buffer to open new buffer in V split
C-x C-s to save buffer to file
C-x s to save some buffers
C-c C-x to exit emacs



C-x C-b to list all buffers
C-x <Right> go to next buffer
C-x <Left> go to previous buffer
C-x k to kill current buffer
C-x b <buffer name> to switch to that buffer
C-x h select everything in buffer
M-x buffer-menu to list all buffers in separate window
~ clear modified flag on that buffer
k mark buffer for delete
x to delete all marked buffers
o to open the buffer in below split window
Note - * says it is modified, % means it is read only file
C-x 1 keep open only the current window
M-x make-frame same as above
C-x o to switch between windows
C-M-v to scroll the other window

C-x } to increase the size of window horizontally
C-x { to decrease the size of window vertically





























Registers
Marking registers -
ma - to mark position on a
'a - to jump to position a

Copying & paste to registers -
"ayy - yank current line to register a
"Ayy - append current line to register a
V"ay - in visual mode, to register a
V"Ay - append current selection to register a
"ap to paste content of register a
:reg to view all registers
:reg a to view contents in register a
Registers
Marking registers -
C-x r Space a - to mark position on a
C-x r j a - to jump to position on a  

Copying & paste to registers -
C-x r s a yank selected region to register a
C-x r i a insert selected region
M-x view-register <CR> a to view contents of register a
M-x append-to-register to append selected region to register a
M-x prepend-to-register to prepend selected region to register a
C-x r r a to copy the rectangle area to register a
C-x r i a to paste the rectangle area to register a
To remove duplicate lines
:sort u to remove duplicate lines




















To remove duplicate lines
Keep below functions in .emacs file
;; To remove duplicate lines
(defun uniquify-all-lines-region (start end)
      "Find duplicate lines in region START to END keeping first occurrence."
          (interactive "*r")
              (save-excursion
                   (let ((end (copy-marker end)))
                        (while
                           (progn
                             (goto-char start)
                               (re-search-forward "^\\(.*\\)\n\\(\\(.*\n\\)*\\)\\1\n" end t))
                               (replace-match "\\1\n\\2")))))

  (defun remove-duplicate-lines ()
        "Delete duplicate lines in buffer and keep first occurrence."
            (interactive "*")
                (uniquify-all-lines-region (point-min) (point-max)))


M-x remove-duplicate-lines
To delete matching lines
:g/<regexp>/d to delete matching lines
To delete empty lines
:g/^\s*$/d



To delete matching lines
M-< M-x delete-matching-lines RET <regexp> RET
To delete empty lines
M-< M-x delete-matching-lines RET ^ *$ RET
Note: there is space between ^ and *
or
M-< M-x flush-lines RET <regexp> RET
To delete non-matching lines
:v/<regexp>/d to delete non-matching lines


To delete non-matching lines
M-< M-x delete-non-matching-lines RET <regexp> RET
or
M-< M-x keep-lines RET <regexp> RET
To copy matching lines
:g/<regexp>/y A to copy matching lines to register a
"ap to paste matching lines




















To copy matching lines
M-< M-x keep-lines RET <regexp> RET
C-x h C-x r s a copy matching lines to register a
C-x r i a to paste matching lines

function to copy matching lines to buffer
reference - http://stackoverflow.com/questions/2289883/emacs-copy-matching-lines
;; To copy matching lines to another buffer *matched*
(defun copy-lines-matching (re)
    "find all lines matching the regexp RE in the current buffer
  putting the matching lines in a buffer named *matching*"
    (interactive "sRegexp to match: ")
      (let ((result-buffer (get-buffer-create "*matching*")))
           (with-current-buffer result-buffer (erase-buffer))
           (save-match-data
             (save-excursion
               (goto-char (point-min))
               (while (re-search-forward re nil t)
                   (princ (buffer-substring-no-properties (line-beginning-position) (line-beginning-position 2))
                          result-buffer))))
                    (pop-to-buffer result-buffer)))

Thursday, October 13, 2011

Traversing current command and command history

In BASH, by default emacs mode is enabled. (set -o emacs)

Traversing the Command
ctrl+f to go forward
ctrl+b to go backward
ctrl+a to go start of the line
ctrl+e to go end of the line
ctrl+p to go previous command
ctrl+n to go next command
ctrl+w to delete word backward
ctrl+k to delete from cursor to end of line
ctrl+i to search for previous commands
ctrl+d to delete letter under cursor
ctrl+l to clear the screen
ctrl+r increment search backwards, use ctrl+r to go next.

Switching between directories
"cd -" is used to switch between directories current $PWD and  $OLDPWD (where PWD is current working directory and OLDPWD is previous working directory).
cd -
for example - 
# Current directory - /home/rajashekr/Downloads
rajashekr@ubuntu:~/Downloads$ pwd
/home/rajashekr/Downloads

# Going to home directory -
rajashekr@ubuntu:~/Downloads$ cd

# To go to previous directory
rajashekr@ubuntu:~$ cd -
/home/rajashekr/Downloads

use cd- to go back to previous director

To execute previous commands
In general, you can navigate thru history by using up and down arrows
Using "!" to execute previous commands.
!<previous-command-starting-letters>
For example -
rajashekr@ubuntu:~/Downloads$ find . -name "*.txt"
./vmware-tools-distrib/doc/open_source_licenses.txt

rajashekr@ubuntu:~/Downloads$ ls
VMwareTools-8.4.7-416484.tar.gz  vmware-tools-distrib

rajashekr@ubuntu:~/Downloads$ vi test.txt

# execute previous command that start with fi
rajashekr@ubuntu:~/Downloads$ !fi
find . -name "*.txt"
./vmware-tools-distrib/doc/open_source_licenses.txt
./test.txt

Other way is to have number of the command in history
!<number-of-the-command-in-history>
$ history | grep -i find
   66  find . -name "*.java"
   67  find . -name "*.java" | grep -i paymentsummary_005fordersummary_005fwp_005ffg_jsp.java

$ !67
find . -name "*.java" | grep -i paymentsummary_005fordersummary_005fwp_005ffg_jsp.java

Tip: You can use alias.
For example: alias gh='history | grep '

$ gh find
   66  find . -name "*.java"
   67  find . -name "*.java" | grep -i paymentsummary_005fordersummary_005fwp_005ffg_jsp.java

Search previous commands with matching pattern
!?<previous-command-substring>
For example:
$ find . -name "*.txt"
./.#log.txt
./log.txt

$ find . -name "*.jsp"

$!?txt
find . -name "*.txt"
./.#log.txt
./log.txt

Use ctrl+r increment search backwards, use ctrl+r to go next.

Passing all previous arguments to current command
!*
For example -
rajashekr@ubuntu:~/Downloads$ echo test > test.txt
rajashekr@ubuntu:~/Downloads$ more test.txt
test
rajashekr@ubuntu:~/Downloads$ cat !*
cat test.txt
test

Another example :
$ touch sample.txt
$ mv !* !*.bak
mv sample.txt sample.txt.bak 

Passing only previous command last argument to current command

!$
Example:
$ echo test1 test2
test1 test2
echo !$
echo test2
test2
Substitue previous command
!!
For example:
$ find . -name "web.xml"
./actint.war/WEB-INF/web.xml
./atg_admin.war/WEB-INF/web.xml
./atg_bootstrap.war/WEB-INF/web.xml

$ grep -i atg.filter.dspjsp.PageFilter `!!`
grep -i atg.filter.dspjsp.PageFilter `find . -name "web.xml"`
./actint.war/WEB-INF/web.xml:        <filter-class>atg.filter.dspjsp.PageFilter</filter-class>
./atg_bootstrap.war/WEB-INF/web.xml:<filter-class>atg.filter.dspjsp.PageFilter</filter-class>
./common.war/WEB-INF/web.xml:        <filter-class>atg.filter.dspjsp.PageFilter</filter-class>

Substitute substring in previous command
!!:s/old/new/
or
^old^new
Example:
$echo test1
test1
$!!:s/test1/test2/
echo test2
test2
$ ^test2^test1
echo test1
test1

!!:gs/old/new/ does the same as above globally

!! is previous command
!!:0 is previous command first word
!!:1 is previous command second word

!# current command
!#:0 current command first word
mkdir test1; cd !#:1 to create directory and cd to that directory
for above !! and !# use :h for head of the word and :t for the tail of the word

To switch to vi mode
set -o vi - to have vi mode
Go through below commands to use vi keys

In ksh - Korn Shell
Make sure that you have
To enable "vi" key commands
set -o vi
To enable tab completion
set -o viraw

To navigate history
<Esc> and then use j,k to go up and down respectively
To edit current command
Use <Esc> use h,l to go previous and next letter.
Use "x" to delete current letter,
Use "r" to replace current letter.
To search previous commands
<Esc>/awk - to find previous commands which has awk in it
"n" - to go next command
"N" - to go previous command

To use "vi" to edit current buffer
<Esc>v to open current command buffer and
"wq" to execute command.
Please note that you can also set "set -o vi" in bash too and have above features.


References - mark.stosberg.com/Tech/tips/bash.tips

Friday, February 18, 2011

How to auto login (ssh) to every server and search logs.

For example: if the environment set up like following
There are 9 boxes from usppre11 to usppre19
logs are stored in /tmp
$ ls -ltr
-rw-r--r--   1 admin atg      16455232 Sep 28 17:15 wls_XXX_AdminServer_XXX_stdout.txt.4728104734
-rw-r--r--   1 admin atg      17439042 Sep 29 08:44 wls_XXX_AdminServer_XXX_stdout.txt.1628171655
-rw-r--r--   1 admin atg      20182090 Sep 29 16:31 wls_XXX_AdminServer_XXX_stdout.txt.4629084624
-rw-r--r--   1 admin atg      26107020 Oct  1 15:04 wls_XXX_AdminServer_XXX_stdout.txt.3329163306
-rw-r--r--   1 admin atg      41899784 Oct  6 13:31 wls_XXX_AdminServer_XXX_stdout.txt.1501171516

grepall.sh
#!/usr/bin/expect -f
#!/bin/bash

# search string
set searchstring  [lrange $argv 0 0]
# month MMM format example: Aug
set month [lrange $argv 1 1]
# day dd example  28 or 1
set day [lrange $argv 2 2]
# to list lines - l or just to know the count c
set arg1 [lrange $argv 3 3]
# set password
set pass wlsview
if {${arg1} == "c"} {
  set cmd "grep -i ${searchstring} \$(ls -ltr | grep -i '${month}.*${day}.*wls.*AdminServer.*stdout' | sed -n 's/^.*wls/wls/p') | wc -l"
}
if {${arg1} == "l"} {
  set cmd "grep -i ${searchstring} \$(ls -ltr | grep -i '${month}.*${day}.*wls.*AdminServer.*stdout' | sed -n 's/^.*wls/wls/p')"
}
for {set i 1} {$i < 10} {incr i 1} {
 spawn ssh -o StrictHostKeyChecking=no username@usppre1$i
 expect -re "Password:"
 send "${pass}\r"
 send "cd /tmp\r"
 send "${cmd}\r"
 send "\r"
 send "\r"
 send "exit\r"
 expect eof
}

Wednesday, October 13, 2010

How to find the exact instance with session id in Weblogic cluster using curl or lynx

Suppose we have 90 weblogic app server instances running behind the load balancers and AKAMAI.
If a user faces an error/exception in one scenario. If we have the user session id (For example: xGGwM2Hpq0lhCr!-1529204333)
Note: For each server we will have jvm id. The part after delimeter !- is jvmid or serverid which will not be changed until server restarts.

To find the exact server so that we directly jump to exact logs. Instead of going through all 90 instances.
Using lynx - findhost.sh
#!/bin/bash
# Iterate through each host
for i in {20..109};
do
  # get jvmid or server id the part after delimeter !- in session id
  jvmid=$(lynx -dump -head http://host$i:7001/business/ | grep -i session  | sed 's/^.*!\|^.*-\|;.*//g')
  echo "p2pre1c$i : $jvmid"
done
or
Using curl - findhost.sh
#!/bin/bash
# Iterate through each host
for i in {20..109};
do
  # get jvmid or server id the part after delimeter !- in session id
  jvmid=$(curl --head --silent http://host$i:7001/business/ | grep -i session  | sed 's/^.*!\|^.*-\|;.*//g')
  echo "p2pre1c$i : $jvmid"
done

Explanation:
curl --head  and lynx -dump -head
will dump the headers which will have session id
For example:
# lynx -dump -head http://host20:7001/business/
HTTP/1.1 200 OK
Cache-Control: no-cache="set-cookie"
Connection: close
Date: Thu, 14 Oct 2010 00:20:18 GMT
Content-Type: text/html; charset=UTF-8
X-ATG-Version: version=QVRHUGxhdGZvcm0vOS4xIFsgRFBTTGljZW5zZS8wIEIyQkxpY2Vuc2Uv
MCAgXQ==
Set-Cookie: DSESSIONID=FnHsM2MCR3ZpJj!233685404; path=/
Set-Cookie: browserid=A001287015618072FnHsM2MCR3ZpJj!233685404!1287015618064; d
omain=.wireless.att.com; expires=Friday, 14-Oct-2011 00:20:18 GMT; path=/
Set-Cookie: cust_type=b2b; domain=.wireless.att.com; expires=Thursday, 21-Oct-2
010 00:20:18 GMT; path=/
X-Powered-By: Servlet/2.5 JSP/2.1

$ ./findhost.sh
host20 : 233685404
host21 : 2060847712
host22 : 447882153
host23 : 1405278242
host24 : 2099132956
...

If user sessionid is FnHsM2MCR3ZpJj!233685404
the instance serving the above user session is host20.

Thursday, October 7, 2010

Few commands to know about solaris box

Mostly Solaris is used as production server since it is more robust.

To find total hard disk space
$ iostat -E
sd1       Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: SEAGATE  Product: ST373207LSUN72G  Revision: 045A Serial No: 0543330VQB
Size: 73.40GB <73400057856 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 0 Predictive Failure Analysis: 0
sd2       Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: FUJITSU  Product: MAW3073NCSUN72G  Revision: 1703 Serial No: 0809B0R45J
Size: 73.40GB <73400057856 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 0 Predictive Failure Analysis: 0
sd3       Soft Errors: 1 Hard Errors: 0 Transport Errors: 0
Vendor: TOSHIBA  Product: ODD-DVD SD-C2732 Revision: 1055 Serial No: 
Size: 0.00GB <0 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 1 Predictive Failure Analysis: 0
sd4       Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: FUJITSU  Product: MAW3073NCSUN72G  Revision: 1703 Serial No: 0751B0PJ01
Size: 73.40GB <73400057856 bytes>

To find free hard disk space available
$ df -kh
Filesystem             size   used  avail capacity  Mounted on
/dev/md/dsk/d0         7.9G   3.1G   4.7G    40%    /
/devices                 0K     0K     0K     0%    /devices
ctfs                     0K     0K     0K     0%    /system/contract
proc                     0K     0K     0K     0%    /proc
mnttab                   0K     0K     0K     0%    /etc/mnttab
swap                   5.0G   1.3M   5.0G     1%    /etc/svc/volatile
objfs                    0K     0K     0K     0%    /system/object

To find how much disk space used
$ du -sh /
  34G  

To find how much disk space used by a directory
$ du -sh /tmp
 869M   /tmp

To find Total RAM
$ /usr/sbin/prtconf -v | head -2
System Configuration:  Sun Microsystems  sun4u
Memory size: 8192 Megabytes

$ /usr/sbin/prtdiag -v | head -3
System Configuration: Sun Microsystems  sun4u Sun Fire V440
System clock frequency: 177 MHZ
Memory size: 8GB       

To monitor RAM usage by each process
$ top
load averages:  0.03,  0.03,  0.02;                    up 41+21:17:50                                               13:46:14
73 processes: 72 sleeping, 1 on cpu
CPU states: 99.0% idle,  0.4% user,  0.6% kernel,  0.0% iowait,  0.0% swap
Memory: 8192M phys mem, 2906M free mem, 8193M total swap, 8193M free swap

   PID USERNAME LWP PRI NICE  SIZE   RES STATE    TIME    CPU COMMAND
   923 wlsadmin 166  59    0 3290M 1305M sleep   37:47  0.13% java
 28115 wlsadmin 197  59    0 3290M 1344M sleep   63:36  0.12% java
 28164 wlsadmin 142  59    0 3010M  926M sleep   31:51  0.12% java

To know all environment variables that are set
$ env
MANPATH=/usr/share/man:/usr/local/man:/usr/dt/share/man:/usr/openwin/share/man:/usr/local/share/man:/opt/VRTS/man
TERM=vt100
SHELL=/bin/bash
SSH_CLIENT=141.204.63.62 1539 22
SSH_TTY=/dev/pts/2
USER=m64711
MAIL=/usr/mail/m64711
PATH=/usr/bin:/usr/local/bin:/usr/openwin/bin
PWD=/home/m64711
EDITOR=vi
TZ=US/Central
PS1=${LOGNAME}@usppre16:${PWD}$
SHLVL=1
HOME=/home/m64711
LOGNAME=m64711
SSH_CONNECTION=141.204.63.62 1539 135.214.192.188 22
_=/usr/bin/env

To know if the solaris running in 32 or 64 bit mode
The isainfo utility can  answer are  whether  64-bit  applications are supported, or whether
the running kernel uses 32-bit or 64-bit device drivers.
$ isainfo -v
64-bit sparcv9 applications
        vis2 vis
32-bit sparc applications
        vis2 vis v8plus div32 mul32

if you get only 32-bit then it is running in 32 bit mode
if you get both 64-bit and 32-bit then it is running in 64 bit mode

The uname utility prints information about the current  system on the standard output.
$ uname -X
System = SunOS
Node = bsdevprem01
Release = 5.10
KernelID = Generic_127111-08
Machine = sun4u
BusType = <unknown>
Serial = <unknown>
Users = <unknown>
OEM# = 0
Origin# = 1
NumCPU = 2

To know the installed JVM is capable of running 64-bit version
$ java -d64 -version
java version "1.5.0_15"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_15-b04)
Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_15-b04, mixed mode)

if not 64 bit capable you will get some message like

This Java instance does not support a 64-bit JVM. Please install the desired version.

To know how long the serer is up
$ uptime
  5:15pm  up 78 day(s),  8:39,  3 users,  load average: 6.77, 6.75, 7.46

To know about active processes running
$ ps -ef | grep -i startweblogic
wlsadmin 28145 28140   0   Oct 01 ?           0:00 /bin/sh /sites/WLS/DOMAINS/PREMIERQA1-CASTAGING/bin/startWebLogic.sh
wlsadmin 28096 28091   0   Oct 01 ?           0:00 /bin/sh /sites/WLS/DOMAINS/PREMIERQA1-CAMAIN1/bin/startWebLogic.sh
wlsadmin 28140     1   0   Oct 01 ?           0:00 /bin/sh /sites/WLS/DOMAINS/PREMIERQA1-CASTAGING/startWebLogic.sh

-e list every process running now
-f list full information

For more info man ps

Using SVN and sqlplus

sqlplus example
devdb.sh
sqlplus username/password@'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=hostport)))(CONNECT_DATA=(SID=schemaname)))'

SVN Example
mysvn.sh
svn checkout "svn://hostname/B2B/branches/trunk/Module@HEAD" -r HEAD --depth infinity "~/workspace/BUILD" --username "xyz" --password "password"