Operating System - Linux
1828293 Members
2746 Online
109975 Solutions
New Discussion

su command with expect script

 
Rohit Khaladkar
Occasional Contributor

su command with expect script

Hi All!
I need to automate the su command using expect script.Suppose I login as a user A , I need to change the user to oracle using the su oracle command and execute the command "sqlplus -ver".

I tried writing a expect script for the same , but can't figure out the reason it is not working.

Can someone please help. Below is the expect script that I wrote:

#!/usr/bin/expect -f
set timeout -1
spawn /bin/su oracle -c date
expect "Password: "
send "oracle\r"
expect "\r\n"
send "sqlplus -ver\n"
3 REPLIES 3
Ivan Ferreira
Honored Contributor

Re: su command with expect script

Probably you don't need expect for this. You only need SUDO. Give SUDO a try.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Rohit Khaladkar
Occasional Contributor

Re: su command with expect script

Yes I thought about the sudo option. But that means I would have to make an entry in the /etc/sudoers file.Since the application I am developing has a constraint that no changes should be made to the file system on the server, I cannot use the sudo option.

Please let me know if you need anymore information on this.

Thanks!
Rohit Khaladkar
Matti_Kurkela
Honored Contributor

Re: su command with expect script

The su command in your expect script is "/bin/su oracle -c date", which means only the command "date" is executed as user "oracle". After that, the su session will not accept further commands: the su session will simply end.

You should be doing something like:

#!/usr/bin/expect -f
set timeout -1
spawn /bin/su oracle -c "sqlplus -ver"
expect "Password: "
send "oracle\r"
expect "\r\n"
# add here suitable expect lines to catch the entire sqlplus -ver output

MK
MK