School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,404 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,629 people online right now. Registration is fast and FREE... Join Now!




UNIX prompt response without Expect script

 

UNIX prompt response without Expect script

gforcing

17 Jun, 2009 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 17 Jun, 2009
Posts: 4



Thanked: 1 times
My Contributions
Hello all,

I'm trying to run the following as a UNIX command:

CODE
su username && command


The catch is that it has to be run automatically, so when su prompts for a password, I need a way to fill the password in. I know what the password is, but I can't figure out how to redirect STDIN or STDOUT to give it to the prompt.

Some suggestions have been to write an Expect script, but that's not an option I have available. Any ideas on redirection, for example:

CODE
echo password | su username && command
doesn't work.

Also, I found that

CODE
su username 2>/dev/null
still displays a prompt.

Help!

This post has been edited by gforcing: 17 Jun, 2009 - 11:13 AM

User is offlineProfile CardPM
+Quote Post


no2pencil

RE: UNIX Prompt Response Without Expect Script

17 Jun, 2009 - 11:21 AM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,228



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
Side stepping the problem, could you setup the user to have su permissions, like in a Wheel group, & that way that user has root level permissions to su to any user.
User is online!Profile CardPM
+Quote Post

gforcing

RE: UNIX Prompt Response Without Expect Script

17 Jun, 2009 - 11:32 AM
Post #3

New D.I.C Head
*

Joined: 17 Jun, 2009
Posts: 4



Thanked: 1 times
My Contributions
QUOTE(no2pencil @ 17 Jun, 2009 - 11:21 AM) *

Side stepping the problem, could you setup the user to have su permissions, like in a Wheel group, & that way that user has root level permissions to su to any user.


Negative. I also can't setup password-less SSH, or I would have gone that route.
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: UNIX Prompt Response Without Expect Script

17 Jun, 2009 - 11:49 AM
Post #4

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,228



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
The example that you gave is for the command 'su'.

Are you trying to provide the password to su or ssh?
User is online!Profile CardPM
+Quote Post

gforcing

RE: UNIX Prompt Response Without Expect Script

17 Jun, 2009 - 12:00 PM
Post #5

New D.I.C Head
*

Joined: 17 Jun, 2009
Posts: 4



Thanked: 1 times
My Contributions
QUOTE(no2pencil @ 17 Jun, 2009 - 11:49 AM) *

The example that you gave is for the command 'su'.

Are you trying to provide the password to su or ssh?


In this case, I'm trying to provide the password to su. I had thought of using SSH as a workaround (because it is possible to set up SSH so that certain accounts don't require a password) but I'm unable to do so.

Here's a more detailed example:

CODE

bob@host$ su jim && whoami
Password: <---- password prompt
jim
bob@host$


What I want to do is this:

CODE

bob@host$ su jim && whoami
jim
bob@host$


with some kind of alteration to the actual command (maybe by redirecting STDIN or STDOUT) to pipe the password in so the prompt doesn't show and the command following && can execute automatically.
User is offlineProfile CardPM
+Quote Post

no2pencil

RE: UNIX Prompt Response Without Expect Script

17 Jun, 2009 - 12:07 PM
Post #6

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,228



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
From the shell, you can't redirect standard input from the prompt of su (or ssh for that matte, because it's mid execution, & the && is going to wait for the su to finish prior to executing.

If you su to root (or a root level user) you'll find that you can su without being prompted for the password. That can be setup with the wheel account, & that's why I threw that out as a suggestion. I'm sure there is a solution for this, other than using expect, but I'm sure it can't be done from the shell alone.


User is online!Profile CardPM
+Quote Post

martin_mooney27@hotmail.com

RE: UNIX Prompt Response Without Expect Script

30 Jul, 2009 - 01:42 AM
Post #7

New D.I.C Head
*

Joined: 30 Jul, 2009
Posts: 1

QUOTE(no2pencil @ 17 Jun, 2009 - 11:21 AM) *

Side stepping the problem, could you setup the user to have su permissions, like in a Wheel group, & that way that user has root level permissions to su to any user.



Only the super user can use su without a password. For security reasons the su command will not allow you specify the password via redirection. It must be typed.

Your best bet is write a program to do what you want and have the set user id bit set on it as well as having the owner as root. I have several bits of code for this but depending on how your environment is managed they may not work for you or may not be allowed.


SSH as suggested before is actually a very good idea.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:32PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month