Thursday, September 16, 2010

Providing Sudo access to commands with arguments

Situation:
You need to grant sudo access to a command that takes some parameters.

For example the following command takes “username” as the parameter.

#nxserver --useradd  <username>

If we just give “nxserver --useradd” in the /etc/sudoers file, it wouldn’t work.


Solution:
Use wild characters in /etc/sudoers file as shown in the following:

%itops ALL=(ALL)  nxserver --useradd  [A-Za-z]*      (or)

%itops ALL=(ALL)   nxserver --useradd ?*          # Where %itops is the groupname


Converting Bash script to Binary code

Bash Shell basically comprises of commands written in C language. Hence we basically need a Compiler that can convert the C code into executable Binary file.  I guess there are many C compilers available;  the one which I use is SHC (SHell script Compiler).  When we compile using this tool, it creates an execute file with .x extension, which is basically a executable version of your Bash script which can’t be readable or modifiable.

Illustration:

Extract the shc tar file.
Go into the shc-3.8.7 folder
Install the package by executing 'make'. It will create the 'shc' executable.

Create a sample bash script "sample.sh".

[root@host01 opt]$ cat sample.sh
#!/bin/bash

echo "Hello how r u"
echo
[root@host01 opt]$
[root@host01 opt]# ./sample.sh                              #  Take a note on output
Hello how r u

[root@host01 opt]#
[root@host01 shc-3.8.7]$ ./shc -f /opt/sample.sh
[root@host01 opt]# ls -l sample.sh*
-rwxr-xr-x 1 root root    39 Sep 13 12:11 sample.sh
-rwx--x--x 1 root root 11720 Sep 13 12:12 sample.sh.x     #  .x file created
-rw-r--r-- 1 root root  9406 Sep 13 12:12 sample.sh.x.c
[root@host01 opt]#
[root@host01 opt]# ./sample.sh.x                                #  Same result as before
Hello how r u

[root@host01 opt]# tail sample.sh.x                            #  File is in Binary format
@
@
 @
  @.
    @>
      @N
        @OÞ}¬÷ú=hòL³})âïM 3ði¹_=ÄÊÚî¦kpsJ=¸­¡`h]L1ôÌ7©+Éj3
                                                          ÌÜ.*Å=ßBl`H>û²ÞÎþýºD`44vÉáÛÑJ-h2»ñûMBpñ<D#ë0óÃH(rAÊß~jÀ

Searching files with particular string in Linux

Let say you want to search all "*.conf" files under  “/home/jboss”  having string “cache” in it and display the output.

# find /home/jboss -name *.conf -type f | xargs grep “cache”
(or)
# find /home/jboss -name *.conf -type f -exec grep -HI "cache" {} \;

Now you have a slightly different requirement: 
You want to search all "*.conf" files under  “/home/jboss”  having string “cache” and replace it with "core".
# perl -p -i -e 's/cache/core/g'  `find /home/jboss -type f -name *.conf`
Another version:
#  find /home/jboss -type f -name *.conf | xargs grep -l cache | xargs perl -p -i -e 's/cache/core/g'

More about Linux Man page

Q1) How to know the options of ‘man’ command?   
# man man    (or)  # info man

Q2) How can we search for man pages?
# man  -k  <command / string>

Q3) How can we convert the Man pages into file so that we can take a printout or save it for future reference?   
# man [command name] | col -b > filename
E.g: # man vim | col -b > vim_manpage
Note:  Using 'col -b' will filter reverse line feeds.


Q4) When we get into the folder /usr/share/man, we would see folders such as man1, man2, man3, man4 ......man9. what are all those?  
At times you would have noticed something like "refer man5 page" etc. What it refers to ? 

Explanation:
Some commands have multiple man pages. For instance, the ‘passwd’ command has a man page in section 1 and another in section 5. By default, the man page with the lowest number is shown.
If you want to see another section than the default, specify it after the man command:

# man 5 passwd

If you want to see all man pages about a command, one after the other, use the -a to man:
# man -a passwd
This way, when you reach the end of the first man page and press SPACE again, the man page from the next section will be displayed.

Sorting TOP output in Linux

You can sort the “Top” output on various criteria such as CPU Usage, Memory Usage, Swap, CPU time, User etc. 
To do this, while running “Top”, press ‘O’ (capital) key to access the sorting options.  From there you can choose sorting criteria (for example, press ‘l’ for CPU time – shown below)
and press ‘Enter’ to apply it.





Comparing two files in Unix

You got 2 files which contains a list of servernames (as shown below). You want to compare the entries between two files and get the list of server names which aren’t present in both files:

1st file:
server_a
server_b
server_c
server_d
server_e

2nd file:
server_c
server_d
server_g
server_a

Output should be:

server_b
server_g
server_e



Solution 1:  
# sort file1 file2 | uniq –u

Solution 2:
# sort file1 > file1.sorted
# sort file2 > file2.sorted
# comm -3 file1.sorted file2.sorted