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.. |
Quote:
Code:
su username -c "echo Hello world!" |
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 |
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 This solution does only work if you need the user switch at the very beginning of the script. |
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... |
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 Can you clarify better what you are trying to accomplish that is alread done with: su - username ? |
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.. |
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 |
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.. |
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" |
sudo su is superfluous. sudo or su alone is sufficient.
|
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. |