TCL,预计:多个文件与SCP

TCL,预计:多个文件与SCP,第1张

概述TCL,预计:多个文件与SCP

我能够传输文件与SCP和期望,现在我试图一次上传几个文件:

#!/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 "nnnUpload 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 "nnnUpload Failed!nPlease check the errors above and start over again.nYou entered most likely a wrong password!" } -re ".*Are.*.*yes.*no.*" { send "yesn" exp_continue #look for the password prompt } -re ".*sword.*" { # Send password aka $PW send -- "$PWr" # send blank line (r) to make sure we get back to gui send -- "rn" 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" "~/test/" 。 这也产生这个输出:

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""等等,但没有任何工作。

从c中的资源解压缩多个文件

创build一个可以处理拖放多个文件的batch file

使用vim读取新行的linux cat命令输出

任何想法或build议?

EDITS

PS:我正在传输相当小的文件。 build立连接是转移的最大部分。 这就是我希望在一个SCP中完成的原因。

PPS:我玩了一下,想出了:

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

与您的第一个解决scheme,但{*}[split $files ","]和

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

用你的第二个解决scheme 这打印:

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

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

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

感谢所有的答案,在这里我的解决scheme :

运用 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" } return "$files[makeabsolute $f]" } #Counts all files from an upload-List proc countUploadfiles {s} { set rc [llength [split $s ""]] 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 ""] $ACCOUNT@$SERVER:[esc $newfolder] match_max 100000 # Look for password prompt expect { -re ".*Connection closed.*" { sendError "nnnUpload 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 "nnnUpload Failed!nPlease check the errors above and start over again.nYou entered most likely a wrong password!" } -re ".*Are.*.*yes.*no.*" { send "yesn" exp_continue #look for the password prompt } -re ".*sword.*" { # Send password aka $PW send -- "$PWr" # send blank line (r) to make sure we get back to gui send -- "rn" exp_continue } send_user -- "Upload successful!n" } set timeout -1 } set fls [addUploadfile "" "ab/testfileA"] set fls [addUploadfile $fls "ab/testfileB"] set fls [addUploadfile $fls "ab/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

你可以产生一个shell,然后运行scp命令:

spawn bash send "scp $files $ACCOUNT@$SERVER:[esc $newfolder]r"

这允许glob扩展,但增加了额外的内务管理,因为在scp进程完成时您将需要进行陷阱,因为您仍然在运行一个shell。 你可以添加到你的期望块:

-re "100%" { if { $index < $count } { set index [expr $index + 1] exp_continue } }

其中index是正在传输的文件的数量,并计算文件的nr。

你应该使用SSH公钥认证,而不是用expect输入密码。 当设置正确时, scp将在没有任何人为输入密码的情况下工作,同时保持系统非常安全。 你将从所有的expect解脱出来。

我如何设置公钥认证?

http://www.ece.uci.edu/~chou/ssh-key.HTML

如果您为什么不能使用pubkey有一些原因,那么您可能会觉得sftp有用,因为它接受一个批处理命令文件作为-b batchfile批处理文件。 见man 1 sftp expect实际上可以分割参数的时候不是一个很好的解决方案

总结

以上是内存溢出为你收集整理的TCL,预计:多个文件与SCP全部内容,希望文章能够帮你解决TCL,预计:多个文件与SCP所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1289459.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存