12 Replies Latest reply on Nov 5, 2012 10:54 PM by Abin Mammala

    Pass parameters into a script?

    Abin Mammala

      Hi,

       

      I am lokking for an option to pass the following parameters in to the script from a text/excel file:

       

      - Server name

      - Oracle Instance name

      - Old password

      - New password

       

      Thanks in advance.

       

      Abin M Mammala

        • 1. Re: Pass parameters into a script?

          cat input.file | while read ServerName OraInstanceName Oldpass NewPass

          do

               echo $ServerName $OraInstanceName $Oldpass $NewPass

          done

           

          instead of echo you put your intended usage for the variables.

          • 2. Re: Pass parameters into a script?
            Abin Mammala

            Hi Lazar

             

            Thanks for the quick response.

             

            Let me give you some more details.

             

            I have two scripts that need to be pushed to the temp dir of the servers.

            Then i need to execute these two scripts one by one on the servers. I need

            to pass the four variables which i mentioned in my last email at this stage

            to both the scripts.

             

            Also i just want to know the header/format/columns in the input file.

             

            Thanks in advance.

             

            Abin

            • 3. Re: Pass parameters into a script?

              Abin, I'm not even sure how to answer; the questions seem generic and there are many ways of implementation.

               

              To pass variables to the script you basically run ./script_name var1 var2 var3 var4. The script needs to be designed to use those variables: ServerName=$1; OraInstanceName=$2; Oldpass=$3; NewPass=$4

               

              The values can be passed to the script_name via multiple ways: NSH Script job with variables, Deploy Job with local properties of BLPackage. Multiple scripts can be executed via multiple NSH Script Jobs (wrapped  batch Job), or via Deploy Job with multiple external command items (the same Deploy Job can be used to push the two scripts to the tmp folder.

               

              Does your input file contain only 4 values or 4 columns with values? The answer to this question will also depend how you implement this.

               

              About the format of the file. The snippet above assumes that you have 4 strings space separated - very simple. If the values in your input file may have spaces, then you need to have another separator and use that when modifying 'cat input.file' command.

              • 4. Re: Pass parameters into a script?
                Abin Mammala

                Hi Lazar,

                 

                There are four values/columns per server. There will be more than 100

                servers.

                 

                Thanks

                Abin

                • 5. Re: Pass parameters into a script?
                  Bill Robinson

                  like lazar said, there are many many ways to do this.

                   

                  the file that you need to read, where will it be located?

                  will the values have spaces in them?

                  what is the delimited in the file ?

                  you have two scripts that need to consume the same file?  why not just 1 script ?  what are the scripts doing ?

                  • 6. Re: Pass parameters into a script?
                    Abin Mammala

                    Hi Bill,

                     

                    The file will be located in a central server and the content of the file would be:

                     

                    server1 instance1 oldpass newpass

                    server1 instance2 oldpass newpass

                    ......

                    ....

                     

                    The sciprts are for changing PatrolOracle Database passowrd and Patrol passwod and we need to execute them sequentially.

                     

                    Thanks

                    Abin

                    • 7. Re: Pass parameters into a script?

                      will targets have access to this central file?

                       

                      If you can write a script that you can run directly on the target that will pull the values from that file, then you can use this and create blpackage with external command to do so.

                       

                      if targets have no access to this file, then it would be NSH Script Job with central execution (provided that the central server has agent installed). The nsh script can:

                      - read the values from the central input file (code snippet give in first post)

                      - copy the two script (with should conveniently be placed on the same central server) to the target temp place

                      - use nexec to run those script on the targets passing the values obtained.

                      • 8. Re: Pass parameters into a script?
                        Abin Mammala

                        Yes, the target will have access to this file.

                         

                        However, I think I will go ahead with the second method.

                         

                        Thanks to Lazar and Bill...

                        • 9. Re: Pass parameters into a script?
                          Bill Robinson

                          can you attach the scripts ?

                          • 10. Re: Pass parameters into a script?
                            Abin Mammala

                            Hi Bill,

                             

                            As you asked, we can merge these two scripts and manyinitial checks can be avaoided). Our intention is to change the Patrol DB  password for all the instances on all the servers.

                             

                            Please find the scripts:

                             

                            patrol-change-oracle-pw-oracle.sh

                            ++++++++++++++++++++++++++++++++++++++

                             

                            #!/bin/ksh

                            PATH=/usr/bin:/bin

                            pw_read=false
                            newpw_read=false

                            if [[ ! -t 0 ]]; then
                                    if [[ $1 == --pw-on-stdin ]]; then
                                            shift
                                            read pw
                                            pw_read=true
                                            read newpw
                                            newpw_read=true
                                    else
                                            echo >&2 "No tty"
                                            exit 1
                                    fi
                            else
                                    if [[ $1 == --pw-on-stdin ]]; then
                                            echo >&2 "Don't pass --pw-on-stdin with a tty"
                                            exit 1
                                    fi
                            fi

                            ORACLE_SID=$1

                            if [[ -z $ORACLE_SID ]]; then
                                    echo >&2 "Please pass a SID to this script"
                                    exit 1
                            fi

                            ORACLE_HOME=$(awk -F: -v SID=$1 '$1 == SID {print $2; exit;}' /etc/oratab)

                            if [[ ! -d $ORACLE_HOME ]]; then
                                    echo >&2 "ORACLE_HOME for sid $ORACLE_SID not found"
                                    exit 1
                            fi

                            PATH=$ORACLE_HOME/bin:$PATH
                            LD_LIBRARY_PATH=$ORACLE_HOME/lib
                            export ORACLE_SID ORACLE_HOME LD_LIBRARY_PATH

                            if ! echo exit | sqlplus -S /nolog; then
                                    if [[ ! -x $ORACLE_HOME/bin/sqlplus ]]; then
                                            echo >&2 "$ORACLE_HOME/bin/sqlplus not executable"
                                            exit 1
                                    fi

                                    if [[ ! -f $ORACLE_HOME/lib/libclntsh.so ]]; then
                                            echo >&2 "$ORACLE_HOME/lib/libclntsh.so not accessible"
                                            exit 1
                                    fi
                            fi

                            function getpass {
                                    typeset pw
                                    stty -echo
                                    read pw?"$1"
                                    echo $pw
                                    echo >&2   # print a newline
                                    stty echo
                            }

                            while ! $pw_read; do
                                    trap 'stty echo' EXIT
                                    pw=$(getpass "Enter the current password for the oracle account patrol: ")
                                    [[ ! -z $pw ]] && pw_read=true
                            done

                            sqlplus -R 2 -S /nolog <<! || exit
                            whenever sqlerror exit failure
                            connect patrol/$pw
                            exit
                            !

                            echo "Access check successful"

                            while ! $newpw_read; do
                                    trap 'stty echo' EXIT
                                    newpw1=$(getpass "Enter the new password for the oracle account patrol: ")
                                    newpw2=$(getpass "Reenter the new password: ")
                                    if [[ "$newpw1" == "$newpw2" ]]; then
                                            newpw=$newpw1
                                            newpw_read=true
                                    fi
                            done

                            sqlplus -R 2 -S /nolog <<! || exit
                            whenever sqlerror exit failure
                            connect patrol/$pw
                            alter user patrol identified by "$newpw" replace "$pw";
                            connect patrol/$newpw
                            exit
                            !

                            echo "Password change appears successful"

                            sqlplus -R 2 -S /nolog <<! || exit
                            whenever sqlerror exit failure
                            connect patrol/$newpw
                            exit
                            !

                            echo "Access check successful"

                             

                             

                            patrol-change-oracle-pw-patrol.sh

                            +++++++++++++++++++++++++++++++++++++++++++++++++++++++

                             

                            #!/bin/ksh

                            PATH=/usr/bin:/bin

                            export PATROL_HOME=/opt/patrol/Patrol3

                            newpw_read=false

                            if [[ ! -t 0 ]]; then
                                    if [[ $1 == --pw-on-stdin ]]; then
                                            shift
                                            read newpw
                                            newpw_read=true
                                    else
                                            echo >&2 "No tty"
                                            exit 1
                                    fi
                            else
                                    if [[ $1 == --pw-on-stdin ]]; then
                                            echo >&2 "Don't pass --pw-on-stdin with a tty"
                                            exit 1
                                    fi
                            fi

                            ORACLE_SID=$1

                            if [[ -z $ORACLE_SID ]]; then
                                    echo >&2 "Please pass a SID to this script"
                                    exit 1
                            fi

                            ORACLE_HOME=$(awk -F: -v SID=$1 '$1 == SID {print $2; exit;}' /etc/oratab)

                            if [[ ! -d $ORACLE_HOME ]]; then
                                    echo >&2 "ORACLE_HOME for sid $ORACLE_SID not found"
                                    exit 1
                            fi

                            if [[ ! -d $PATROL_HOME ]]; then
                                    echo >&2 "PATROL_HOME not found"
                                    exit 1
                            fi

                            function getpass {
                                    typeset pw
                                    stty -echo
                                    read pw?"$1"
                                    echo $pw
                                    echo >&2   # print a newline
                                    stty echo
                            }

                            while ! $newpw_read; do
                                    trap 'stty echo' EXIT
                                    newpw1=$(getpass "Enter the new password for the oracle account patrol: ")
                                    newpw2=$(getpass "Reenter the new password: ")
                                    if [[ "$newpw1" == "$newpw2" ]]; then
                                            newpw=$newpw1
                                            newpw_read=true
                                    else
                                            echo >&2 "Passwords do not match. Try again."
                                    fi
                            done

                            target=$("$PATROL_HOME/target") || exit
                            PATH=$PATH:$PATROL_HOME/bin:$PATROL_HOME/$target/bin

                            function send_config {
                                    typeset ORACLE_SID=$1
                                    typeset newpw=$2
                                    typeset km
                                    echo PATROL_CONFIG
                                    COMMA=
                                    for km in ORACLE_AVAILABILITY ORACLE_CAPACITY ORACLE_COLLECTORS ORACLE_DB_LINKS ORACLE_ENVIRONMENT ORACLE_INSTANCE ORACLE_JOBS ORACLE_LOG ORACLE_MTS ORACLE_NETWORK ORACLE_PERFORMANCE ORACLE_PQO ORACLE_REPLICATION ORACLE_SGA ORACLE_SQLVIEWER ORACLE_TABLESPACES ORACLE_USERS; do
                                            echo "$COMMA\"/AgentSetup/$km.$ORACLE_SID.defaultAccount\" = { REPLACE = \"patrol/$newpw\" }\\c"
                                            COMMA=",
                            "
                                    done
                                    echo
                            }

                            send_config "$ORACLE_SID" "$newpw" | pconfig +Reload -- || exit

                            pconfig +RESTART || exit

                            echo "You are done! Good job!"

                             

                             

                            Thanks

                            Abin

                            • 11. Re: Pass parameters into a script?
                              Bill Robinson

                              so i don't really want to get into wrting your scripts here, so if you have a csv file that has like:

                               

                              value1,value2,value3,value4

                               

                              there's a few different ways to read that in - one way would be like:

                               

                              while read line

                              do

                              prop1=`echo ${line} | cut -f1 -d,`

                              prop2=`echo ${line} | cut -f2 -d,`

                              prop3=`echo ${line} | cut -f3 -d,`

                              prop4=`echo ${line} | cut -f4 -d,`

                               

                              echo do some stuff here

                               

                              done < file.csv

                              1 of 1 people found this helpful
                              • 12. Re: Pass parameters into a script?
                                Abin Mammala

                                Thank you Bill.....