Pages

Tuesday, January 5, 2016

Trick to keep your ssh session active


Home

For a person working on a Unix based OS over a ssh connection, SSH session timeout can be a pain area. If person is using putty for connection Unix OS over ssh then session can become inactive if kept idle for a longer time. So person needs to keep session alive by execution of some command or reconnect session manually.

To get rid of this inactivity of ongoing session irrespective of its idle or not, putty has an option of “Sending null packets to keep session active” by setting some desired value you can keep your session active, below is the screenshot of putty configuration option.




Back To Top
Home

Monday, January 4, 2016

Capture current directory within a shell script

Home

There are various scenarios where you need to ensure that all supporting components of a shell script are written in the same directory where script has been placed.

In order to achieve that easy way it to initialize a variable at start of script with value as parent folder of the shell script. But hard-coding of it can be a pain area in later on stages.

So to make it dynamic below syntax can be utilized, this has been tested and used in various implementations worth a try.

#!/bin/bash
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Enjoy !!


Back To Top
Home

Sunday, January 3, 2016

Installing RedHat 07

Home

Below sequence of images will get you basic idea of screens that you can expect during installation of RedHat 07

TODO

Back To Top
Home

Installing RedHat 06

Home

Below sequence of images will get you basic idea of screens that you can expect during installation of RedHat 06

TODO

Back To Top
Home

Installing RedHat 05

Home

Below sequence of images will get you basic idea of screens that you can expect during installation of RedHat 05

TODO

Back To Top
Home

wc command one liners

Home

Normal Use of wc command

# wc /etc/passwd
  23   30 1035 /etc/passwd

To count number of lines in a file

# wc -l /etc/passwd
23 /etc/passwd

To count number of words in a file

# wc -w /etc/passwd
30 /etc/passwd

To count number of characters in a file

# wc -c /etc/passwd
1035 /etc/passwd

To apply normal wc command on a output using PIPE operator

# cat /etc/passwd |wc
     23      30    1035

To count number of lines from an output using PIPE

# cat /etc/passwd |wc -l
23

To count number of words from an output using PIPE

# cat /etc/passwd |wc -w
30

To count number of characters form an output using PIPE

# cat /etc/passwd |wc -c

1035

Back To Top
Home

Recovering a deleted file in use by a process

lsof command one liners

Home

lsof command will provide a list of all open files belonging to respective active processes.

# lsof
COMMAND  PID       USER   FD      TYPE     DEVICE  SIZE/OFF       NODE NAME
init       1       root  cwd       DIR        8,1      4096          2 /
init       1       root  txt       REG        8,1    124704     917562 /sbin/init
init       1       root    0u      CHR        1,3       0t0       4369 /dev/null
init       1       root    1u      CHR        1,3       0t0       4369 /dev/null
init       1       root    2u      CHR        1,3       0t0       4369 /dev/null
init       1       root    3r     FIFO        0,8       0t0       6323 pipe
...

Time to see the details about output obtained,

COMMAND: - Name of process using a particular file
PID: - Process ID using a particular file
USER: - User name who has initiated the process/PID
DEVICE: - Details of device which is using the particular file
SIZE/OFF: - This is the size of the file or the file offset in bytes
NODE: - This is the node number of a local file
NAME: - This is the name of the mount point and file system on which the file resides

FD: - Represents the file descriptor.

Some of the values of FDs are,
CWD – Current Working Directory
TXT – Text file
MEM – Memory mapped file
MMAP – Memory mapped device
NUMBER[r,w,u] – Represent the actual file descriptor. The character after the number i.e ‘u’ or ‘r’ or ‘w’, represents the mode in which the file is opened. r for read, w for write, u for read and write.

TYPE: - Specifies the type of the file.

Some of the important values of TYPEs are as below reset can be referred from MAN  pages of lsof command,
REG – Regular File
DIR – Directory
FIFO – First In First Out
CHR – Character special file
IPv4 – An IPv4 socket file
IPv6 – An open IPv6 network file
sock – A socket of unknown domain
unix – A UNIX domain socket file

To get list processes which opened a specific file

# lsof /var/log/syslog

To get list opened files under a directory

# lsof +D /var/log/
+D will recurse the sub directories

# lsof +d /var/log/
To not to recurs use ‘+d’ option.

To list opened files based on process names starting with

# lsof -c ssh
# lsof -c ssh -c init

To list processes using a mount point

# lsof /home
# lsof +D /home/

To list files opened by a specific user

# lsof -u USERNAME

To list files opened by all users apart from a specific user

# lsof -u ^USERNAME

To list all open files by a specific process

# lsof -p 1753

To kill all process that belongs to a particular user

# kill -9 `lsof -t -u USERNAME`

To kill all process that belongs to a particular user

# kill -9 `lsof -t /var/log/syslog`

Combine more list options using OR/AND
When more than one list option in lsof are used they will be ORed

# lsof -u USERNAME -c FILENAME

To make it ANDed condition ass –a at end

# lsof -u USERNAME -c FILENAME -a

To execute lsof in repeat mode

# lsof -u USERNAME -c FILENAME -a -r5

List all network connections
Network connections are also files. So we can find information about them by using lsof. You can list all the network connections opened by using ‘-i’ option.

# lsof -i

To list IPv4 connections

# lsof –i4

To list IPv6 connections

# lsof –i6

To list all network files in use by a specific process

# lsof -i -a -p 234
# lsof -i -a -c ssh

To list processes which are listening on a particular port

# lsof -i :25

To list all TCP or UDP connections

# lsof -i tcp;
# lsof -i udp;

To list connections against a particular TCP or UDP port

# lsof –I TCP:22
# lsof –I UDP:123

To list all Network File System ( NFS ) files

# lsof -N -u USERNAME –a

To list open files of TCP Port ranges 1-1024


# lsof -i TCP:1-1024


References :-

http://www.innovationsts.com/blog/?p=658 

Back To Top
Home

Saturday, January 2, 2016

Regular Rxpressions One Liners

Home

To make a copy of file

# cp /path/to/file /path/to/file_move
# cp /path/to/file{,_copy}

To move or rename a file

# mv /path/to/file_name /path/to/file_name_old
# mv /path/to/file_name{,_old}

To generate Small letter a to z (with spaces between characters)

# echo {a..z}

To generate Capital letter A to Z (with spaces between characters)

# echo {A..Z}

To generate Small letter a to z (without spaces between characters)

# printf "%c" {a..z}

To generate Capital letter A to Z (without spaces between characters)

# printf "%c" {a..Z}
Above with result in output is without a terminating newline because the format string was "%c" and it doesn't include \n.

To have it newline terminated, just add $'\n' to the list of chars to print:

# printf "%c" {a..z} $'\n'
# echo $(printf "%c" {a..z})

To generate all letters in a column output and store it in a variable

# printf "%c\n" {a..z}
# variable=$( printf "%c" {a..z})
# variable=`printf "%c" {a..z}`
# printf –v variable "%c" {a..z}

To generate numbers from 1 to 100 (with spaces between numbers)

# echo {1..100}

To generate numbers from 1 to 100 (in column format)

# seq 1 100

To generate numbers 0 to 9 with a leading zero as pad

# printf "%02d " {0..9}
# echo {00..09}

To generate n number of words

# echo {w,t,}h{e{n{,ce{,forth}},re{,in,fore,with{,al}}},ither,at}

To generate alphanumeric strings

# echo {a,b,c}{1,2,3}

To generate 5 copies of the same string

# echo potato{,,,,}


To search lines starting with a string

grep '^string' files

To search lines ending with a string

grep 'smug$' files

To search lines containing only the string

grep '^smug$' files

To search lines starting with a special character ^ which needs to be escaped

grep '\^s' files

To search lines containing string with capital and small letter combination

grep '[Ss]mug' files

grep 'B[oO][bB]' files

To search empty lines

grep '^$' files

To search lines containing pair of digits

grep '[0-9][0-9]' file

To search lines containing at least one letter

grep '[a-zA-Z]'

To search lines containing letter or digit

grep '[^a-zA-Z0-9]

To search lines similar to 999-9999, like phone numbers

grep '[0-9]\{3\}-[0-9]\{4\}’

To search lines with exactly one character

grep '^.$'

To search lines with string within quotes (Single/Double)

grep '"string"'        {'string' within double quotes}
grep '"*string"*'        {'string', with or without quotes}

To search lines starting with special character “.”

grep '^\.'     {any line that starts with a Period "."}



Back To Top
Home

echo command one linsers

Home

Input a line of text and display on standard output

# echo Test string for command exercises
Test string for command exercises

Print value of a variable

# var1=10
# echo The value of variable var1 = $var1
The value of variable x = 10

Note: The ‘-e’ option in Linux acts as interpretation of escaped characters that are backslashed.

‘\b’– Backspace which removes all the spaces in between.
With backslash interpreter (-e)

# echo -e "Test string \bfor command \bexercises "
Test stringfor commandexercises

Without backslash interpreter (-e)

# echo "Test string \bfor command \bexercises "
Test string \bfor command \bexercises

‘\n’ – New line to add new line from where it is used.
With backslash interpreter (-e)

# echo -e "Test string \nfor command \nexercises "
Test string
for command
exercises

Without backslash interpreter (-e)

# echo "Test string \nfor command \nexercises "
Test string \nfor command \nexercises

‘\t’ – Horizontal tab to have horizontal tab spaces.

With backslash interpreter (-e)

# echo -e "Test string \tfor command \texercises "
Test string     for command     exercises

Without backslash interpreter (-e)

# echo "Test string \tfor command \texercises "
Test string \tfor command \texercises

Using new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously.
With backslash interpreter (-e)

# echo -e "Test string \n\tfor command \n\texercises "
Test string
        for command
        exercises

Without backslash interpreter (-e)

# echo "Test string \n\tfor command \n\texercises "
Test string \n\tfor command \n\texercises

With backslash interpreter (-e) Sequence matters

# echo -e "Test string \t\nfor command \t\nexercises "
Test string
for command
exercises

With backslash interpreter (-e)

# echo  "Test string \t\nfor command \t\nexercises "
Test string \t\nfor command \t\nexercises

‘\v’ – Vertical tab to have vertical tab spaces.
With backslash interpreter (-e)

# echo -e "Test string \vfor command \vexercises "
Test string
            for command
                        exercises

Without backslash interpreter (-e)

# echo "Test string \vfor command \vexercises "
Test string \vfor command \vexercises

Using new Line ‘\n’ and vertical tab ‘\v’ simultaneously.
With backslash interpreter (-e)

# echo -e "Test string \n\vfor command \n\vexercises "
Test string

for command

exercises

Without backslash interpreter (-e)

# echo "Test string \n\vfor command \n\vexercises "
Test string \n\vfor command \n\vexercises

With backslash interpreter (-e)

# echo -e "Test string \v\nfor command \v\nexercises "
Test string

for command

exercises

Without backslash interpreter (-e)

# echo "Test string \v\nfor command \v\nexercises "
Test string \v\nfor command \v\nexercises

Note: no difference observed in combinational use of new line and vertical tab option, multiple time usage can get you different results.

‘\r’ – carriage return to have specified carriage return in output.
With backslash interpreter (-e)

# echo -e "Test string \rfor command exercises "
for command exercises

Without backslash interpreter (-e)

# echo "Test string \rfor command exercises "
Test string \rfor command exercises


‘\c’ – suppress trailing new line to continue without emitting new line.
Without backslash interpreter (-e)

# echo -e "Test string \cfor command exercises "
Test string [test ~]#

Without backslash interpreter (-e)

# echo "Test string \cfor command exercises "
Test string \cfor command exercises

Omit printing trailing new line using option ‘-n‘.

# echo -n "Test string for command exercises "
Test string for command exercises [test ~]#

‘\a’ – alert return to have sound alert.
With backslash interpreter (-e)

# echo -e "Test string \afor command exercises "
Test string for command exercises
Note: Make sure to check Volume key, before firing.

Without backslash interpreter (-e)

# echo "Test string \afor command exercises "
Test string \afor command exercises

Print all the files/folder using echo command (simulate ls command).

# echo *

Print files of a specific kind.

# echo *.txt

To append a text to a file instead of sending it to standard output

# echo "Test content" > testfile
# cat testfile
Test content

Prints " (quote, octal ASCII character 42)

echo "QUOTATION MARK"
echo -e "\042"       # Prints " (quote, octal ASCII character 42).
echo "=============="

Quote (") framed by tabs.

echo $'\t \042 \t'  # Using ASCII Values
echo $'\t \x22 \t'  # Using Hexadecimal Values

Assigning ASCII characters to a variable.

quote=$'\042'        # " assigned to a variable.
echo "$quote Quoted string $quote and this lies outside the quotes."

Concatenating ASCII chars in a variable.

triple_underline=$'\137\137\137'  # 137 is octal ASCII code for '_'.
echo "$triple_underline UNDERLINE $triple_underline"

Assigning Octal character values to a variable.

ABC=$'\101\102\103\010'           # 101, 102, 103 are octal A, B, C.
echo $ABC

Reference:-
http://www.asciitable.com/





Back To Top
Home