问题
I am trying to use expect to bypass the password authentication process for a remote server I'm using, so I can download a series of files in bulk, each of which is stored in a different folder.
The reason for using expect is the remote files are stored in separate folders, and the server I'm accessing won't allow me ssh key access.
At present I am struggling to download a single file using expect, never mind numerous ones at once. Below is my simple script:
#!/bin/bash
#connect to the server using scp
expect <<EOF
spawn scp username@server:file.txt ./
expect “*word:*“
send “password\r”
EOF
I expect the file.txt to be downloaded into the directory I run the script in. However, when it runs I instead get the text output from my server asking for the password, followed by a pause of 4-5 seconds before it exits to command prompt.
Any help with this is greatly appreciated!
回答1:
- I see "fancy" quotes in your code. Expect will not understand those as quotes. Make sure your editor does not use "smart quotes".
After you send the password, you have to wait for the download to complete before you exit your script:
#!/bin/bash #connect to the server using scp expect <<EOF set timeout -1 spawn scp username@server:file.txt ./ expect "*word:*" send "password\r" expect eof EOF
来源:https://stackoverflow.com/questions/56170769/how-can-i-fix-my-expect-script-for-scp-download