【问题标题】:BASH: Send directory files from server(non-sftp) to remote server that use sftpBASH:将目录文件从服务器(非 sftp)发送到使用 sftp 的远程服务器
【发布时间】:2014-06-06 06:32:16
【问题描述】:

我可以通过更改文件名来一个一个地发送文件,文件数量越来越多,所以我尝试使用for循环但它不起作用。

我需要将 FILE 变量中的所有文件发送到远程计算机 USER@$HOST:/Semi/KFILE-ENG/ELD。此外,远程机器启用了 sftp,而发送方仅使用 ftp。 这是我唯一拥有的。

HOST=ftpabc.abc.cn
PORT=22
USER=user
PASSWORD=pass
FILE=/u01/flatringDev/ToVendor/LC7/ELD/ELD.7

spawn /usr/bin/sftp $USER@$HOST:/Semi/KFILE-ENG
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "mkdir ELD\r"
expect "sftp>"
send "cd ELD\r"
expect "sftp>"
send "put $FILE\r"
expect "sftp>"
send "bye\r"

如果有人知道解决方案,请告诉我。谢谢

【问题讨论】:

  • 您的问题是发送许多文件 还是只在客户端使用ftp?如果以后你怎么能产生 sftp ?
  • 它正在向远程服务器发送许多文件。

标签: bash unix sftp


【解决方案1】:
#!/bin/bash

HOST=ftpabc.abc.cn
PORT=22
USER=user
PASSWORD=pass
declare -a arry
arry=('file1' 'file2' 'file3' 'file4' '/tmp/kl.pl') # list files with absolute path eg: /tmp/kl.pl
#for FILE in $(ls -1 DIR_containing_files) # If you can all files in same dir, else create an array with elements as filenames with absoulte path and use 2nd for statement

for FILE in ${arry[*]} 
do

spawn /usr/bin/sftp $USER@$HOST:/Semi/KFILE-ENG
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "mkdir ELD\r"
expect "sftp>"
send "cd ELD\r"
expect "sftp>"
send "put $FILE\r"
expect "sftp>"
send "bye\r"

sleep 5

done

检查这是否有效,您可以让bash function 完成所有expect 的事情并在for loop 中调用它 或期望脚本文件并在bash for中调用它

【讨论】:

  • 感谢您的回复。该代码不起作用..它说“ls:找不到命令”
  • ls not found 很奇怪。无论如何,您可以尝试代码中给出的第二个for 循环。为此修改了答案。
猜你喜欢
  • 2021-12-27
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 2014-09-16
  • 1970-01-01
  • 2019-05-20
  • 2013-10-17
相关资源
最近更新 更多