簡體   English   中英

bash腳本中的scp命令無法正常工作

[英]scp command not working inside expect in bash script

while [ $FileLine -le $FileListLines ];
                do
                        # extract each line from FileList
                        str=$(tail -n+$FileLine ../$FileList | head -n1)
                        hostpath=$username@$ip:$str
                        export hostpath ip
                        expect -c '
                                spawn bash -c "scp -pr $env(hostpath) $env(ip)"
                                expect {
                                        "(yes/no)?"{
                                                send "yes\r"
                                                expect "*?assword:*"
                                                send "password\r"
                                                }
                                        "*?assword:*"{
                                                send "password\r"
                                                }
                                        }
                                '
                        FileLine=$(( $FileLine + 1 ))
                done

上面是bash腳本的一部分。 scp在命令expect塊不工作,也就是從遠程計算機的文件沒有得到復制到本地機器。

從終端運行時,具有pathhostname的相同scp命令可以正常工作。

在期望代碼的末尾添加expect eof ,否則在發送密碼后立即終止scp進程。 (盡管不確定是否有問題,也請在模式{{ expect {}中的expect {}之間)之間添加一個空格。)

expect -c '
    spawn bash -c "scp -pr $env(hostpath) $env(ip)"
    expect {
        "(yes/no)?" {
            send "yes\r"
            expect "*?assword:*"
            send "password\r"
        }
        "*?assword:*" {
            send "password\r"
        }
    }
    expect eof
'

更新

剛剛嘗試過, "(yes/no)?"{不起作用。 模式{之間的空格是必需的,因此應該為"(yes/no)?" { "(yes/no)?" {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM