Macos – Is it possible to TFTP files via shell script in OS X

macosshelltftpunix

I'm working on migrating my development environment from Windows to OS X and have run across a problem with TFTP. I use TFTP to upload files to an embedded network device, this is the only option to get the files on the device (no SCP support is provided).

Every time I build firmware for this device, there's a series of three files that need to be uploaded to it. On Windows I had some simple scripts that would upload all of the files I needed in one shot which makes deploying everything much simpler. These scripts were easy because the Windows TFTP client is run in one-line commands like this:

tftp -i <Server IP Address> PUT <Local Filename> <Server Filename>

It doesn't seem that there's any way to upload a file with one command like this on OS X. Instead, you have to open the tftp program and execute a series of commands that looks something like this:

$ tftp
tftp> connect <Server IP Address>
tftp> mode binary
tftp> put <Local Filename> <Server Filename>
tftp> quit

So far I have not had any luck in figuring out how to write a script to do this, although I don't have a ton of experience with Unix shell scripting. Has anyone tried to do anything similar and come up with any solutions?

Best Answer

You should be able to use a "here document" to feed commands to the tftp program in a script:

#!/bin/sh

tftp <<EOF
connect <server IP Address>
mode binary
put <Local Filename 1> <Server Filename 1>
put <Local Filename 2> <Server Filename 2>
put <Local Filename 3> <Server Filename 3>
quit
EOF
Related Question