Operating System - HP-UX
1829160 Members
16711 Online
109986 Solutions
New Discussion

How to login with other userid from a script ...

 
SOLVED
Go to solution
Manuales
Super Advisor

How to login with other userid from a script ...

Hi ..
I need to change some things from a script but using other userid, how do i have to program that? i mean, how do i have to use "su -" command to login with the other user and after exit that user?

thanks in advance.
Thanks and Regards :0)
16 REPLIES 16
Pete Randall
Outstanding Contributor
Solution

Re: How to login with other userid from a script ...

Unless the script runs as root, you will have to supply a password for the su command. That means you will probably need to use Expect:

http://hpux.its.tudelft.nl/hppd/hpux/Tcl/expect-5.43/


Pete

Pete
OldSchool
Honored Contributor

Re: How to login with other userid from a script ...

look at the su command options

su - -c

where command is the script you need to run to "change some things"

as noted, unless you are root when the above is issued, you'll get prompted for the password so you may need expect
Manuales
Super Advisor

Re: How to login with other userid from a script ...

Nop, i do not need that .. thanks any way.
i need to know what is the sintax of the command "su" to use it into a script using ksh .....

su - user1 - pass??

please let me know.
Thanks.
Manuales
Super Advisor

Re: How to login with other userid from a script ...

ok Oldschool , you read my brain ...
that is i want.. but ..
my userid is not root, then, where do i have put the password??? that has to be in automatical way .... with out put the password manually
please let me know ...

thanks.
Ralph Grothe
Honored Contributor

Re: How to login with other userid from a script ...

If it is a command to be executed on localhost
an appropriate sudo rule could do.
If it is a command run from a remote host
creating an ssh RSA key on executing host and appending it to the user's authorized_keys file under which the script should run could be an option.
You can limit the use of the key by prepending a cmd="/path/to/script" option stanza to the key entry in authorized_keys.
Then setting an empty passphrase on ssh-keygen generation is half-way secure (otherwise with passphrase make use of ssh-agent).
Madness, thy name is system administration
Pete Randall
Outstanding Contributor

Re: How to login with other userid from a script ...

That's just it - there is no option to the su command that allows you to pass the password to it. That is way you need to use expect to handle the interactive dialog that su uses to obtain the password.


Pete

Pete
Denver Osborn
Honored Contributor

Re: How to login with other userid from a script ...

If you have no root accees and only have access to the user accounts, then your best option is to setup ssh key pair as Ralph suggested. The ssh setup would only take a min or two to setup. Other than that, I'd use sudo to avoid passing your passwd in a script.

-denver
James R. Ferguson
Acclaimed Contributor

Re: How to login with other userid from a script ...

Hi Manuales:

> my userid is not root, then, where do i have put the password??? that has to be in automatical way .... with out put the password manually

Your script will prompt you interactively for the password when 'su' executes. You cannot place the password in a file and redirect it as STDIN to 'su' either. As noted, 'expect' is an option.

Regards!

...JRF...
OldSchool
Honored Contributor

Re: How to login with other userid from a script ...

yep..what Pete said.....

there isn't any

"su - user1 - pass??"

"expect" scripting can handle this, as it can be programmed to react to prompts on the screen
Manuales
Super Advisor

Re: How to login with other userid from a script ...

PROBLEM SOLVED, I did the following:

user to be used: "special"
my user: myuser

1.- i access to "special" user id home and i created the file .rhosts and this user gave permissions to "muyuser" id to access as follows:

serverdns.company.com myuser

2.- i run the following:
/usr/bin/remsh ipaddress_serverdns -l myuser -n /home/scripts/test_command

AND WORKED !!!!

Thanks all for your help !!!

Have nice day!!
Manuales
Super Advisor

Re: How to login with other userid from a script ...

only one more thing ..
this script is run into the same serverdns ...

thanks.
Pete Randall
Outstanding Contributor

Re: How to login with other userid from a script ...

Yes, it worked, but that bears no resemblance to the su question you originally asked.

Well done workaround, though!


Pete

Pete
Manuales
Super Advisor

Re: How to login with other userid from a script ...

My apologies for my english, probably i did not say exactly what i wanted .....

thanks.
OldSchool
Honored Contributor

Re: How to login with other userid from a script ...

"2.- i run the following:
/usr/bin/remsh ipaddress_serverdns -l myuser -n /home/scripts/test_command"

and it ran as "myuser"....not "special" as you logged in as yourself (if the above is indeed correct
Manuales
Super Advisor

Re: How to login with other userid from a script ...

you are right, it was my error , it was run with "special" user ....
Dennis Handly
Acclaimed Contributor

Re: How to login with other userid from a script ...

>I need to change some things from a script but using other userid

Other than your remsh solution, you could just set it up to use cron, if you can schedule your execution.