Friday, 19 February 2016

Copying time from test output

Recently I was given the task of running SQL queries from a shell script. This script used to run overnight and next day I
'd to check how much time each of the queries took and fill them up in an excel sheet. There were around 200 SQL queries which means around 200 timings. Now, the format of the output, the output were stored in a file, was somewhat like this:

========= Test Queries ========
std_q01          ... MATCH - (13.71 s)
std_q02          ... MATCH - (41.38 s)
std_q03          ... MATCH - (45.27 s)
.....

copying each timing was a tedious task and was boring too, so I thought of doing it in a different way with the help of grep,
 awk and sed. How?

This is how I've done it:

grep std_ /path/of/output/file | awk ' { begin; print $5 } ' | sed 's/^(//'

Output:
13.71
41.38
45.27

From the above output the only task left for me to do is copy-paste to the excel sheet.
Excel sheet is smart enough to split them into separate cells. That's all.

Monday, 6 July 2015

Delete files older than a particular period

Files that are older than a particular day can be deleted easily with a one line command. For example, the following command will delete files older than 30 days in the path /tftpboot/:

#fine /tfptboot/* -mtime +30 -exec rm {} \;

Thursday, 25 June 2015

Disable the root SSH login

For security reasons, we might need to disable the 'root' user SSH login for the Linux system. This can be easily done by editing the sshd_config file as the 'root' user.

    First, open up the file in any text editor:

    vi /etc/ssh/sshd_config

 
   Next, find the following line:

    #PermitRootLogin no

    The line is currently commented. Uncomment it, and that's all you need to do. Save the file, and restart the ssh service.

    /etc/init.d/sshd restart

Monday, 23 February 2015

Execute commands on a remote Linux machine

We know that ssh is used to remote login to a server. But did you know that we can use ssh to execute command or script on a remote system. This is the syntax how we can do it:

ssh [USER]@[IP] [command or script]


Let us look at how this can be done. Suppose you want to display the directory contents of /root of a remote host, you can run the following command:

ssh root@192.168.1.79 ls -l

N.B. - It will ask for the password.

In the same way you can execute any script on the remote machine.


Monday, 2 February 2015

Securing files

    Here is a simple tip to password protect your files:

               vi -x test.txt

    This command will ask for an encryption key. You have to type the key twice. Then save and quit the opened file.
     Now, whenever you open this file, it will ask for that password first. Remember, this will only encrypt the content of the file. Anybody, can enter a wrong password and update the file.

Monday, 26 January 2015

Disabling the password prompt for sudo users


Often, GNU/Linux users come across the sudo utility. But on executing any command with sudo requests, users have to provide their own passwords once, which can then be used afterwards.

$ sudo vim /etc/sudoers
[sudo] password for tom:

To disable this sudo password prompt, edit the /etc/sudoers file as follows:

sudo vim /etc/sudoers

and change...

%sudo ALL=(ALL:ALL) ALL

...to

%sudo ALL=(ALL:ALL) NOPASSWD: ALL

Save the file and check for the changes. You are now free to execute anything without entering passwords.

Tuesday, 6 January 2015

Command line shortcuts

Here goes some tips to speed up our work.

cmd1;cmd2

The above command will run cmd1 and then execute cmd2.

cmd1 && cmd2

This will execute cmd2 if cmd1 is successful.

cmd1 || cmd2

The above sequence will run cmd2 if cmd1 is not successful.

Ctrl+a

This will move the control to the beginning of the line in CLI.

Ctr+e

The above command will move the control to the end of the line in CLI.

!ser :p

The above command will print the last command starting with 'ser'.