gpt4 book ai didi

linux - TCL,期待: Multiple files w/SCP

转载 作者:太空狗 更新时间:2023-10-29 12:04:04 25 4
gpt4 key购买 nike

我能够使用 scp 和 expect 传输文件,现在我尝试一次上传多个文件:

#!/usr/bin/expect -f
# Escapes spaces in a text
proc esc text {
return [regsub -all {\ } $text {\\&}]
}

# Uploads several files to a specified server
proc my_scp_multi {ACCOUNT SERVER PW files newfolder} {
set timeout 30
send_user -- "\n"
spawn scp $files $ACCOUNT@$SERVER:[esc $newfolder]
match_max 100000
# Look for password prompt
expect {
-re ".*Connection closed.*" {
sendError "\n\n\nUpload failed!\nPlease check the errors above and start over again.\nThis is most likely induced by too many wrong password-attempts and will last quite a time!"
}
-re ".*Permission denied.*" {
sendError "\n\n\nUpload failed!\nPlease check the errors above and start over again.\nYou entered most likely a wrong password!"
}
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
#look for the password prompt
}
-re ".*sword.*" {
# Send password aka $PW
send -- "$PW\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r\n"
exp_continue
}

send_user -- "Upload successful!\n"
}

set timeout -1
}

当我要上传多个文件时,sh命令是:scp $a $b $c user@server:$folder,所以我调用了 my_scp_multi "ACCOUNT""SERVER""PW""~/testfileA ~/testfileB ~/testfileC""~/测试/”。这也产生了这个输出:

spawn scp ~/testfileA ~/testfileB ~/testfileC user@server:~/test/
user@server's password:
~/testfileA ~/testfileB ~/testfileC: No such file or directory

似乎将“~/testfileA ~/testfileB ~/testfileC”视为一个文件。但是当我将 scp ~/testfileA ~/testfileB ~/testfileC user@server:~/test/ 复制粘贴到控制台时,它工作正常!

我做错了什么?我试过 "\"~/testfileA\"\"~/testfileB\"\"~/testfileC\"" 之类的东西,但根本没有任何效果。

有什么想法或建议吗?


编辑

P.S.:我正在传输相当小的文件。建立联系是转移的最大部分。这就是我希望它在一个 scp 中完成的原因。

附注:我玩了一下,想出了:

my_scp_multi3 "user" "server" "pw" "~/a\ b/testfileA, ~/a\\ b/testfileB, ~/a\\\ b/testfileC" "~/test"

使用您的第一个解决方案,但{*}[split $files ","]

my_scp_multi2 "user" "server" "pw" "~/a b/testfileA" "~/a\ b/testfileB" "~/a\\ b/testfileC" "~/test"

第二个解决方案。这打印:

~/a b/testfileA: No such file or directory
~/a\ b/testfileB: No such file or directory
~/a\ b/testfileC: No such file or directory

~/a b/testfileA: No such file or directory
~/a b/testfileB: No such file or directory
~/a\ b/testfileC: No such file or directory

(顺便说一句:我当然移动了文件 :))


感谢所有的回答,这里是我的解决方案:

使用 \n\0(空字节)作为分隔符,因为它是除/和\之外唯一不能在文件名中使用的符号。

#!/usr/bin/expect -f

# Escapes spaces in a text
proc esc text {
return [regsub -all {\ } $text {\\&}]
}

# Returns the absolute Filepath
proc makeAbsolute {pathname} {
file join [pwd] $pathname
}

proc addUploadFile {files f} {
if {$files != ""} {
set files "$files\0"
}
return "$files[makeAbsolute $f]"
}

#Counts all files from an upload-list
proc countUploadFiles {s} {
set rc [llength [split $s "\0"]]
incr rc -1
return $rc
}

# Uploads several files from a list (created by addUploadFile) to a specified server
proc my_scp_multi {ACCOUNT SERVER PW files newfolder} {
foreground blue
set nFiles [countUploadFiles $files]
set timeout [expr $nFiles * 60]
send_user -- "\n"
spawn scp -r {*}[split $files "\0"] $ACCOUNT@$SERVER:[esc $newfolder]
match_max 100000
# Look for password prompt
expect {
-re ".*Connection closed.*" {
sendError "\n\n\nUpload failed!\nPlease check the errors above and start over again.\nThis is most likely induced by too many wrong password-attempts and will last quite a time!"
}
-re ".*Permission denied.*" {
sendError "\n\n\nUpload failed!\nPlease check the errors above and start over again.\nYou entered most likely a wrong password!"
}
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
#look for the password prompt
}
-re ".*sword.*" {
# Send password aka $PW
send -- "$PW\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r\n"
exp_continue
}

send_user -- "Upload successful!\n"
}

set timeout -1
}



set fls [addUploadFile "" "a b/testfileA"]
set fls [addUploadFile $fls "a b/testfileB"]
set fls [addUploadFile $fls "a b/testfileC"]

my_scp_multi "user" "server" "pw" $fls "~/test"

最佳答案

您不想将文件名作为单个字符串发送。要么这样做:

spawn scp {*}[split $files] $ACCOUNT@$SERVER:[esc $newfolder]

并继续引用文件名:

my_scp_multi "ACCOUNT" "SERVER" "PW" "~/testfileA ~/testfileB ~/testfileC" "~/test/"

这样做:

proc my_scp_multi {ACCOUNT SERVER PW args} {
set timeout 30
send_user -- "\n"
set files [lrange $args 0 end-1]
set newfolder [lindex $args end]
spawn scp {*}$files $ACCOUNT@$SERVER:[esc $newfolder]

然后引用文件名

my_scp_multi "ACCOUNT" "SERVER" "PW" ~/testfileA ~/testfileB ~/testfileC "~/test/"

splat ({*}) 将列表拆分为各个元素,因此 spawn 命令会看到多个单词,而不是单个单词。参见 http://tcl.tk/man/tcl8.5/TclCmd/Tcl.htm

关于linux - TCL,期待: Multiple files w/SCP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21407177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com