Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-22-2012
Registered User
 

Join Date: Feb 2012
Posts: 32
Thanks: 23
Thanked 0 Times in 0 Posts
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

Code:
 
cat myscript.sh
*
*
*
pbrun command....

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

Code:
 
./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
Sponsored Links
    #2  
Old 02-22-2012
Registered User
 

Join Date: Nov 2009
Location: BeiJing China
Posts: 146
Thanks: 12
Thanked 46 Times in 31 Posts

Code:
[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

Sponsored Links
    #3  
Old 02-26-2012
Registered User
 

Join Date: Feb 2012
Posts: 32
Thanks: 23
Thanked 0 Times in 0 Posts
"expect" is not defined

Quote:
Originally Posted by huaihaizi3 View Post
Code:
[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
    #4  
Old 02-26-2012
durden_tyler's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 1,684
Thanks: 4
Thanked 209 Times in 189 Posts
Quote:
Originally Posted by Sara_84 View Post
...
each time I run the script , it asks me for my password then it executes fine.

Code:
 
./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.
...

Code:
$
$
$ # 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
Sponsored Links
    #5  
Old 02-26-2012
Registered User
 

Join Date: Feb 2012
Posts: 32
Thanks: 23
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by durden_tyler View Post
Code:
$
$
$ # contents 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:


Code:
 
$ pbrun commands

the system will ask me to provide my pbrun password


Code:
 
Password for sara@mydomain

Sponsored Links
    #6  
Old 02-26-2012
durden_tyler's Avatar
Registered User
 

Join Date: Apr 2009
Posts: 1,684
Thanks: 4
Thanked 209 Times in 189 Posts
Quote:
Originally Posted by Sara_84 View Post
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:


Code:
 
$ pbrun commands

the system will ask me to provide my pbrun password


Code:
 
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
Sponsored Links
    #7  
Old 02-27-2012
Registered User
 

Join Date: Feb 2012
Posts: 32
Thanks: 23
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by durden_tyler View Post
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
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to automatically enter a password Ernst Shell Programming and Scripting 0 01-18-2011 10:03 AM
sftp shell script - Password read error. vino_hymi Shell Programming and Scripting 0 10-25-2010 02:50 AM
How to enter a password in the script automatically when prompted? sourabhsharma Shell Programming and Scripting 1 07-08-2009 01:36 PM
Script to automatically change password. rahulrathod Shell Programming and Scripting 1 10-04-2004 08:33 AM
How would I telnet & change user password automatically in a script darthur UNIX for Dummies Questions & Answers 4 01-14-2002 10:40 AM



All times are GMT -4. The time now is 04:55 AM.