LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2008, 03:18 AM   #1
av.dubey
Member
 
Registered: Nov 2007
Posts: 148

Rep: Reputation: 15
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..
 
Old 07-06-2008, 03:56 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by av.dubey View Post
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.
 
Old 07-06-2008, 04:25 AM   #3
pradeep.sree
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Rep: Reputation: 0
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
 
Old 07-06-2008, 04:37 AM   #4
dom83
LQ Newbie
 
Registered: Jan 2006
Posts: 20

Rep: Reputation: 1
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.

Last edited by dom83; 07-06-2008 at 04:38 AM.
 
Old 07-06-2008, 04:57 AM   #5
av.dubey
Member
 
Registered: Nov 2007
Posts: 148

Original Poster
Rep: Reputation: 15
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...
 
Old 07-06-2008, 05:21 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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
?
 
Old 07-07-2008, 10:41 AM   #7
av.dubey
Member
 
Registered: Nov 2007
Posts: 148

Original Poster
Rep: Reputation: 15
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..
 
Old 07-07-2008, 12:14 PM   #8
simonapnic
Member
 
Registered: Jul 2008
Posts: 70

Rep: Reputation: 16
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
 
Old 07-09-2008, 12:49 AM   #9
av.dubey
Member
 
Registered: Nov 2007
Posts: 148

Original Poster
Rep: Reputation: 15
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..
 
Old 07-09-2008, 01:13 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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"
 
Old 07-09-2008, 01:18 AM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
sudo su is superfluous. sudo or su alone is sufficient.
 
Old 07-11-2008, 10:06 PM   #12
av.dubey
Member
 
Registered: Nov 2007
Posts: 148

Original Poster
Rep: Reputation: 15
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..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to switch user via shell script cool_anupam Linux - General 7 05-29-2008 10:01 PM
How to program shell script to automate mass user account creation? EsAsher Linux - General 2 06-30-2007 08:41 AM
How to switch user in shell scripting.. Parished.D Linux - Software 2 10-31-2006 07:30 AM
Pass SCP User passward in shell program michaelyu33 Programming 3 03-18-2005 12:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration