cancel
Showing results for 
Search instead for 
Did you mean: 

Shell scripting help

 
kaushikbr
Frequent Advisor

Shell scripting help

Hi

 

I'm writing a shell script to carry out some user checks on more than one server.

I have a main script which is run from a central server ( so to speak ).

This main script calls another sub-script on the remote servers, using ssh -n <node> script.

 

The ssh process for remote script invocation sometimes hang and as a result the main script waits indefinitely for the ssh to finish. But when I check on the remote node, I cannot find the sub-script running. 

 

I need a method to check for these hanging ssh processes in my main script and terminate them after a set timeout.

However I do not want the main script to wait for a timeout period after each invocation of ssh.

 

Main script invoking ssh -

 

awk // /etc/passwd | while read line
do
  CUNAME="$(echo ${line} | awk -F: '{ print $1 }')"
  CUID="$(echo ${line} | awk -F: '{ print $3 }')"


  #echo ${CUNAME} | ${GREP} -f /admin/CK_UNIX/CK_UNIX_ignore_users.lis > /dev/null
  ${GREP} -w "${CUNAME}" /admin/CK_UNIX/CK_UNIX_ignore_users.lis > /dev/null
  if [ $? -ne 0 ]
  then
     #echo ${CUNAME}
     for Node in ${CAE_MGT} ${CAE_ALL}
     do
       SSH_CMD="ssh -n ${Node} ${CK_UNIX_DIR}/CK_UNIX_verify_user.ksh -u ${CUNAME} -i ${CUID}"
       if [[ "${Node}" = "fils2005" || "${Node}" = "fils2006" ]]
       then
          SHELL2USE="/bin/sh"
          SSH_CMD="ssh -n ${Node} ${SHELL2USE} ${CK_UNIX_DIR}/CK_UNIX_verify_user.ksh -u ${CUNAME} -i ${CUID}"
       fi
       RETVAL="$(${SSH_CMD})"
     done
  fi
done

 

 

The main script sometimes waits for the "ssh -n ${Node} ${CK_UNIX_DIR}/CK_UNIX_verify_user.ksh -u ${CUNAME} -i ${CUID}" to process to finish ( since this process does not finish, even after the sub-script CK_verify_user.ksh has existed on the remote node ).

Thank you all for your valuable comments.

 

Regards

K

 

3 REPLIES 3

Re: Shell scripting help

I don't know what your CK_UNIX_verify_user.ksh script is doing, but it doesn't sound like something terribly complicated that should cause a hang?

 

>> The ssh process for remote script invocation sometimes hang and as a result the main script waits indefinitely for the ssh to finish. But when I check on the remote node, I cannot find the sub-script running. 

 

Are you sure it ever actually got to run? Are you sure that ssh is setup correctly such that the remote system isn't requesting a password? Whenever I include ssh in a script is add the -o "batchmode yes" argument to my ssh command line so I know I'll get an error rather than a hang in that situation.


I am an HPE Employee
Accept or Kudo
kaushikbr
Frequent Advisor

Re: Shell scripting help

Hi

 

The sub-script prints some messages to the console, messages such as "Invalid user" , "User OK" and soon.

I found this info about ssh hang

http://www.openssh.org/faq.html#3.10, look at section 3.10.

Since this is what I'm seeing in my script and I cannot really redirect the output to dev/null, I modified the script slightly

 

for line in $(awk -F: '{ printf "%s:%s\n",$1,$3 }' /etc/passwd)
do
  CUNAME="$(echo ${line} | awk -F: '{ print $1 }')"
  CUID="$(echo ${line} | awk -F: '{ print $2 }')"

  ${GREP} -w "${CUNAME}" /admin/CK_UNIX/CK_UNIX_ignore_users.lis > /dev/null
  if [ $? -ne 0 ]
  then
     #echo ${CUNAME}

     for Node in ${MGT_NODES} ${ALL_NODES}
     do
       case ${Node} in
          esuk1man|eguk1man)
            SSH_CMD="ssh ${Node} ${CK_UNIX_DIR}/CK_UNIX_verify_user.ksh -u ${CUNAME} -i ${CUID}"
            ${SSH_CMD}
          ;;
          eguk1ds[12])
            # Nothing to do on these nodes
          ;;
          *)
             RPASSWDLINE="$(ssh ${Node} grep -w ${CUNAME} /etc/passwd)"
             if [ "X${RPASSWDLINE}" != "X" ]
             then
                  RUID="$(echo ${RPASSWDLINE} | awk -F: '{ print $3 }')"
                  if [ "X${CUID}"  != "X${RUID}" ]
                  then
                      print_message ${Node} 1 ${CUNAME}
                  fi
             fi
          ;;
       esac
     done
  fi
done

 

However I cannot explain why ssh works without any problem for the two nodes esuk1man and eguk1man.

 

Thanks and Regards

K

Dennis Handly
Acclaimed Contributor

Re: Shell scripting help

>${GREP} -w "${CUNAME}" /admin/CK_UNIX/CK_UNIX_ignore_users.lis > /dev/null

 

This isn't your problem but you can remove that /dev/null by using -q:

${GREP} -q -w "${CUNAME}" /admin/CK_UNIX/CK_UNIX_ignore_users.lis