su with expect...in a single command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting su with expect...in a single command?
# 1  
Old 04-13-2011
su with expect...in a single command?

Hello
I am trying su from one user to another in a script. Now the person who runs the script(user) cannot have the password for the user he is su -ing to (webuser) .

Here is my script which performs su . This is a hack job based on a few hours of web search.

Code:
#!/usr/local/bin/expect
set pssword [lrange $argv 0 0]
spawn su - webuser -c /home/webuser/testwebuser.sh
expect "Password:" { send "$pssword\r" }
expect "# " { send "q" }
exit

as you can see the script has to run by passing the webuser password as a parameter as such:
Code:
./expectscript.sh PASSWORD

I would be passing the password using plink in an exe file.

All this works fine, I was wondering if there is a way to write the entire script I wrote in a single command format so that I can use it in the plink statement itself thus eliminating the need for ./expectscript.sh which basically calls another script testwebuser.sh?

if testwebuser is performing just a few tasks, would it be possible to eliminate both scripts and directly su to a differnt user and execute commands ( any other language/tool/ solution are also welcome)

example of plink command is

Code:
plink -ssh ip_addr_of_server -l user -pw userpassword "command to be executed"

where command to be executed can be
Code:
/home/user/expectscript.sh PASSWORD

# 2  
Old 04-13-2011
the sudo utility is really the way to go here. Assume you want to start two background tasks "/usr/local/bin/service1 -start -b" and "/bin/service2 -Y" as user webadmin you could do it like this:

Code:
printf "Starting service one.."
sudo -u webadmin "/usr/local/bin/service1 -start -b"
echo
printf "Starting service two.."
sudo -u webadmin /bin/service2 -Y
echo

sudo has a configuration file (/etc/sudoers) where you would can allow testwebuser (or anyone in testgrp group for example) to run these two commands (and only these two) as user webadmin without prompting for a password:
Code:
Cmnd_Alias WEBSTART= /bin/service2, /usr/local/bin/service1 
 
testwebuser ALL=(webadmin) NOPASSWD: WEBSTART
%testgrp ALL=(webadmin) NOPASSWD: WEBSTART

# 3  
Old 04-13-2011
Hi Chubler_XL

My initial reaction was sudo as well. However the servers are managed by another datacenter and it is not possible for me to get them to add a sudo command for every time I think up a script.

The idea here is that other users performing testing in this environment can run certain scripts that I provide to them ( say a script to run jra recordings or restart a weblogic server) without knowing what they are running. I would keep providing them new scripts based on their requests. I do not have root access either, I only have access to webuser, while they log in as user.

Hence I need a flexible solution , which does not involve the datacenter getting involved.
# 4  
Old 04-14-2011
Sudo is pretty flexable, you could get the datacenter to setup an cmd_Alias pointing to a certain directory (that you control) and allow any scripts in that directory to be run by "user" as "webuser"

Code:
Cmnd_Alias WEBSCRIPTS= /home/webuser/asme/
 
user ALL=(webuser) NOPASSWD: WEBSCRIPTS

If you have to provide a new script just put it in your ~/asme directory and away you go, just make sure you have tight restrictions on access to this directory and it's all pretty safe.
# 5  
Old 04-14-2011
Thanks Chubler, will definitely give this a try. It was one of the things I was trying to look into, but couldnt figure out how to do it.
I will update the post, once I try it out
# 6  
Old 04-19-2011
Thanks Chubler, this seems to work pretty well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect command- need help

Hi All, I need help in making my script run perfectly as expected. This is a snippet from my script. I am trying to read the ";" from the below output using the expect command, but the command is not detecting it. Could you please help me correct the expect string? Snippet: ---- ---... (2 Replies)
Discussion started by: sonicj
2 Replies

2. Shell Programming and Scripting

Single command - unzip files from a tar command

I have a tar file that contains multiple .Z files. Hence I need to issue a tar command followed by a gzip command to fully extract the files. How do I do it in a single command? What I'm doing now is tar xvf a.tar (this will output 1.Z and 2.Z) gzip -d *.Z (to extract 1.Z and 2.Z) (9 Replies)
Discussion started by: ericlim
9 Replies

3. Shell Programming and Scripting

Expect command

Hi, I wanted to automate the scp command where i do not want to enter the password each time. So thought of using expect command. Script is executing without any issues but files are not copied to remote server. Can any one help me? Below is my shell script.. #!/bin/ksh ... (1 Reply)
Discussion started by: balasubramani04
1 Replies

4. Shell Programming and Scripting

Help with expect command

Hi, I am using following commands in my script to send a password to scp command. Everything is working fine except that I need a return code from the scp command. Do any one have an idea how to receive the exit code of scp command from expect. /usr/bin/expect << EOF spawn -noecho scp -pq... (0 Replies)
Discussion started by: pvamsikr
0 Replies

5. Shell Programming and Scripting

How to use expect command?

Hi, 10.152.165.217 and 10.152.165.218 are two servers that i use. I would like to access 218 from 217 using expect command. i'm not aware of how to use the command. Could anyone help me with it with a script snippet -> imagine abc be the user in 217 with passwd abc123 -> imagine xyz be... (1 Reply)
Discussion started by: kaushik_87
1 Replies

6. Linux

how to use expect command?

Hello, I am doing a project which asks me to enter password 4 times in a row. I need to enter the same password 4 times. My target is to enter password once and apply it 4 times. My friend told me to use "expect" command. Can you please give me a sample code to do so. sample command :- $ ssh... (1 Reply)
Discussion started by: nsharath
1 Replies

7. UNIX for Dummies Questions & Answers

How to run multiple command in single command?

Dear Unix Guru, I have several directories as below /home/user/ dir1 dir2 dir3 Each directory has different size. I want to print each directory size (Solaris command du -hs .) Can you please guide me how to achieve this? Thanks Bala (2 Replies)
Discussion started by: baluchen
2 Replies

8. HP-UX

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (1 Reply)
Discussion started by: bosskr
1 Replies

9. Shell Programming and Scripting

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (0 Replies)
Discussion started by: bosskr
0 Replies

10. Shell Programming and Scripting

Expect and auto expect command

I need to run a remote application(GUI) in a client.i.e on running a script in the client machine i should get the GUI application which is running in the server by providing password through the script.Will expect or autoexpect command suit for this scenario? could anyone help me by posting some... (0 Replies)
Discussion started by: arun_v
0 Replies
Login or Register to Ask a Question