Archive for the ‘ Shell Scripting ’ Category

PAM Login Notification Centos

Edit the file :  /etc/pam.d/sshd

vi /etc/pam.d/sshd

Add this line to the above file :

session optional     /path/to/

Now Create the Script that will send the notification :

vi /path/to/script

Paste The Following content, and adjust the admin email address :

[ "$PAM_TYPE" == "open_session" ] || exit 0
        echo "User: $PAM_USER"
        echo "Remote Host: $PAM_RHOST"
        echo "Service: $PAM_SERVICE"
        echo "TTY: $PAM_TTY"
        echo "Date: `date`"
        echo "Server: `hostname -s`"
        } | mail -s "`hostname -s` $PAM_SERVICE login: $PAM_USER" $adminEmail

Reference first argument in move command

mv /path/to/dir/filename.txt !#^:h/newname.txt

Where :

! means history expansion

# means command currently being typed

^ means the first argument

:h gets the “head” that is the directory without the filename portion

Reading an Input From File

Reading an input from a file:

Command <file>

#trĀ  ‘a-z’ ‘A-Z <file (Translates all the lowercase characters to uppercase).

Note: if you want to print the output of existing file to new file

Use this command

#tr `a-z’ `A-Z'<oldfilename>newfilename

Time Saving CLI Techniques

$ echo {x,y,z}
x y z

$ echo a{x,y,z}
ax ay az 

$ echo {{1,2,3},1,2,3}
1 2 3 1 2 3

$ echo {{1,2,3}1,2,3}
11 21 31 2 3

$ cp /etc/samba/smb.conf{,.bak}

>CTRL + R to search through the history .

Simple yes no input shell script

Simple shell script to get user confirmation

echo “Do You wish to Continue ?<y/n>: ”
read response
if [ $response = “y” ];then
echo “You entered yes”
echo “Please Wait..Window Will Close When Done”
sleep 10

echo “Done…Exiting Now”
sleep 2



Disable Traps ctrl c , ctrl z in shell script

Use the following line in the beginning of your shell script to disable traps like ctrl c and ctrl z in your shell script.

trap “” 2 20

Protect Shell scripts