0

我试图使用expect shell 做一个sftp。但我无法获取可能发生的不同 sftp 错误代码。相反,我得到的返回值为 0

这是代码片段和操作

"${sftp_prompt}" {

send "${command}\r"
    expect "${sftp_prompt}"

    # Close the sftp session
    send "bye\r"

expect eof

set details [wait]

puts "sftp exit status=[lindex $details 3]"

   }

}

输出:

spawn sftp root@dsc-A

Connecting to dsc-A...

root@dsc-a's password:

sftp> get shiv.txt /var/log/nsn/pkiclient/Response_files/

Couldn't stat remote file: No such file or directory

File "/root/shiv.txt" not found.

sftp> bye

sftp exit status=0

SSH_ERROR_OK | Indicates successful completion of the operation

期望的操作:我期望退出状态为 10,对应于

sftp 错误 - SSH_ERROR_NO_SUCH_PATH | 文件路径不存在或无效。

任何人都可以帮忙吗?

4

1 回答 1

0

您的 sftp 会话正常退出,这就是您得到结果代码 0 的原因。您需要在期望块中捕获 sftp 错误。

set timeout 60
send "$command\r"
expect {
    timeout {
        puts "A timeout occured"
        exit 1 
    }

    #One of many errors 
    -re "Couldn't|Can't" {
        puts "Can not perform $command"
        exit 2 
    } 

    #Ok 
    "sftp>" {
        puts "$command completed successfully"
    }
}
于 2014-01-21T20:20:28.287 回答