How can I fix my expect script for scp download?

左心房为你撑大大i 提交于 2021-01-29 05:42:55

问题


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:


  1. I see "fancy" quotes in your code. Expect will not understand those as quotes. Make sure your editor does not use "smart quotes".
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!