On Wed, 24 Sep 2003 14:53:17 -0700, Dmitry wrote:
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