如何使用 expect 发送目录中包含的多个文件?
How to send several files contained in a directory using expect?
你好我正在写下面的脚本来使用ftp协议自动发送几个文件,我决定用expect来实现这个,我试过了,如下:
#!/usr/bin/expect
定义变量:
set user myUser
set gate myGate
set password myPassword
定义每 3 分钟更改一次的令牌:
set token [lindex $argv 0]
这是要发送的文件名的第二个参数:
set file [lindex $argv 1]
set server myServer
set password2 myPassword2
spawn ftp ${gate}
expect "some lines of response:"
send "${user}\r"
expect "password:"
send "${password}${token}\r"
expect "ftp>"
send "user myServer\r"
expect "Password:"
send "myPassword2"
将我转到适当的目录:
send "pwd\r"
send "cd myFolder\r"
expect "successfully changed."
这里是出现问题的地方:
send "put ${file}\r"
interact
我运行它如下:
expect.exe ftpScript myToken filesToSend/
一切正常,直到它尝试发送包含文件的目录的部分:
ftp> myFolder/
250 Directory successfully changed.
ftp> put filesToSend/
filesToSend/: not a plain file.
ftp>
我的文件夹位于存储我的脚本的同一层:
ls:
filesToSend ftpScript
如果我对名为 filesToSend 的目录执行 ls,它看起来如下:
$ ls
file1 file10 file11 file12 file2 file3 file4 file5 file6 file7 file8 file9
因此,自最近我开始学习 expect 以来,我想感谢实现此目的的任何想法以及如何改进我的代码,在此先感谢您的帮助。
回答后,我将脚本复制到要发送的文件所在的同一文件夹中,然后尝试:
send "mput ${file}\r"
但在 运行 之后:
expect.exe ftpScript token file10 file11 file12
测试的那三个文件,我只成功发送了file10,确认后:
mput file10? y
200 PORT command successful [translated to PASV by DeleGate].
150 Ok to send data.
226 File receive OK.
我相信这只是发送一个文件,因为我使用获取参数的方式我只有这一行来获取参数:
set file [lindex $argv 1]
我不太确定我是否需要在这里使用一个列表,这是导致只发送一个文件的原因,正在考虑argv1这个脚本,谢谢支持,我想收到建议修复它。
我相信您可以通过使用带有通配符的命令 mput
而不是 put
来解决您的问题。
编辑您的 expect
脚本以通过以下方式更改 put
行:
send "mput ${file}\r"
然后调用:
expect ftpScript myToken filesToSend/*
我解决问题如下:
如果我们执行:
cd filesToSend
然后我们修改脚本如下:
spawn ftp -i ${gate}
-i 标志关闭提示确认每个文件的交互模式。
然后我们使用:
send "mput *\r"
终于可以把所有的文件无误的发送给命运了,运行只是:
expect.exe /myPath/ftpScript myToken
我们收到了正确命运的所有文件:
file1 file10 file11 file12 file2 file3 file4 file5 file6 file7 file8 file9
感谢所有有用的评论,经过研究我决定 post 我的答案,因为也许有人发现这个脚本有助于让他们的生活更轻松,谈论使用 ftp 发送多个文件,
你好我正在写下面的脚本来使用ftp协议自动发送几个文件,我决定用expect来实现这个,我试过了,如下:
#!/usr/bin/expect
定义变量:
set user myUser
set gate myGate
set password myPassword
定义每 3 分钟更改一次的令牌:
set token [lindex $argv 0]
这是要发送的文件名的第二个参数:
set file [lindex $argv 1]
set server myServer
set password2 myPassword2
spawn ftp ${gate}
expect "some lines of response:"
send "${user}\r"
expect "password:"
send "${password}${token}\r"
expect "ftp>"
send "user myServer\r"
expect "Password:"
send "myPassword2"
将我转到适当的目录:
send "pwd\r"
send "cd myFolder\r"
expect "successfully changed."
这里是出现问题的地方:
send "put ${file}\r"
interact
我运行它如下:
expect.exe ftpScript myToken filesToSend/
一切正常,直到它尝试发送包含文件的目录的部分:
ftp> myFolder/
250 Directory successfully changed.
ftp> put filesToSend/
filesToSend/: not a plain file.
ftp>
我的文件夹位于存储我的脚本的同一层:
ls:
filesToSend ftpScript
如果我对名为 filesToSend 的目录执行 ls,它看起来如下:
$ ls
file1 file10 file11 file12 file2 file3 file4 file5 file6 file7 file8 file9
因此,自最近我开始学习 expect 以来,我想感谢实现此目的的任何想法以及如何改进我的代码,在此先感谢您的帮助。
回答后,我将脚本复制到要发送的文件所在的同一文件夹中,然后尝试:
send "mput ${file}\r"
但在 运行 之后:
expect.exe ftpScript token file10 file11 file12
测试的那三个文件,我只成功发送了file10,确认后:
mput file10? y
200 PORT command successful [translated to PASV by DeleGate].
150 Ok to send data.
226 File receive OK.
我相信这只是发送一个文件,因为我使用获取参数的方式我只有这一行来获取参数:
set file [lindex $argv 1]
我不太确定我是否需要在这里使用一个列表,这是导致只发送一个文件的原因,正在考虑argv1这个脚本,谢谢支持,我想收到建议修复它。
我相信您可以通过使用带有通配符的命令 mput
而不是 put
来解决您的问题。
编辑您的 expect
脚本以通过以下方式更改 put
行:
send "mput ${file}\r"
然后调用:
expect ftpScript myToken filesToSend/*
我解决问题如下:
如果我们执行:
cd filesToSend
然后我们修改脚本如下:
spawn ftp -i ${gate}
-i 标志关闭提示确认每个文件的交互模式。
然后我们使用:
send "mput *\r"
终于可以把所有的文件无误的发送给命运了,运行只是:
expect.exe /myPath/ftpScript myToken
我们收到了正确命运的所有文件:
file1 file10 file11 file12 file2 file3 file4 file5 file6 file7 file8 file9
感谢所有有用的评论,经过研究我决定 post 我的答案,因为也许有人发现这个脚本有助于让他们的生活更轻松,谈论使用 ftp 发送多个文件,