LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2005, 05:51 AM   #1
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
expect - read passphrase from a file


hi,
I want to rsync files into a backup server periodically. I use rsync over ssh. I use ssh-keygen to generate public-private keypair (using a passphrase) and copy the public key to the destination machine.
Now when i do ssh, the rsync prompts for the passphrase. To supply it, i use the expect script. I am new to expect. I googled and found the following script.

<code>
#!/usr/bin/expect
set timeout 19900
spawn /bin/bash
expect -re "]# "
send "rsync -avz -e ssh user@host:/etc/passwd /tmp/\r"
expect -re "Enter passphrase"
sleep 2
send "abcdefgh\r"
expect -re "total size is"
send "exit\r"
</code>

The passphrase is "abcdefgh". This works perfectly fine. But my requirement is to get this passphrase from a file instead of hardcoding inside the expect script like the following.

<code>
expect -re "Enter passphrase"
sleep 2
send "`cat /tmp/passphrasefile`\r"
</code>

How can i do this.

Any help is appreciated. TIA.

 
Old 09-03-2010, 10:46 AM   #2
Eduardo Nunes
LQ Newbie
 
Registered: Aug 2010
Location: /root/SouthAmerica/Brazil/SaoPaulo/SP
Distribution: Slackware
Posts: 24

Rep: Reputation: 2
Post

Hi hk_linux,

As you I ran into the same problem and found out the answer checking the TCL/TK manual.

You have to open the file you want to `cat` with TCL commands (not shell one's) and place its contents on a variable. Then you might use this variable anytime at your script. At last, you may close the file descriptor.

For example:
Code:
set fp [open "/tmp/passphrasefile" r]
set data [read $fp]

expect "somestuff"
send -- "$data"

close $fp
Or you may use all macros in one line, like:

Code:
expect "somestuff"
send -- "[read [open "/tmp/passphrasefile" r]]"
Altought this thread is old this may help anyone that finds it, like me again!

Good luck,

Eduardo Nunes

Last edited by Eduardo Nunes; 09-03-2010 at 10:51 AM.
 
1 members found this post helpful.
Old 03-01-2018, 02:34 AM   #3
HadroLepton
LQ Newbie
 
Registered: Mar 2004
Location: germany
Posts: 15

Rep: Reputation: 0
(wow. it is 2018 and i found an answer outside of stackoverflow.com)

for the people coming here looking for how to read both username and password from file

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

set passfile [open "~/.secrets/logindata" r]
gets $passfile username
gets $passfile password
close $passfile

spawn somelogintool
expect "username:"
send "$username\r"
expect "password:"
send -- "$password\r"
expect eof

set exitstatus [lindex [wait] 3]
puts "exitstatus: $exitstatus"
exit $exitstatus
bonus: the last three lines is how to print the exit status of somelogintool

the file logindata should look like this

Code:
username
password
that is: two lines. first line containing username followed by newline. second line containing password followed by newline. anything else is possible but will make the reading code more complicated.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
where does openssl expect the config file rblampain Linux - Security 4 08-12-2005 09:11 AM
Expect script throws "open(slave pty): bad file number" error KNut Linux - Newbie 0 02-24-2004 12:24 PM
passphrase patilpravin Linux - Security 3 11-08-2003 05:14 PM
File Manager SU mode using expect Allen614 Programming 0 02-26-2003 01:29 AM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration