Change user


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change user
# 1  
Old 04-22-2010
Change user

Hi,
I have to change many times user in a script.
With the command su userName I receive the request for password. I need to open a session with another user I would put the password at the beginning.
How can I do it?
Thanks, bye bye.

---------- Post updated 22-04-10 at 10:58 AM ---------- Previous update was 21-04-10 at 04:41 PM ----------

Hi,
I tried this command:

Code:
VAR=$(expect -c "
spawn su - $USER 
expect \"password:\"
send \"$PWD\r\"
expect \"\\\\$\"
send \"ls >pippo2204.txt\r\"
expect -re \"$USER.*\"
send \"logout\"
")

In this way I am able to change user and execute the ls command.
No I have a problem

I have to execute, changing user this code:


Code:
operation1

operation2



ssh $HOST<<END

operation3

END

Where operation are sqlplus session. I need to execute the code I past as userA while other code, after what I past need to be executed as userB.

How can I open a session for the code I past as userA and close the session after operations are completed?
Thanks, bye bye.
# 2  
Old 04-22-2010
sql loader?
# 3  
Old 04-22-2010
No, sqlplus.

I'm trying in this way:

Code:
ssh server04<<"END"

VAR=$(expect -c "
spawn su - $USER 
expect \"password:\"
send \"$PWD\r\"
expect \"\\\\$\"
send "sqlplus / as sysdba"
expect \">\"
send \"shutdown immediate\"
expect \"shutdown*\"
send \"quit\"
expect -re \"$USER.*\"
send \"logout\"
")

END

This is a piece of a larger script. I tried to insert the expect command in this way but it does not work. I don't know how I can do.

I receive this error:

Quote:
send: spawn id exp5 not open
while executing
"send sqlplus"
couldn't read file "/": illegal operation on a directory
How can I solve?
Thanks, bye bye.
# 4  
Old 04-22-2010
Try autoexpect sqlplus

---------- Post updated 04-23-10 at 09:18 AM ---------- Previous update was 04-22-10 at 06:12 PM ----------

Did you manage to do it?
# 5  
Old 04-23-2010
Where can I find an example?
Thanks, bye bye.

---------- Post updated at 09:43 AM ---------- Previous update was at 09:27 AM ----------

I tried this:

Code:
#!/usr/bin/expect

spawn su - user
expect "password:"
send "password\r"
expect "\\\\$"


sqlplus INFRA/INFRA@SINGLETDB as sysdba<<END
shutdown immediate
/
END


expect -re "user.*"
send "logout"

But It doesn't work.

Whay do I have to change?
Thanks, bye bye.
# 6  
Old 04-23-2010
You have to use autoexpect first. This will record what you do, but the catch is: do not make any mistakes! Then to finish....press control + D... and save the generated exp file.

For more info:
Mastering Unix Shell Scripting; Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus

And remember to post your solution for others Smilie
# 7  
Old 04-23-2010
Hi,
I registered everything with autoexpect.
The operation are:
1)I open an ssh session;
2)I change user;
3)I open sqlplus session;
4)I complete some sql operation and I close sqlplus session
5)I close ssh session;

Everything was registered and I have script.exp. When I try to execute the script I receive this:
Quote:
[gaia_user@server02 ~]$ ./script.exp
spawn /bin/bash
[gaia_user@server02 ~]$ ssh server04
Enter passphrase for key '/home/gaia_user/.ssh/id_rsa':
Last login: Fri Apr 23 10:34:56 2010 from server02.gaia_dpct
[gaia_user@server04 ~]$
It changes server but it doesn't complete the other operation.
In the exp file the operation are registered correctly, I think.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change user

Hi All, need your assistance, how can i change user again after i change my user. here is the code that i tried su - myuser #success su - webuser ##what i want to try is to change user again to webuser from myuser account my output is it cannot change to webuser account. only in... (8 Replies)
Discussion started by: meister29
8 Replies

2. AIX

How to change normal user id to LDAP user id?

If I create a new user id test: mkuser id=400 test then I want it to LDAP user: chuser -R LDAP SYSTEM=LDAP registry=LDAP test It shows: 3004-687 User "test" does not exist. How to do? (4 Replies)
Discussion started by: rainbow_bean
4 Replies

3. Red Hat

Change user from SH TO CSH

Dear Experts, I'm currently working on a Red Hat Linux env. I have been working in AIX. My user is set to use SH default so when it logs in, we have to manually csh to do few things. I know you can change this in AIX using smitty tool. is there any similar too in LINUX that can change it? ... (7 Replies)
Discussion started by: Afi_Linux
7 Replies

4. Solaris

how to change /export/home/user dir to /home /user in solaris

Hi all i am using solaris 10, i am creating user with useradd -d/home/user -m -s /bin/sh user user is created with in the following path /export/home/user (auto mount) i need the user to be created like this (/home as default home directory ) useradd -d /home/user -m -s /bin/sh... (2 Replies)
Discussion started by: kalyankalyan
2 Replies

5. UNIX for Dummies Questions & Answers

change user> to user@host> ssh prompt

Hi, I was wondering how to change the prompt for my ssh login. At the moment it is like user> while I'd like it to be as user@host> It is in the .bash_profile or .ssh ??? Thanks (2 Replies)
Discussion started by: pmasterkim
2 Replies

6. Shell Programming and Scripting

How do i change to super user then revert back to ordinary user ,using shell script?

Hi all, I am trying to eject the cdrom from a livecd after certain stage... Now assuming that it is possible to eject,please consider my issue!!! The OS boots into a regular user by default...so i am unable to use the eject command to push out the drive... However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies

7. Forum Support Area for Unregistered Users & Account Problems

Change of user name

I am on several other Forums under the handle of FloridaBD and therefore would like to request that my user name here on Unix forums be changed from SunBurntYux to FloridaBSD Thanks. (1 Reply)
Discussion started by: SunBurntYux
1 Replies

8. Post Here to Contact Site Administrators and Moderators

User name change?

Hello, I love the service. My username is Jmt. Can I change that to WebKruncher? Thanks, -Jmt WebKruncher.com - sole proprietor. (1 Reply)
Discussion started by: Jmt
1 Replies
Login or Register to Ask a Question