LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   switch user using shell program. (https://www.linuxquestions.org/questions/linux-newbie-8/switch-user-using-shell-program-653849/)

av.dubey 07-06-2008 03:18 AM

switch user using shell program.
 
i want to write a shell program dat ll prompt the use to type the username and password and it will switch user...

like this .

test.sh

echo "enter username"
read username
echo "enter password"
read password

su - $username -p $password
pwd

but this is not working...
its giving me error as


-bash: intruder3: No such file or directory

can neone help me in this..

colucix 07-06-2008 03:56 AM

Quote:

Originally Posted by av.dubey (Post 3205368)
su - $username -p $password

I bet intruder3 is the password you've entered. You cannot use su in this way. Look at the man page: the -p option is not meant to input password. Furthermore, I think using su in a script is not possible at all, unless you're root and want to run a command as a regular user, for example
Code:

su username -c "echo Hello world!"
in this case you're not prompted for password, since root can switch to any other user without entering his password.

pradeep.sree 07-06-2008 04:25 AM

Try this script and let me know if you have any issue... :)

#!/usr/local/bin/expect -f
if {$argc!=2} {
send_user "usage: $argv0 username password \n"
exit
}
set timeout -1
match_max 100000
set password [lindex $argv 1]
set user [lindex $argv 0]
spawn $env(SHELL)
send -- "su - $user\r"
expect "assword:"
send "$password\r"
send "\r"
expect eof

dom83 07-06-2008 04:37 AM

Edit: I'm a bit slow. So here is another solution:


Do you just want to run the script as another user?

If thats the case I only found a workaround for this:
Code:

#!/bin/bash

# First call? Switch user.
if [ "$1" != "-nouserswitch" ] ; then
    echo "Username:"
    read username
    exec su $username -c "/home/dom/Temp/test.sh -nouserswitch"
fi

whoami

So you basically run your script twice. The first time to execute the script as another user. The second time (with the "-nouserswitch" argument) to actually run your script.

This solution does only work if you need the user switch at the very beginning of the script.

av.dubey 07-06-2008 04:57 AM

hi..predeep.sree
i tried ur script but its giving hell lot of errors...still thanx yaar..can u plzz check it urself y so many errors r comin.....

@dom83....
i tried ur script but i think ur not gettin wat m tryin to say..
cause ur script is switcin user but its asking for password as normaly it is asked wen we do su <username>
but i want a script in which once a user has provided username and password then the script shd itself switch the user ....n not ask for nethin else...

Mr. C. 07-06-2008 05:21 AM

There is nothing wrong with placing an su or sudo in a script. Onoe just has to understand the consequences, and how it require STDIN to be attached to the terminal.

Code:

$ cat /tmp/su-test.sh
#!/bin/bash

echo Hello
su -c 'echo Hello Root' root
echo All done

$ /tmp/su-test.sh
Hello
Password:
Hello Root
All done

av.dubey - my versions of su do not support a -p password option. The -p option is used to preseve the environment. Furthermore, the "pwd" command that follows you call to su won't be executed until the su-invoked shell exits.

Can you clarify better what you are trying to accomplish that is alread done with:

su - username
?

av.dubey 07-07-2008 10:41 AM

i don understand y m not able to make you clear abt wat i m tryin to do ...
see i want to make a shell program ..which will be run by normal user...
it will prompt for username and password ..
once the normal user will enter username as root and password as the root's password...then he will automatically be logged in as superuser and a switch case will be there that will enable him to run many root commands just by entering his choice as 1,2 or 3 ,,for example adduser,neat and all..

my main problem is that m not able to login as superuser using shell script..
i know we can do it manually like typine su - and den entering password when prompted...but cant it be automased using shell script..

simonapnic 07-07-2008 12:14 PM

I suggest you use sudo instead of su for the following reason:
-S The -S (stdin) option causes sudo to read the password from the
standard input instead of the terminal device.
(so you can echo it or something)
Also, expect is a wise solution as well.
I have found a few other people with the same problem, check them out:
http://www.dbforums.com/archive/index.php/t-319516.html
Here's an expect guide:
http://www.unix.com/shell-programmin...ll-script.html

av.dubey 07-09-2008 12:49 AM

can u give me one example of except ..plz..i searched but m not able to understand it properly..
can ne one give me a small example of except..

chrism01 07-09-2008 01:13 AM

Why do you want to wrap a shell script around

su -

??
If you are willing to give the user the root passwd there doesn't seem to be any point...

If you really want to you could do something like:

sudo su - -c "somemenu"

Mr. C. 07-09-2008 01:18 AM

sudo su is superfluous. sudo or su alone is sufficient.

av.dubey 07-11-2008 10:06 PM

actually im trying to make an application using cgi which will provide users a web interface software type of thing that will help them to do all the root activities using graphical interface...
but leave that right now..

the thing is that if iam able to login as root using shellscript ...my work will be done...so plzz don ask me to change my mind regarding that..

can neone please give me a simple example of using execute or ne other thing like that using which i can get as superuser using shell script..


All times are GMT -5. The time now is 08:18 AM.