Shell Script to Automatically Read My Password

I have a shell script to run set of commands every week . I dont have a root access on the server but I can run the commands using pbrun

 
cat myscript.sh
*
*
*
pbrun command....

each time I run the script , it asks me for my password then it executes fine.

 
./myscript.sh
Password for ....

is there a way to automate the script to read my password from a file or something without me interacting with the script each time I run it.

Sincerely, Sara

[noon@node2 ~]$ cat run.exp
#!/usr/bin/expect -f

if { $argc<1 } {
        send_user "usage: $argv0 <passwdfile> \n"
        exit 1 
}
set timeout 20
set passwdfile [ open [lindex $argv 0] ]
catch {spawn -noecho ./myscript.sh}
expect "Password:" {
	while {[gets $passwdfile passwd] >= 0} {
		send "$passwd\r"
		}
}
expect "*]$\ " {send "exit\r"}
close $passwdfile
expect eof
[noon@node2 ~]$ chmod +x run.exp
[noon@node2 ~]$ ./run.exp your_password_file

Thanks, but the command "expect" is not defined in my OS
I'm using AIX 6.1

I will be Grateful and Thankful if someone can help me with this :slight_smile:

$
$
$ # contents of myscript.sh
$ cat -n myscript.sh
     1  #!/usr/bin/bash
     2  echo "Printing from myscript.sh"
     3  echo -n "Enter password: "
     4  read passwd
     5  echo
     6  echo "Your password is: $passwd"
     7  echo "End of myscript.sh"
$
$ # run myscript.sh and feed password when prompted
$ ./myscript.sh
Printing from myscript.sh
Enter password: hello_world

Your password is: hello_world
End of myscript.sh
$
$ # now put the password in a file, say "mypasswd.txt"
$ cat -n mypasswd.txt
     1  hello_world
$
$ # and call myscript.sh, redirecting the password from mypasswd.txt
$ ./myscript.sh < mypasswd.txt
Printing from myscript.sh
Enter password:
Your password is: hello_world
End of myscript.sh
$
$

tyler_durden

Im running my command using "pbrun", so the password is required by system .. I want to automate providing my password to the system each time it asks me

when I run:

 
$ pbrun commands

the system will ask me to provide my pbrun password

 
Password for sara@mydomain

Right, but does it work if you put your password in a file and redirect from it? If not, then explain how you set it up, how you invoked your script, and what exactly you see when the script is run.

tyler_durden

I used your code but , the system still asks me for my password

Perhaps a "here document" will work but then your password is visible in the file which could be a security risk:

pbrun command <<EOF
password
EOF
.
.
.