Showing posts with label ubuntu. Show all posts
Showing posts with label ubuntu. Show all posts

Wednesday, 8 June 2016

How to generate GPG Key?

Prerequisites

packages to be installed

  1. rngd
  2. gpgv OR gpgv2
Step1: Create a Temporary Folder and CD to it
Step2: Generate GPG Key
  1. eval `gpg-agent --daemon --no-grab --write-env-file $HOME/.gpg-agent-info`
  2. export GPG_TTY=`tty`
  3. export GPG_AGENT_INFO
  4. gpg --gen-key
    1. Enter passphrase: ******
  5. Run in a Different terminal rngd -f -r /dev/urandom
Step3: read the key' s.

 gpg -k

Sample Output:

GPG Key o/p:
.......+++++
+++++
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.+++++
.................+++++
gpg: key 50F6E79D marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/50F6E79D 2015-08-14
Key fingerprint = 3C2C 81AA 5B9E 6772 1AEA  9AF3 78B2 D37D 50F6 E79D
uid ZeroOnePrograms  <developer@zerooneprograms.com>
sub   2048R/EA01868C 2015-08-14

Sunday, 5 June 2016

How to check prerequisite in linux bash script?

Check the user is root or not?

if [ "$(whoami)" != 'root' ]; then
        echo "You have no permission to run $0 as non-root user."
        exit 1
fi

Check the OS?

OS=`cat /etc/issue |head -n 1 |awk '{ print $1 }'`
if [ $OS != "Ubuntu" ]; then
          echo "OS Architecture Not Supported...!"
       exit 1
fi

Check the VCPU' s?

vcpu=$(lscpu | grep "^CPU(s):.*$"|awk '{print $2}')
if [ $vcpu -lt 2 ];then
        echo "Minimum 2 vcpu's are Required...!"
        exit 1
fi


Check the Total Memory/RAM?

TotalMemory=$(grep MemTotal /proc/meminfo | awk '{print $2}')

if [ $TotalMemory -lt 8113442 ];then
        echo "Insuffiecient Memory 'Total RAM Should be 8G'"
        exit 1
fi

<<'COMMENT'
Total Memory        -  8G/100%  -   8388608KB
Operational Memory  -  .30%     -   25165.824KB
Remaining           -  99.7%    -   8363442.176
Required            -  6G/100%  -   6291456KB
COMMENT

Check the Available Memory/RAM?

FreeMemory=$(grep MemFree: /proc/meminfo |awk '{print $2}')
if [ $FreeMemory -lt 6291456 ];then
       echo "Insuffiecient Memory 'Require 6G Free Memory'"
       exit 1
fi

Check the any other installation' s are in progress?

if lsof /var/lib/apt/lists/lock >>/dev/null 2>&1 ;then
                echo "/var/lib/apt/lists/lock  locked"
                exit 1
fi

if lsof /var/lib/dpkg/lock >>/dev/null 2>&1 ;then
                echo "/var/lib/dpkg/lock  locked"
                exit 1
fi

Check the availability of Port?

nc -z 127.0.0.1 80;

if test "$?" -eq "0"  #if port 80 is in Listening mode
then
        echo "Port 80 is not available."
        exit 1
fi


Note: Tested under Ubuntu 12.x& 14.x

How to repair the MongoDB with bash script?


repair_and_restart_mongod(){

  if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi

  ENABLE_MONGODB="yes"
  if [ "x$ENABLE_MONGODB" = "xyes" ]; then
        if [ -f $MONGODB_HOME/mongod.lock ]; then
                rm $MONGODB_HOME/mongod.lock >> /dev/null
                sudo -u mongodb /usr/bin/mongod --config /etc/mongodb.conf --repair >> /dev/null
        fi
        #exec start-stop-daemon --start --quiet --chuid mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf
        service mongodb start >> /dev/null  2>&1
  fi
}

Tested under Ubuntu Server 12.x& 14.x

How to check the MongoDB Status in bash script?


is_mongod_running(){
PID=$(cat $MONGODB_HOME/mongod.lock)
        status=$(service mongodb status 2>&1)
        echo $status | grep -q "mongodb start/running, process.*"

        if [ $? -eq 0 ] && [ "x$PID" != "x" ] ;then     #&& kill -0 $PID 2>/dev/null; then
                _status=0 #echo "mongod is running"
        else
                _status=1 #echo "mongod is not running"
        fi
        return $_status
}


Note: Tested under Ubuntu Server 2.x& 14.x

How to check the Apache Tomcat Status with bash script?


##check the status of tomcat
is_tomcat_running(){
        PID=$(cat $TOMCAT/conf/catalina.pid 2>&1)
        if [ "x$PID" != "x" ]&& kill -0 $PID 2>/dev/null; then
                _status=0 #echo "tomcat already running"
        else
                _status=1 #echo "tomcat is not running"
        fi
        return $_status
}

How to stop the Tomcat with bash script?


stop() {
  pid==$(cat $TOMCAT/conf/catalina.pid 2>&1)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        sh $CATALINA_HOME/bin/shutdown.sh

    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done

    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi

  return 0
}


Note: We need to kill/terminate the tomcat if it is failed to shutdown with in the given period of time& we need to clear the PID.

How to check Apache Web Server Status in Ubuntu with bash script?


##Check Apache Web Server running
is_apache_running(){
        a=$(service apache2 status) >> /dev/null
        if [ "$a" = " * apache2 is running" ];
        then
                _status=0  #echo "Apache WebServer Already Running"
        else
                _status=1  #echo "Apache WebServer is not Running"
        fi
        return $_status
}


##Check Apache Syntax
is_apache_syntax_ok(){

_syntax=$(apache2ctl -t 2>&1)

if [ "$_syntax" = "Syntax OK" ]; then
         _syntax_ok=0
else
         _syntax_ok=1
fi
        return  $_syntax_ok
}

Above snippet is tested under Ubuntu Server 12.x & 14.x.

How to validate Fully Qualified Domain with bash script?

#!/bin/bash

fqdn="www.google.com"

#FQDN Validation
{
        echo $fqdn | grep -P "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"
} && {
        echo "valid FQDN"  
} || {
        echo "Invalid FQDN Address"
exit 1
}


How to validate the email with bash script?


If else structure created with shell script operators

{
// shell script snippet A
} && {
// shell script snippet B
} || {
//shell script snippet C
}

snippet A executes, B will execute upon successful execution of script A & C will execute upon unsuccessful execution of script A.

grep is a linux command based tool to search the string match pattern' s where -E option for extended regular expression evaluation.



#!/bin/bash
email="test@gmail.com"

#Email Validation
{
echo  | grep -E "^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
} && {
echo "Valid E-Mail Address"
} || {
echo "Invalid E-Mail Address"
exit 1
}







Friday, 3 June 2016

How to check MongoDB is running with linux shell script?


#!/bin/bash

if pgrep mongod >>/dev/null ;then
    echo "Mongo DB Server is Running"
else
    echo "Mongo DB Server is not Running"
fi


Explanation: pgrep is signal process, it looks through running process and list the process id' s that matches the name of the process.