su option is not fetching the password

Hi,

Please find the below code.

function ABC
{
    echo "Enter Element Name:"
       read ename
       echo "Enter Element Source path:"
       read spath
       echo "Enter Element Destination path:"
       read dpath
       echo "cp -p $spath/$ename $dpath/$ename"
       echo "chmod 774 $dpath/$ename"
}
echo      "Do you want to initiate the process ${bold}(Y/N?)${offbold}"
read inp
if [ $inp == "Y" -o $inp == "y" ] ;
then
............................
............................
............................
echo " Does this involves any other files? ${bold}(Y/N?)${offbold}"
read inp
    if [ $inp == "Y" -o $inp == "y" ] ;
    then
   
    echo "***************************** "
    echo " Enter the abcuser password "
    echo "***************************** "
      (
    while [ $inp == "Y" -o $inp == "y" ] ;
       do
      ABC
    echo "Do you have any other elements?${bold}(Y/N?)${offbold}"
       read inp
      done
      ) | su abcuser
    fi
  echo "Program has been executed."
fi

If i execute it, i am getting password prompt for switching into abcuser. But it is not fetching the password and moving to next step.

Getting response like below:

*****************************
 Enter the abcuser password
*****************************
Password:





su: Sorry
Program has been executed

Please help me to fix this issue.

Regards,
gggg:wall:

Hello, could you put set -x and launch script again.

Please find the output which has been executed in debug mode.

Does this involves any other files? (Y/N?)

+ read inp
y
+ [ y == Y -o y == y ]
+ echo *****************************
*****************************
+ echo  Enter the abcuser password
 Enter the abcuser password
+ echo *****************************
*****************************
+ su abcuser
+ [ y == Y -o y == y ]
+ echo Enter Element Name:
+ read ename
Password: + echo Enter Element Source path:
+ read spath
+ echo Enter Element Destination path:
+ read dpath
+ echo cp -p /dsf/cfsabc fdf/cfsabc
+ echo chmod 774 fdf/cfsabc
+ echo Do you have any other elements?(Y/N?)
+ read inp
+ [ y == Y -o y == y ]
+ echo Enter Element Name:
+ read ename
+ echo Enter Element Source path:
+ read spath
+ echo Enter Element Destination path:
+ read dpath
+ echo cp -p /dsf/cfsegf fdf/cfsegf
+ echo chmod 774 fdf/cfsegf
+ echo Do you have any other elements?(Y/N?)
+ read inp
+ [ n == Y -o n == y ]




su: Sorry
+ echo program has been executed.

Its taking password as element name cfsabc like its moving on for next inputs.

Look and figure out what is happening:

ant:/home/vbe $ ./test01
hello
su test01
This script  is shell PID:  29792
Enter passwd
Password: 
ant:/home/vbe $ echo $$
29793
ant:/home/vbe $ exit
hihi back to $$ : 29792
done

How do you expect to pass information from process 29792 to 29793 ?

Then Please let me know how i can update my script.:wall:

Either using expect utility or by using files descriptors I suppose...
su command has no options for passwd so it will always prompt... I tried with heredocs without success (but didnt pass much time either...) there is a no very elegant nor secure way if you know which user you want to allow to "su", its using rlogin and a .rhosts file...
like that you will not have any passwd to enter...
you should write your script in such a way all you enter in interative is to generate a newscript that you pass with the su command or rlogin e.g. for su:

 su <newuser> -c ~/newscript 

with rlogin:

ant:/home/vbe $ remsh ant -l jju -n date
Tue Aug 28 17:44:38 METDST 2012

Another way would be using the sudo utility, which can be configured to allow one user to do one specific thing as another user. This would be better than installing a third-party brute-forcing utility to inject insecure plaintext passwords into an otherwise secure login system.

This +1 - It looks like sudo is exactly what you need for this task.

But an alternative might be to dabble with setuid instead. Extremely bad idea to do this with scripts as you can trick the OS into running arbitrary code that way, but with code that's not interpreted at runtime (ie compiled code) it works a treat.

1 Like

Can anyone explain me how to use sudo option with example?

---------- Post updated 08-29-12 at 01:49 AM ---------- Previous update was 08-28-12 at 11:10 PM ----------

After doing su, my script get inputs from user. That was the reason for problem. Now i updated script to get input from separate function and then called in su.

It works.

Code:

function ABC
{
       echo "cp -p $spath/$ename $dpath/$ename"
       echo "chmod 774 $dpath/$ename"
}
echo      "Do you want to initiate the process ${bold}(Y/N?)${offbold}"
read inp
if [ $inp == "Y" -o $inp == "y" ] ;
then
............................
............................
............................
echo " Does this involves any other files? ${bold}(Y/N?)${offbold}"
read inp
    while [ $inp == "Y" -o $inp == "y" ] ;
       do
        echo "Enter Element Name:"
       read ename
       echo "Enter Element Source path:"
       read spath
       echo "Enter Element Destination path:"
       read dpath
   
    echo "***************************** "
    echo " Enter the abcuser password "
    echo "***************************** "
      (
       ABC
      ) | su abcuser
 echo "Do you have any other elements?${bold}(Y/N?)${offbold}"
       read inp
       done
  echo "Program has been executed."

---------- Post updated at 01:49 AM ---------- Previous update was at 01:49 AM ----------

[FONT=Courier New]
After doing su, my script get inputs from user. That was the reason for problem. Now i updated script to get input from separate function and then called in su.

It works.

Code:

function ABC
{
       echo "cp -p $spath/$ename $dpath/$ename"
       echo "chmod 774 $dpath/$ename"
}
echo      "Do you want to initiate the process ${bold}(Y/N?)${offbold}"
read inp
if [ $inp == "Y" -o $inp == "y" ] ;
then
............................
............................
............................
echo " Does this involves any other files? ${bold}(Y/N?)${offbold}"
read inp
    while [ $inp == "Y" -o $inp == "y" ] ;
       do
        echo "Enter Element Name:"
       read ename
       echo "Enter Element Source path:"
       read spath
       echo "Enter Element Destination path:"
       read dpath
   
    echo "***************************** "
    echo " Enter the abcuser password "
    echo "***************************** "
      (
       ABC
      ) | su abcuser
 echo "Do you have any other elements?${bold}(Y/N?)${offbold}"
       read inp
       done
  echo "Program has been executed."

Thanks everyone