Monday, February 24, 2014

Some example of if statement - Shell scripting example

#!usr/bin/ksh
num1=20
num2=30
if [[ $num2 -gt $num1 ]]; then
    echo "The second value ${num2} is greater than the first value ${num1} ."
    exit 1
else
    echo "The sum of these number is `expr $num1 + $num2`"
    echo "Cool, enjoy !!!"
fi

-----------------------------------------------------------

if [ -d /opt/share ]
then
    /usr/bin/rmdir /opt/share 2>/dev/null
        if [ $? != "0" ]
        then
            /usr/bin/rm -r /opt/share 2>/dev/null
        fi
    /usr/bin/rm -rf /opt/share 2>/dev/null
fi
/usr/bin/mkdir -p /opt/share
/usr/bin/cp ~jay/notes/* /opt/share

-----------------------------------------------------------

if [ `whoami` != "root" ]
then
    exit 1
fi

-----------------------------------------------------------

Automate FS creation task for LVM and VxVM

bash-3.2$ cat >my_LVM
# VG    FS      Size
vg01    FS_WWW  20G
vg01    FS_WWW2 30G
vg02    FS_WWW  20G
vg02    FS_WWW2 30G
---------------------------------------------
bash-3.2$ more mylvm.sh
#!/bin/sh
# Sam
# Automate FS creation task
# Mon Feb 24 12:30:18 EST 2010
#
cat my_LVM | grep -v "^#" | while read myvg myfs mysize
do
#
echo " "
echo For LVM
        echo lvcreate -L ${mysize} -n ${myfs} ${myvg}
        echo mkfs /dev/${myvg}/${myfs}
#
echo " "
echo For Veritas Volume Manager
        echo vxassist -g ${myvg} -b make ${myfs} ${mysize} layout=nostripe
        echo " "
#
echo For vxfs
#
        echo /usr/lib/fs/vxfs/mkfs -F vxfs -o largefiles /dev/vx/rdsk/${myvg}/${myfs}
        echo " "
#
echo For UFS
#
        echo newfs -v /dev/vx/rdsk/${myvg}/${myfs}
        echo fsck -y /dev/vx/rdsk/${myvg}/${myfs}
        echo "-----------------------------------"
#       echo " "
done
#
echo
echo "Task completed !!!"
echo
bash-3.2$

---------------------------------------------
bash-3.2$ sh mylvm.sh
For LVM
lvcreate -L 20G -n FS_WWW vg01
mkfs /dev/vg01/FS_WWW
For Veritas Volume Manager
vxassist -g vg01 -b make FS_WWW 20G layout=nostripe
For vxfs
/usr/lib/fs/vxfs/mkfs -F vxfs -o largefiles /dev/vx/rdsk/vg01/FS_WWW
For UFS
newfs -v /dev/vx/rdsk/vg01/FS_WWW
fsck -y /dev/vx/rdsk/vg01/FS_WWW
-----------------------------------
For LVM
lvcreate -L 30G -n FS_WWW2 vg01
mkfs /dev/vg01/FS_WWW2
For Veritas Volume Manager
vxassist -g vg01 -b make FS_WWW2 30G layout=nostripe
For vxfs
/usr/lib/fs/vxfs/mkfs -F vxfs -o largefiles /dev/vx/rdsk/vg01/FS_WWW2
For UFS
newfs -v /dev/vx/rdsk/vg01/FS_WWW2
fsck -y /dev/vx/rdsk/vg01/FS_WWW2
-----------------------------------
For LVM
lvcreate -L 20G -n FS_WWW vg02
mkfs /dev/vg02/FS_WWW
For Veritas Volume Manager
vxassist -g vg02 -b make FS_WWW 20G layout=nostripe
For vxfs
/usr/lib/fs/vxfs/mkfs -F vxfs -o largefiles /dev/vx/rdsk/vg02/FS_WWW
For UFS
newfs -v /dev/vx/rdsk/vg02/FS_WWW
fsck -y /dev/vx/rdsk/vg02/FS_WWW
-----------------------------------
For LVM
lvcreate -L 30G -n FS_WWW2 vg02
mkfs /dev/vg02/FS_WWW2
For Veritas Volume Manager
vxassist -g vg02 -b make FS_WWW2 30G layout=nostripe
For vxfs
/usr/lib/fs/vxfs/mkfs -F vxfs -o largefiles /dev/vx/rdsk/vg02/FS_WWW2
For UFS
newfs -v /dev/vx/rdsk/vg02/FS_WWW2
fsck -y /dev/vx/rdsk/vg02/FS_WWW2
-----------------------------------
Task completed !!!
bash-3.2$

==============================================================

# cat lvm_create.sh
#!/bin/sh
# SAM Bhusal
# Automate FS creation task. This script tested and verified on Redhat Enterprise 6.x version.
# Config file entry eg.
# # file_name: LVM_WEBLOGIC
# # For Weblogic
# # vg    FS      Size(GB) Mnt point
# --------------------------------
# datavg  WEB     10     /web
# datavg  WEBDATA 15     /webdata
#
# Mon Feb 24 12:13:24 EST 2014
# Update: Thu Aug 14 09:55:12 EDT 2014
# Added the entry for mount point on config file.
# Added entry to the fstab.
#
if [ `/usr/bin/whoami` != "root" ]
then
        echo "You must be root to run this script"
        exit 1
fi
# Creating LVM.
/bin/cat LVM_WEBLOGIC | grep -v "^#" | while read myvg myfs mysize mymt
do
        echo " "
        # Check if mount point already exists.
        #/bin/df -h ${mymt}
        /bin/ls -ld ${mymt}
        if [ $? -eq 0 ]; then
                echo "Mount Point exists."
                echo "Please review the config file."
                exit 1
        fi
        /sbin/lvcreate -L ${mysize}G -n ${myfs} ${myvg}
        /sbin/mkfs.ext4 /dev/${myvg}/${myfs}
        # Create mount point
        /bin/mkdir -p ${mymt}
        # Add Entry to fstab
        echo "###########################################################################" >>/etc/fstab
        echo "/dev/${myvg}/${myfs}      ${mymt} ext4    defaults        1 2" >>/etc/fstab
        # Mount the filesystem.
        /bin/mount -a
        # Verify if the filesystems were created.
        /bin/df -h      ${mymt}
        # Check the condition if the process is successful.
        if [ $? -eq 0 ]; then
                echo "Successfully Created fileystem."
                continue
        else
                echo "Failed, please review the error"
                # exit
        fi
echo " ----------------end----------------"
done
# EOF

# cat LVM_WEBLOGIC
# file_name: LVM_WEBLOGIC
# SAM Bhusal
# For Weblogic
# vg    FS       Size(GB)  mPoint
#---------------------------------------
datavg  PKGS            20      /pkgs
datavg  WWW             10      /www
datavg  WWW_DOCROOT     10      /www/docroot
datavg  WWW_SERVERS     10      /www/servers
datavg  FMAC_TEST       10      /fmac/test
datavg  FMACDATA        10      /fmacdata
 

Saturday, February 15, 2014

Shell Script example

Example of shel script

Eg of C code

[devi@ram ~]$ cat mail.c
#include <stdio.h>

int main() {
      printf ("Hello World\n");
      return (0);
}
[devi@ram ~]$ gcc mail.c
[devi@ram ~]$ ./a.out
Hello World

eg of shell script
[devi@ram ~]$ cat greet.sh
#!/bin/sh
# Author:
# It displays the "Hello World"
#
echo "Hello World !!!"

[devi@ram ~]$ sh greet.sh
Hello World !!!


Configure SUDO

Enhanced User Security

- Sudo user
- Normal user cannot run every command
- It is used for giving or allowing special permission to the normal user
- use " whereis" command to know the command exact path
- When executing, use sudo + full path of the command
- Configuration file is /etc/sudoers
- When editing use visudo which checks the syntax.
------------------------------------------------------


Permission to users
# visudo
  root ALL = (ALL)
  jay  ALL = /usr/sbin/useradd  OR    // only allowing useradd command permission
  jay ALL = ALL                // Giving full permission
  :wq

Now, change to the user jay
# su - jay
$ useradd tom                // Error, Permission Denied
$ sudo /usr/sbin/useradd tom
---------------------------------------------

Permission to group
# groupadd sales
# usermod -G sales jay
# usermod -G sales dev
#visudo
  %sales    ALL=/usr/sbin/useradd
  :wq

Now, change user to dev

# su - dev
$ sudo /usr/sbin/useradd ram
---------------------------------------

Aliases

There are two (or three) types of Alias
- User alias
- Command Alias
{- Host Alias }
------------------------------------------------------

# visudo
  ## Users aliases
  User_Alias  TESTER = jay, dev        // TESTER is just a name to represent users.
  ## You can create different user alias and add different user to give different permission.
  ## Command aliases
  Cmmd_Alias CMDS = /sbin/service,/sbin/fdisk      // CMDS=just an name, anyname of command alias

  root ALL = (ALL)
  TESTER ALL = CMDS
  :wq

Now, switch to user and test the access.

======================================

visodo and add the following info on the file.
# visudo

Host_Alias     DBSERVERS = bardiya, nepal, udaya
User_Alias ADMINS = surya, jay, kamal
Cmnd_Alias ADMINCMDS = /usr/sbin/useradd, /usr/bin/passwd,/sbin/route
ADMINS DBSERVERS=ADMINCMDS

wq!

login as: surya
surya@192.168.10.32's password:
Last login: Fri Mar 15 00:13:01 2013 from 192.168.10.11
Red Hat Enterprise Linux Server release 6.1 (Santiago)
Kernel \r on an \m
If you mess with this system, you will be on big trouble.....

[surya@bardiya ~]$ sudo useradd pratic
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
[sudo] password for surya:

[surya@bardiya ~]$ id pratic
uid=2021(pratic) gid=2021(pratic) groups=2021(pratic)

[surya@bardiya ~]$ sudo passwd pratic
Changing password for user pratic.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[surya@bardiya ~]$

Create LVM - a simple method...

Still need to fine tune...

[root@ram ~]# cat >vols
apps    1G
webs    1G

[root@ram ~]# cat vols | while read fs size
> do
> echo Filesystem is $fs
> echo Size is $size
> done
Filesystem is apps
Size is 1G
Filesystem is webs
Size is 1G


[root@ram ~]# cat vols | while read fs size
> do
>  lvcreate -L ${size} -n ${fs} test_vol
> mkfs.ext4 /dev/test_vol/${fs}
> mkdir /myFS
> mount /dev/test_vol/${fs} /myFS
> done
  Logical volume "apps" created
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
  Logical volume "webs" created
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
mkdir: cannot create directory `/myFS': File exists
[root@ram ~]# df -h /myFS
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/test_vol-webs
                     1008M   34M  924M   4% /myFS
[root@ram ~]#