创建一个 bash 文件用于通过 tftp 传输文件

Create a bash file for transfer file through tftp

我想创建 shell 脚本来调用 tftp 将特定文件传输到我的本地机器

这是我想做的操作

tftp 172.2.22.2
get file1_in_remote location file2_in_local_machine
quit

但我无法让它工作,因为当第一个 tftp 命令执行时,控制将进入 tftp 提示符。它看起来像这样

tftp>

它不会接受我在 shell 脚本中给出的第二个和第三个命令。

但是当我退出 tftp 提示符时,bash 会打印一条错误消息,如

get: command not found
quit: command not found

那么我怎样才能完成它

编辑 1

tftp 173.32.52.12 -c get MyFile1.txt MyFile2.txt
tftp 173.32.52.12 -c get MyFile1.txt MyFile2.txt MyFile3.txt etc. etc.

这 2 个命令不适用于我的 ubuntu(14.0.4) 系统

我找到了解决方案。

file1_in_remote location = "/tftpboot/file1.txt"
file2_in_local_machine = "/home/file2.txt"

tftp 172.2.22.2 << !
#file names can be stored in varible
get ${file1_in_remote location} ${file2_in_local_machine}     
quit
!

这对我有用

还有一个解决方案

tftp 172.2.22.2 << 'EOF'
#explictely specify the file names. varibales wont accept here.
get /tftpboot/file1.txt /home/file2.txt  
quit
EOF

但在第二种解决方案中,您必须明确给出文件位置。在第一个解决方案中,文件名可以存储在变量中,我们可以在“get”命令中使用该变量。

你可以使用 atftp

安装:

sudo apt-get install atftp

使用:

atftp 172.2.22.2 -g -r /remote/file.txt -l /tmp/local.txt

tftp 是一个微小的 ftp 变体,此客户端协议有多种实现。 tftp-hpa 包有一个命令选项 (-c),可以将其作为脚本中的一行。在 Ubuntu 18.04:

上测试
apt install tftp-hpa
tftp 172.2.22.2 -c get /tftpboot/file1.txt /home/file2.txt

另一个单行选项:

printf "bin\nget file.txt\n" | tftp 172.2.22.2