Switching Unix user in unattended Perl script

D

Dmitry

Hi folks!

I need to find a way to switch to a different Unix user from within
the Perl script that is currently ran unattended. Script must switch
user in order to execute proper profile for different Data Base
Instance and set corresponding environmental variables correctly.
Script works fine for a single Data Base Instance. I need to be able
to run it consecutively for several others instances. This requires
login in as a different Instance Owner.

I don't have (and don't need to have!) root privileges to run the
script. How can I switch to a different user without having script
prompting me for entering password? I have all login credentials so I
just need to switch user by supplying username and password.

Any help would be greatly appreciated.

Thanks!

D.
 
J

James K

Hi folks!

I need to find a way to switch to a different Unix user from within
the Perl script that is currently ran unattended. Script must switch
user in order to execute proper profile for different Data Base
Instance and set corresponding environmental variables correctly.
Script works fine for a single Data Base Instance. I need to be able
to run it consecutively for several others instances. This requires
login in as a different Instance Owner.

I don't have (and don't need to have!) root privileges to run the
script. How can I switch to a different user without having script
prompting me for entering password? I have all login credentials so I
just need to switch user by supplying username and password.

I would be very surprised to discover that you can do this in perl
without running as root. Are you sure that this is the right solution? I
would look into using expect, if you have it installed. It typically
comes with another program called autoexpect which will generate a script
for you, which you should then trim down.

Here is my general purpose 'do something as someone else' script:
(If you've never ssh'ed to localhost, do it at least once before running
this in order to accept the key.)

An even better option is to set up the ssh keys so that you don't even
need to supply a password, or use expect. You can just shell out within
your master program to run:
`ssh -l $user localhost run-this-as-another-user.pl '$param1' '$param2'`

------------------------------------------------
#!/usr/bin/expect -f

set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set user [lindex $argv 0]
set pass [lindex $argv 1]
set arglist [lindex $argv 2]
for {set i 3} {$i < $argc} {incr i +1} {
set arglist "$arglist [lindex $argv $i]"
}

set timeout -1
spawn ssh -l $user localhost $arglist
#the reason I use ssh is so that I
#don't have to guess what the user's
# prompt looks like in order to know
#when the command completed

match_max 100000
expect "assword: "
send -- "$pass\r"
expect eof
 
D

Dale Atkin

Dmitry said:
I need to find a way to switch to a different Unix user from within
the Perl script that is currently ran unattended.

Could you set up ssh authorized keys for each user and do it that way?

Just a thought

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top