Im finding trouble writing a script that runs from account A, switches user to B (on same server), performs a few jobs at B, and switches back to A. I want the password for B hardcoded in the script so that it doesnt prompt for one. Someone please help.

Recommended Answers

All 3 Replies

Hardcoded passwords are a security risk - FWIW.

if you are root you can use sudo or su by default. Otherwise use a here document:

su username <<!
password
/bin/ksh
command 
command 
command
exit
!

su may also give you the wrong shell, which is why I put "/bin/ksh" in there.

Thanks, Jim. But it still prompts for a password just after "su username" command. Any way we could hardcode this? And none of the accounts is a superuser. Anyone?

I'm going to have to agree with Jim on this one. You really should look into setting up sudo for something like this.

If you know the user that's going to run it, and you know the username ahead of time, then you can set up the appropriate permissions in /etc/sudoers, and not have to worry about a password at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.