Les Ostness
asked on
How can I get an expect script to read a password from a file so it can be used to pass it to a program that requires that password.
How can I get an expect script to read a password from a file so it can be used to pass it to a program that requires that password.
I currently used autoexpect to create expect scripts, but the passwords are then hard coded.
Note: My OS is Linux
here is a snippet from my expect script
send -s -- "cd \$ADMIN_SCRIPTS_HOME"
expect -exact "cd \$ADMIN_SCRIPTS_HOME"
sleep .1
send -s -- "\r"
expect -exact "applmgr@appsrvr:/u01/blah /SID/R122/ fs1/inst/a pps/SID_ho stname/adm in/scripts > "
sleep .1
send -s -- "adop phase=fs_clone"
expect -exact "adop phase=fs_clone"
sleep .1
send -s -- "\r"
expect -exact "Enter the APPS password:"
sleep .1
send -s -- "hard_coded_password\r"
I want to be able to read the APPS password from a file and use it in this expect script.
I currently used autoexpect to create expect scripts, but the passwords are then hard coded.
Note: My OS is Linux
here is a snippet from my expect script
send -s -- "cd \$ADMIN_SCRIPTS_HOME"
expect -exact "cd \$ADMIN_SCRIPTS_HOME"
sleep .1
send -s -- "\r"
expect -exact "applmgr@appsrvr:/u01/blah
sleep .1
send -s -- "adop phase=fs_clone"
expect -exact "adop phase=fs_clone"
sleep .1
send -s -- "\r"
expect -exact "Enter the APPS password:"
sleep .1
send -s -- "hard_coded_password\r"
I want to be able to read the APPS password from a file and use it in this expect script.
ASKER
noci, Good point. I have updated the question.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
noci,
I like this a lot. I will test it. Thank you.
I like this a lot. I will test it. Thank you.
Expect is bundled with tcl
You can use the regular open and read from tcl. I have a snippet available if needed. Unfortunately not on my mobile phone ;) but that should be trivial enough once you know you can use tcl
You can use the regular open and read from tcl. I have a snippet available if needed. Unfortunately not on my mobile phone ;) but that should be trivial enough once you know you can use tcl
From the top of my head
set fp [ open pathtofile r]
set password [ read fp ]
Obviously replace pathtofile adequately
set fp [ open pathtofile r]
set password [ read fp ]
Obviously replace pathtofile adequately
ASKER
I like both your solutions. I tested noci's and implemented that solution already. I'm sure skullnobrains solution would also work.
Thank you both.
Thank you both.
good to see you got it running
just in case you're interested, here is a snipet from an ugly but working piece of code
you can remove the '\\\' which are escapes for the shell
this also demonstrates ways to mix shell and expect scripts so shell commands can be interleaved directly : "set password [ $(cat PASSWORDFILE) ]" would work in the below case and similar stuff would work in other cases depending on the quoting in use. i prefer reading the file from within expect which reduces the password exposure.
also note that many command line tools can read passwords from files directly using command line parameters
just in case you're interested, here is a snipet from an ugly but working piece of code
you can remove the '\\\' which are escapes for the shell
this also demonstrates ways to mix shell and expect scripts so shell commands can be interleaved directly : "set password [ $(cat PASSWORDFILE) ]" would work in the below case and similar stuff would work in other cases depending on the quoting in use. i prefer reading the file from within expect which reduces the password exposure.
also note that many command line tools can read passwords from files directly using command line parameters
expect $DBG -c "`cat <<EOF
set timeout 30
log_user $LOG
#log_file -noappend /dev/stderr
# /!\ exit in the function
proc readfile { file } {
if [catch {set fp [open \\\$file r] } errmsg] {
send_error "failed to open file='\\\$file' : \\\$errmsg\n"
exit 1
}
set ret ""
while { [gets \\\$fp data] >= 0 } { set ret "\\\$ret\\\$data" }
close \\\$fp
return \\\$ret
}
set p [ string trim [ readfile "$passwordfile" ] ]
set pen [ string trim [ readfile "$enpasswordfile" ] ]
...
The OS is lightly relevant here, more important it establishes that you need a scripting language like perl, python, bash, ... and those are available.
So much more info is needed what program does need the password, how does it accept passwords for a start.
In some cases the program can support this or can accept the authentication info in different ways.
Due to lack of info the question is alike: "how do i do something".