Script Testing

Hello Scripting Czars

I have written one script, which does the Openssl Connection test to 1000's of server's , but before it finally takes off , its error-ring out, kindly take a look into this.

#!/bin/bash
#Script to Test Connectivity withListen Address & Port Address

#Script Executiong Path
#export $PATH=$HOME:/bin:/usr/bin/:/usr/sfw/bin:/tools:/Production

#Connectivity Test Using TelNet & OpenSsl Connectivity Test
#TEST1=/usr/bin/telnet
OPSS_TEST=/usr/sfw/bin/openssl

# Script name without path
BASENAME=`basename $0`

# Script Location
HOME=/home/asm3808

#OPENSSL TesT Configuration Directory
OPSS_CGI_AB=$HOME/conf/OPSS-Test/OPSS_CGI_ABUSE_LST
OPSS_CGI_XSS=$HOME/conf/OPSS-Test/OPSS_CGI_XSS_LST
OPSS_FTP_SRV=$HOME/conf/OPSS-Test/OPSS_FTP_SRV_LST
OPSS_GEN_SRV=$HOME/conf/OPSS-Test/OPSS_GEN_SRV_LST
OPSS_MIS_SRV=$HOME/conf/OPSS-Test/OPSS_MIS_SRV_LST
OPSS_COMP_SRV=$HOME/conf/OPSS-Test/COM_SRV_LST
OPSS_RPC_SRV=$HOME/conf/OPSS-Test/OPSS_RPC_SRV_LST
OPSS_SRV_DET=$HOME/conf/OPSS-Test/SRV_SRV_LST
OPSS_SMT_PROB=$HOME/conf/OPSS-Test/OPSS_SMT_PROB_LST
OPSS_SNM_SRV=$HOME/conf/OPSS-Test/OPSS_SNM_SRV_LST
OPSS_WEB_SER=$HOME/conf/OPSS-Test/WEB_SRV_LST
OPSS_WINDOW=$HOME/conf/OPSS-Test/WIN_SRV_LST

#The changes will write to OUTFILE

OUTFILE=$HOME/conf/

DATE=`date`


# Functional Usage

usage() {
         if [ "$2" != 0 ]|| [ "$1" != 0 ]
        then
        echo
        echo "  Check USAGE for correct Parameters  "
        fi
        echo "   $BASENAME  <OPTION> "
        echo
        echo "OPTION:   -a = Connectivity Test Report for CGI-Abuse Family
                                -b = Connectivity Test Report for CGI-XSS Family
                                -c = Connectivity Test Report for FTP Family
                                -d = Connectivity Test Report for General Cateory
                                -e = Connectivity Test Report for Misc
                                        -f  = Connectivity Test Repport for Compilance Category
                                        -g = Connectivity Test Report for RPC Family
                        -h = Connectivity Test Service Detection
                -i  = Connectivity  Test SMTP Problem
                -j  = Connectivity Test for SNMP
                -k  = Connectivity Test WebServers
                -l   = Connectivity Test to WINDOWs Servers. "
        exit
        }


connectivityTest () {

#Local Variables
      #
       local STATE="$1"
      #
    case $1 in
        -a|-A) echo "Trying to test ${OPSS_CGI_AB}"
         while read a; do
                    echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -a
                         done  < "${OPSS_CGI_AB}"
                 if [ $? != 0]
                                then
                                echo " Connectivity ${OPSS_CGI_AB} : Failed  `$tr` Port Closed "
                else
                echo " Test Succeeded : ${OPSS_CGI_AB}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

       -b|-B) echo "Trying to test ${OPSS_CGI_XSS}"
        while read a; do
        echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect  -b
                         done  < "${OPSS_CGI_XSS}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_CGI_XSS} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_CGI_XSS}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

     -c|-C) echo "Trying to test ${OPSS_FTP_SRV}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -c
                         done  < "${OPSS_FTP_SRV}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_FTP_SRV} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_FTP_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

    -d|-D) echo "Trying to test $OPSS_GEN_SRV"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -d
                         done  < "${OPSS_GEN_SRV}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_GEN_SRV} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_GEN_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

   -e|-E)echo "Trying to test ${OPSS_MIS_SRV}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -e
                         done  < "${OPSS_MIS_SRV}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_MIS_SRV} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_MIS_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

    -f|-F) echo "Trying to test ${OPSS_COMP_SRV}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -f
                         done  < "${OPSS_COMP_SRV}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_COMP_SRV} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_COMP_SRV}  `$tr` Refer to the Nessus Solution"
                exit 1

    -f|-F) echo "Trying to test ${OPSS_COMP_SRV}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -f
                         done  < "${OPSS_COMP_SRV}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_COMP_SRV} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_COMP_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

     -g|-G) echo "Trying to test ${OPSS_RPC_SRV}"
        while read a; do
          ${OPSS_TEST} s_client -debug -connect  -g
                         done  < "${OPSS_RPC_SRV}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_RPC_SRV} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_RPC_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

     -h|-H) echo "Trying to test ${OPSS_SRV_DET}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect  -h
                         done  < "${OPSS_SRV_DET}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_SRV_DET} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_SRV_DET}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

    -i|-I) echo "Trying to test ${OPSS_SMT_PROB}"
        while read a; do
          ${OPSS_TEST} s_client -debug -connect -i
                         done  < "${OPSS_SMT_PROB}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_SMT_PROB} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_SMT_PROB}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

     -j|-J) echo "Trying to test ${OPSS_SNM_SRV}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect  -j
                         done  < "${OPSS_SNM_SRV}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_SNM_SRV} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_SNM_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

     -k|-K)echo "Trying to test ${OPSS_WEB_SER}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect  -k
                         done  < "${OPSS_WEB_SER}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_WEB_SER} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_WEB_SER}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1

     -l|-L) echo "Trying to test ${OPSS_WINDOW}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -l
                         done  < "${OPSS_WINDOW}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_WINDOW} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_WINDOW}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1
}
exit

Error is:

./OpenSSLConTest.sh: line 84: syntax error near unexpected token `)'
./OpenSSLConTest.sh: line 84: `       -b|-B) echo "Trying to test ${OPSS_CGI_XSS}"'

I would add a space after 0 in all of your tests:

if [ $? != 0 ]

vbe,

There was an error in case statement, I rectified and added space after 0 in exit status check, but found to be an error in Line 83

./OpenSSLConTest.sh
./OpenSSLConTest.sh: line 83: syntax error near unexpected token `;;'
./OpenSSLConTest.sh: line 83: `   ;;'

each "case" statements must be closed with ";;"
Reference -- [Chapter 5] 5.3 case
below is sample modification of your code. Change it accordingly ..

-l|-L) echo "Trying to test ${OPSS_WINDOW}"
        while read a; do
          echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -l
                         done  < "${OPSS_WINDOW}"
                if [ $? != 0]
                                 then
                                echo " Connectivity ${OPSS_WINDOW} : Failed  `$tr` Port Closed "
                 else
                echo " Test Succeeded : ${OPSS_WINDOW}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
                exit 1
                ;;

Yes, I have modified the code, the case statement was not closed before. Below is an example.

case $1 in
        -a|-A) echo "Trying to test ${OPSS_CGI_AB}"
         while read a; do
                    echo "------------------------------"
          ${OPSS_TEST} s_client -debug -connect -a
                         done  < "${OPSS_CGI_AB}"
                 if [ $? != 0 ]
                                then
                                echo " Connectivity ${OPSS_CGI_AB} : Failed  `$tr` Port Closed "
                else
                echo " Test Succeeded : ${OPSS_CGI_AB}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
          ;;

Attached the modified code, please review it.

Yes .. it sounds good .. try and let us know if any errors faced ..

Jay,

Its the same result, no difference . First I want to test for usage, there only getting failed.

$ ./OpenSSLConTest.sh
./OpenSSLConTest.sh: line 83: syntax error near unexpected token `;;'
./OpenSSLConTest.sh: line 83: `   ;;'

I modified the usage something like this:

if [ "$2" != 0 ]|| [ "$1" != 0 ]
then
usage() {
        echo
        echo "  Check USAGE for correct Parameters  "
        echo "   $BASENAME  <OPTION> "
        echo
        echo "OPTION:   -a = Connectivity Test Report for CGI-Abuse Family
                                -b = Connectivity Test Report for CGI-XSS Family
                                -c = Connectivity Test Report for FTP Family
                                -d = Connectivity Test Report for General Cateory
                                -e = Connectivity Test Report for Misc
                                        -f  = Connectivity Test Repport for Compilance Category
                                        -g = Connectivity Test Report for RPC Family
                        -h = Connectivity Test Service Detection
                -i  = Connectivity  Test SMTP Problem
                -j  = Connectivity Test for SNMP
                -k  = Connectivity Test WebServers
                -l   = Connectivity Test to WINDOWs Servers. "
        exit
        }
fi

you are calling functions, better run the script with "bash" shell
for me, its working good ..

$ cat filename
#!/bin/bash
function usage
{
        pwd
        date
}
case $1 in
        1)
        echo "hi"
        ;;
        2)
        echo "bye"
        ;;
        *)
        usage
        ;;
esac
$ bash filename 1
hi
$ bash filename 2
bye
$ bash filename 3
/your/home/path
Fri Sep 30 13:51:10 BST 2011
$

Jay,

I have removed the function and i tried again, its the same result.

./OpenSSLConTest.sh
./OpenSSLConTest.sh: line 77: syntax error near unexpected token `;;'
./OpenSSLConTest.sh: line 77: `   ;;'

Should you not also close with fi all your if statements? (your indentation is difficult to follow...)

#!/bin/bash
#Script to Test Connectivity withListen Address & Port Address

#Script Executiong Path
#export $PATH=$HOME:/bin:/usr/bin/:/usr/sfw/bin:/tools:/Production

#Connectivity Test Using TelNet & OpenSsl Connectivity Test
#TEST1=/usr/bin/telnet
OPSS_TEST=/usr/sfw/bin/openssl

# Script name without path
BASENAME=`basename $0`

# Script Location
HOME=/home/myAccount

#OPENSSL TesT Configuration Directory
OPSS_CGI_AB=$HOME/conf/OPSS-Test/OPSS_CGI_ABUSE_LST
OPSS_CGI_XSS=$HOME/conf/OPSS-Test/OPSS_CGI_XSS_LST
OPSS_FTP_SRV=$HOME/conf/OPSS-Test/OPSS_FTP_SRV_LST
OPSS_GEN_SRV=$HOME/conf/OPSS-Test/OPSS_GEN_SRV_LST
OPSS_MIS_SRV=$HOME/conf/OPSS-Test/OPSS_MIS_SRV_LST
OPSS_COMP_SRV=$HOME/conf/OPSS-Test/COM_SRV_LST
OPSS_RPC_SRV=$HOME/conf/OPSS-Test/OPSS_RPC_SRV_LST
OPSS_SRV_DET=$HOME/conf/OPSS-Test/SRV_SRV_LST
OPSS_SMT_PROB=$HOME/conf/OPSS-Test/OPSS_SMT_PROB_LST
OPSS_SNM_SRV=$HOME/conf/OPSS-Test/OPSS_SNM_SRV_LST
OPSS_WEB_SER=$HOME/conf/OPSS-Test/WEB_SRV_LST
OPSS_WINDOW=$HOME/conf/OPSS-Test/WIN_SRV_LST

#The changes will write to OUTFILE

OUTFILE=$HOME/conf/

DATE=`date`


# Functional Usage

usage() {
         if [ "$2" != 0 ]|| [ "$1" != 0 ]
        then
        echo
        echo "  Check USAGE for correct Parameters  "
        echo "   $BASENAME  <OPTION> "
        echo
        echo "OPTION:   -a = Connectivity Test Report for CGI-Abuse Family
                                -b = Connectivity Test Report for CGI-XSS Family
                                -c = Connectivity Test Report for FTP Family
                                -d = Connectivity Test Report for General Cateory
                                -e = Connectivity Test Report for Misc
                                        -f  = Connectivity Test Repport for Compilance Category
                                        -g = Connectivity Test Report for RPC Family
                        -h = Connectivity Test Service Detection
                -i  = Connectivity  Test SMTP Problem
                -j  = Connectivity Test for SNMP
                -k  = Connectivity Test WebServers
                -l   = Connectivity Test to WINDOWs Servers. "
        exit
        fi
        }


connectivityTest () {

#Local Variables
      #
       local STATE="$1"
      #

    case $1 in
        -a|-A) echo "Trying to test ${OPSS_CGI_AB}"
         while read a; 
         do
              echo "------------------------------"
                ${OPSS_TEST} s_client -debug -connect -a
         done  < "${OPSS_CGI_AB}"
         if [ $? != 0 ]
         then
               echo " Connectivity ${OPSS_CGI_AB} : Failed  `$tr` Port Closed "
               else
               echo " Test Succeeded : ${OPSS_CGI_AB}  `$tr` Refer to the Nessus Solution"
               echo "------------------------------"
         fi
         ;;
       -b|-B) echo "Trying to test ${OPSS_CGI_XSS}"
        while read a; 
        do
                echo "------------------------------"
                ${OPSS_TEST} s_client -debug -connect  -b
        done  < "${OPSS_CGI_XSS}"
        if [ $? != 0 ]
        then
               echo " Connectivity ${OPSS_CGI_XSS} : Failed  `$tr` Port Closed "
               else
               echo " Test Succeeded : ${OPSS_CGI_XSS}  `$tr` Refer to the Nessus Solution"
               echo "------------------------------"
        fi
        ;;
     -c|-C) echo "Trying to test ${OPSS_FTP_SRV}"
        while read a; 
        do
           echo "------------------------------"
           ${OPSS_TEST} s_client -debug -connect -c
        done  < "${OPSS_FTP_SRV}"
        if [ $? != 0 ]
        then
               echo " Connectivity ${OPSS_FTP_SRV} : Failed  `$tr` Port Closed "
        else
               echo " Test Succeeded : ${OPSS_FTP_SRV}  `$tr` Refer to the Nessus Solution"
               echo "------------------------------"
        fi
        ;;
    -d|-D) echo "Trying to test $OPSS_GEN_SRV"
        while read a; 
        do
             echo "------------------------------"
             ${OPSS_TEST} s_client -debug -connect -d
        done  < "${OPSS_GEN_SRV}"
        if [ $? != 0 ]
        then
               echo " Connectivity ${OPSS_GEN_SRV} : Failed  `$tr` Port Closed "
        else
               echo " Test Succeeded : ${OPSS_GEN_SRV}  `$tr` Refer to the Nessus Solution"
               echo "------------------------------"
        fi
        ;;
   -e|-E)echo "Trying to test ${OPSS_MIS_SRV}"
        while read a; 
        do
            echo "------------------------------"
            ${OPSS_TEST} s_client -debug -connect -e
        done  < "${OPSS_MIS_SRV}"
        if [ $? != 0 ]
        then
               echo " Connectivity ${OPSS_MIS_SRV} : Failed  `$tr` Port Closed "
        else
               echo " Test Succeeded : ${OPSS_MIS_SRV}  `$tr` Refer to the Nessus Solution"
               echo "------------------------------"
        fi
        ;;
    -f|-F) echo "Trying to test ${OPSS_COMP_SRV}"
        while read a; 
        do
            echo "------------------------------"
            ${OPSS_TEST} s_client -debug -connect -f
        done  < "${OPSS_COMP_SRV}"
        if [ $? != 0 ]
        then
                echo " Connectivity ${OPSS_COMP_SRV} : Failed  `$tr` Port Closed "
        else
                echo " Test Succeeded : ${OPSS_COMP_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
        fi
        ;;
     -g|-G) echo "Trying to test ${OPSS_RPC_SRV}"
        while read a; 
        do
            ${OPSS_TEST} s_client -debug -connect  -g
        done  < "${OPSS_RPC_SRV}"
        if [ $? != 0 ]
        then
                echo " Connectivity ${OPSS_RPC_SRV} : Failed  `$tr` Port Closed "
        else
                echo " Test Succeeded : ${OPSS_RPC_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
        fi
        ;;
     -h|-H) echo "Trying to test ${OPSS_SRV_DET}"
        while read a; 
        do
            echo "------------------------------"
            ${OPSS_TEST} s_client -debug -connect  -h
        done  < "${OPSS_SRV_DET}"
        if [ $? != 0 ]
        then
                echo " Connectivity ${OPSS_SRV_DET} : Failed  `$tr` Port Closed "
        else
                echo " Test Succeeded : ${OPSS_SRV_DET}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
        fi
        ;;
    -i|-I) echo "Trying to test ${OPSS_SMT_PROB}"
        while read a; 
        do
            ${OPSS_TEST} s_client -debug -connect -i
        done  < "${OPSS_SMT_PROB}"
        if [ $? != 0 ]
        then
                echo " Connectivity ${OPSS_SMT_PROB} : Failed  `$tr` Port Closed "
        else
                echo " Test Succeeded : ${OPSS_SMT_PROB}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
        fi
        ;;
     -j|-J) echo "Trying to test ${OPSS_SNM_SRV}"
        while read a; 
        do
            echo "------------------------------"
            ${OPSS_TEST} s_client -debug -connect  -j
        done  < "${OPSS_SNM_SRV}"
        if [ $? != 0 ]
        then
                echo " Connectivity ${OPSS_SNM_SRV} : Failed  `$tr` Port Closed "
        else
                echo " Test Succeeded : ${OPSS_SNM_SRV}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
        fi
        ;;
     -k|-K)echo "Trying to test ${OPSS_WEB_SER}"
        while read a; 
        do
            echo "------------------------------"
            ${OPSS_TEST} s_client -debug -connect  -k
        done  < "${OPSS_WEB_SER}"
        if [ $? != 0 ]
        then
                echo " Connectivity ${OPSS_WEB_SER} : Failed  `$tr` Port Closed "
        else
                echo " Test Succeeded : ${OPSS_WEB_SER}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
        fi
        ;;
     -l|-L) echo "Trying to test ${OPSS_WINDOW}"
        while read a; 
        do
            echo "------------------------------"
            ${OPSS_TEST} s_client -debug -connect -l
        done  < "${OPSS_WINDOW}"
        if [ $? != 0 ]
        then
                echo " Connectivity ${OPSS_WINDOW} : Failed  `$tr` Port Closed "
        else
                echo " Test Succeeded : ${OPSS_WINDOW}  `$tr` Refer to the Nessus Solution"
                echo "------------------------------"
        fi
        ;;
          *) usage
        ;;
        esac
        }
exit 1

Can you please guide me, where this script is erroring out .

---------- Post updated at 11:36 PM ---------- Previous update was at 09:54 AM ----------

The Script seems to be almost perfect except reading the data from the Conf File.

$ ./OpenSSLConTest.sh

   Check USAGE for correct Parameters
  ------------------------------------

   OpenSSLConTest.sh  <OPTION>

   OPTION:              -a = Connectivity Test Report for CGI-Abuse Family
                                -b = Connectivity Test Report for CGI-XSS Family
                                -c = Connectivity Test Report for FTP Family
                                -d = Connectivity Test Report for General Cateory
                                -e = Connectivity Test Report for Misc
                                -f = Connectivity Test Report for Compilance Category
                                -g = Connectivity Test Report for RPC Family
                                -h = Connectivity Test Service Detection
                                -i = Connectivity Test SMTP Problem
                                -j = Connectivity Test for SNMP
                                -k = Connectivity Test WebServers
                                -l = Connectivity Test to WINDOWs Servers.

$ ./OpenSSLConTest.sh -i
Trying to test /home/myHome/conf/OPSS-Test/SMT_PROB
------------------------------
no port defined
usage: s_client args

The script is trying to test the Conf File instead of the Hostname's & ports mentioned in it. That means script is not reading the file and not issuing the HOSTNAME:PORT as Input argument for the OPENSLL TEST, because of which I received the Output

Trying to test /home/myHome/conf/OPSS-Test/SMT_PROB
------------------------------
no port defined
usage: s_client args

Contents of /home/myHome/conf/OPSS-Test/SMT_PROB

HOST1:PORT
HOST2:PORT
HOST3:PORT
HOST4:PORT
HOST5:PORT