engine: resources: Add a tftp server and tftp file resource
This adds a tftp server and tftp file resource to help you run a small pure golang tftp server embedded inside the mgmt resource model.
This commit is contained in:
44
examples/lang/tftp0.mcl
Normal file
44
examples/lang/tftp0.mcl
Normal file
@@ -0,0 +1,44 @@
|
||||
$root = "/tmp/tftproot/"
|
||||
file $root {
|
||||
state => "exists",
|
||||
}
|
||||
file "${root}file0" {
|
||||
content => "i'm file0 in ${root}\n",
|
||||
state => "exists",
|
||||
}
|
||||
|
||||
tftp:server ":1069" { # by default tftp uses :69 but using :1069 avoids needing root!
|
||||
#address => ":1069", # you can override the name like this
|
||||
timeout => 60, # increase the timeout
|
||||
root => $root, # add a tftproot (optional)
|
||||
}
|
||||
|
||||
# you can add a raw file like this...
|
||||
tftp:file "/file1" {
|
||||
data => "hello, world, i'm file1 and i don't exist on disk!\n",
|
||||
}
|
||||
|
||||
# or if there's a file on disk you care about...
|
||||
$f2 = "/tmp/some_file"
|
||||
file $f2 {
|
||||
content => "i'm a cool file in /tmp\n",
|
||||
state => "exists",
|
||||
}
|
||||
|
||||
# you can point to it directly...
|
||||
tftp:file "/file2" {
|
||||
path => $f2,
|
||||
|
||||
Depend => File[$f2], # TODO: add autoedges
|
||||
}
|
||||
|
||||
# here's a file in the middle of nowhere that still works...
|
||||
tftp:file "/i/am/some/deeply/nested/file" {
|
||||
data => "how did you find me!\n",
|
||||
}
|
||||
|
||||
# and this file won't autogroup with the main tftp server
|
||||
tftp:file "/nope/noway" {
|
||||
data => "i won't be seen!\n",
|
||||
server => "someone else!", # normally we don't use this this way
|
||||
}
|
||||
50
examples/tftp/tftp_client.go
Normal file
50
examples/tftp/tftp_client.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/pin/tftp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 || len(os.Args) > 3 {
|
||||
log.Printf("Usage: ./%s [port] <filename>", os.Args[0])
|
||||
return
|
||||
}
|
||||
|
||||
port := "69" // default
|
||||
if len(os.Args) >= 3 {
|
||||
port = os.Args[1]
|
||||
}
|
||||
file := os.Args[len(os.Args)-1]
|
||||
|
||||
addr := fmt.Sprintf("127.0.0.1:%s", port)
|
||||
log.Printf("Connecting to: %s", addr)
|
||||
|
||||
c, err := tftp.NewClient(addr)
|
||||
if err != nil {
|
||||
log.Printf("Error connecting to server: %v", err)
|
||||
return
|
||||
}
|
||||
wt, err := c.Receive(file, "octet") // no idea why this is "octet"
|
||||
if err != nil {
|
||||
log.Printf("Error receiving from server: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Optionally obtain transfer size before actual data.
|
||||
if n, ok := wt.(tftp.IncomingTransfer).Size(); ok {
|
||||
log.Printf("Transfer size: %d", n)
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
n, err := wt.WriteTo(buf)
|
||||
|
||||
log.Printf("%d bytes received", n)
|
||||
log.Printf("Got: %s", buf.String())
|
||||
|
||||
log.Printf("Done!")
|
||||
}
|
||||
Reference in New Issue
Block a user