awk print Nth line after matching a pattern


By using awk we can print only Nth line after matching a pattern.

Lets say I have a file test.txt with following content and I want to extract 5 line after matching pattern.

$ cat test.txt


Use following awk code to extract Nth line:


$ awk 'c&&!--c;/pattern/{c=N}' file

where “pattern” is your input pattern and N is a line number to extract after matching pattern.

$ awk 'c&&!--c;/1/{c=5}' test.txt



In above example I am trying to extract 5th line after matching pattern, here my input pattern is 1 and 5th line after matching pattern is 6. So our output is 6.

bash – get previous command status


To get previous command status in bash use “$?” variable.

echo $? will return zero if previous command is success & if its non zero it indicates failure.


$ ls ; echo $?

Output of the above command is list of files, directories and next line it will return number.

We can use condition for check if previous command is success or failure:

if [ $? -eq 0 ] 
    echo SUCCESS
    echo FAIL

Ansible – Create user with password in Ubuntu/Linux

Ansible is an open-source software platform for configuring and managing computers.

In this article I will explain how to create new user with password.

Ansible has a user module to create a user. You can refer user module docs for more details.

I would like to create a user with name “guest” and password “guest123”.

Ansible will take only encrypted password as input password i.e, if we give passowrd as “guest123” it will decrypt the given password and set, obviosely if we decrypt “guest123” it wont be same as “guest123”.

After deciding username and password generate encrypted password:

python -c ‘import crypt; print crypt.crypt(“guest123”, “guest”)’



In above example I using guest as key to encrypt the password.

After creating password add following code in ansible-playbook:

- hosts: yourhostname
  user: root
  sudo: no

  #Define user and password variables
    # created with:
    # python -c 'python -c 'import crypt; print crypt.crypt("guest123", "guest")'
    password : gu2KmqcJp0Yyo
    user : guest

  # Define task to add user
    - name: add user
      action: user name={{ user }} password={{ password }} update_password=always 
                   shell=/bin/bash home=/home/{{ user }}
        - user

After updating code run above playbook and it will create new user with given password.

Print only response headers with curl

To print only response headers with curl use following command:

$ curl -s -D – -o /dev/null


HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Content-Length: 261
Date: Wed, 05 Nov 2014 05:06:40 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic,p=0.01

Nagios – Check Ping /bin/ping Unknown status problem

Recently while I am trying to setup Nagios on Ubuntu 14.04 OS I got the error saying /bin/ping Unknown status.

After debugging a wile I got to know that, this issue is because of permissions for /bin/ping script.

To resolve this issue just run following command ( use root or sudo):

$ chmod u+s /bin/ping

After running above command /bin/ping script permissions look like below:

$ ls -l /bin/ping

-rwsr-xr-x 1 root root 44168 Mar 15  2014 /bin/ping*

After a while, nagios should be able to ping your servers without any issue.

How to clear swap space – Ubuntu/Linux


To clear swap space in Ubuntu/Linux we need to use swapoff command as shown below:

$ swapoff -a

Above command will turn of swap completely.

Before running swapoff command you need to have enough free RAM available, since swapoff command will move all swap data to RAM.

If you wont have enough free RAM this command may make your system unstable.

Again to turn on swap you need to use swapon command ash shown below:

$ swapon -a

You need to have root/sudo permissions to run above commands.

Number of open files or file descriptors associated with a process

Since in *nix every thing is a file, When you try to find open files or file descriptor associated with a process you will see Actual file log, Unix Sockets, Library files, Network Sockets,  /dev files and others.

lsof is a command to list all open files.

To list all open files or file descriptors by a process:

$ lsof -p PID


$ lsof -a -p PID


$ ls /proc/PID/fd

where PID is a process ID of any process that is running.

To count all open files by a process:

$ lsof -p PID | wc -l


$ ls /proc/PID/fd | wc -l

To list all open files:

$ lsof 

To count all open files:

$ lsof | wc -l

Nmap – Scan for open ports


nmap is a network exploration tool and security/port scanner.

We can list all open ports using nmap for a given IP/Host.

Use following command to list all open ports:

$ sudo nmap -v -sS

Use following command to scan specific ports:

$ nmap -sS -O -p80,8080

To can specific range of ports(scan from port 80 to 100):

$ sudo nmap -sS -O -p80-100

Python convert string to list/tuple/dictionary

By using ast modules literal_eval function we can convert any python string to valid literal.

ast modules literal_eval function will only evaluates string that are valid python literals.

Lets try with an example:

I have file with some valid python literals and I want to evaluate each of them.

Content of my input file is following:

[1, 2, 3]
(1, 2, 3)
{‘a’:1, ‘b’:2, ‘c’:3}

Here I want to detect type of literal in each line.

First I will try with a simple example:

import os, sys
f = open(sys.argv[1], 'r')
lines = f.readlines()
print lines
for i in lines:
    print i.strip(), type(i)


[1, 2, 3] <type ‘str’>
(1, 2, 3) <type ‘str’>
{‘a’:1, ‘b’:2, ‘c’:3} <type ‘str’>
‘abc’ <type ‘str’>
123 <type ‘str’>

Just observer output, by default all lines are detected as string.

Now try with ast modules literal_eval function.

import os, sys, ast
f = open(sys.argv[1], 'r')
lines = f.readlines()
#print lines
for i in lines:
    print i, type(ast.literal_eval(i))


[1, 2, 3] <type ‘list’>
(1, 2, 3) <type ‘tuple’>
{‘a’:1, ‘b’:2, ‘c’:3} <type ‘dict’>
‘abc’ <type ‘str’>
123 <type ‘int’>

Now observer above output, each line is detected and converted as its own literal type.

I tested this example in Python 2.7.3