Skip to content

Commit 5c4b283

Browse files
facchinmmatteosuppo
authored andcommitted
Fix SSH command execution
ATTENTION: the commandline signature is not checked at the moment! example payload: ``` {"board":"arduino:avr:yun","port":"10.130.22.105","commandline":"wget {upload.verbose} -O /tmp/arduino_connector && wget https://raw.githubusercontent.com/xively/mosquitto/master/test/ssl/mosquitto.org.crt -O ~/mosquitto.org.crt && mv /tmp/arduino* ~ && chmod +x ~/arduino_connector && ~/arduino_connector","signature":null,"hex":"","filename":"Blink.hex","extra":{"auth":{"password":"user", "username":"user"},"wait_for_upload_port":false,"use_1200bps_touch":false,"network":true,"params_verbose":"http://10.130.22.222/arduino_connector","params_quiet":null,"verbose":true, "SSH":true}} ```
1 parent 25dbf81 commit 5c4b283

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

conn.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/arduino/arduino-create-agent/upload"
2222
"github.com/arduino/arduino-create-agent/utilities"
2323
"github.com/gin-gonic/gin"
24-
"github.com/googollee/go-socket.io"
24+
socketio "github.com/googollee/go-socket.io"
2525
)
2626

2727
type connection struct {
@@ -72,6 +72,7 @@ type Upload struct {
7272
var uploadStatusStr string = "ProgrammerStatus"
7373

7474
func uploadHandler(c *gin.Context) {
75+
7576
data := new(Upload)
7677
c.BindJSON(data)
7778

@@ -115,8 +116,12 @@ func uploadHandler(c *gin.Context) {
115116
return
116117
}
117118

119+
var filePaths []string
120+
filePaths = append(filePaths, filePath)
121+
118122
for _, extraFile := range data.ExtraFiles {
119123
path := filepath.Join(filepath.Dir(filePath), extraFile.Filename)
124+
filePaths = append(filePaths, path)
120125
log.Printf("Saving %s on %s", extraFile.Filename, path)
121126
err := ioutil.WriteFile(path, extraFile.Hex, 0644)
122127
if err != nil {
@@ -141,7 +146,7 @@ func uploadHandler(c *gin.Context) {
141146
// Upload
142147
if data.Extra.Network {
143148
send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Network"})
144-
err = upload.Network(data.Port, data.Board, filePath, commandline, data.Extra.Auth, l, data.Extra.SSH)
149+
err = upload.Network(data.Port, data.Board, filePaths, commandline, data.Extra.Auth, l, data.Extra.SSH)
145150
} else {
146151
send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Serial"})
147152
err = upload.Serial(data.Port, commandline, data.Extra, l)

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/arduino/arduino-create-agent/tools"
1818
"github.com/arduino/arduino-create-agent/utilities"
1919
"github.com/gin-gonic/gin"
20-
"github.com/itsjamie/gin-cors"
20+
cors "github.com/itsjamie/gin-cors"
2121
"github.com/kardianos/osext"
2222
"github.com/vharitonsky/iniflags"
2323
//"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily

upload/upload.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
shellwords "github.com/mattn/go-shellwords"
2121
"github.com/pkg/errors"
2222
"github.com/sfreiberg/simplessh"
23-
"go.bug.st/serial.v1"
23+
serial "go.bug.st/serial.v1"
2424
)
2525

2626
// Busy tells wether the programmer is doing something
@@ -81,7 +81,7 @@ func fixupPort(port, commandline string) string {
8181
}
8282

8383
// Network performs a network upload
84-
func Network(port, board, file, commandline string, auth Auth, l Logger, SSH bool) error {
84+
func Network(port, board string, files []string, commandline string, auth Auth, l Logger, SSH bool) error {
8585
Busy = true
8686

8787
// Defaults
@@ -94,11 +94,11 @@ func Network(port, board, file, commandline string, auth Auth, l Logger, SSH boo
9494

9595
commandline = fixupPort(port, commandline)
9696

97-
// try with a form
98-
err := form(port, board, file, auth, l)
97+
// try with ssh
98+
err := ssh(port, files, commandline, auth, l, SSH)
9999
if err != nil {
100-
// try with ssh
101-
err = ssh(port, file, commandline, auth, l, SSH)
100+
// fallback on form
101+
err = form(port, board, files[0], auth, l)
102102
}
103103

104104
Busy = false
@@ -365,7 +365,7 @@ func form(port, board, file string, auth Auth, l Logger) error {
365365
return nil
366366
}
367367

368-
func ssh(port, file, commandline string, auth Auth, l Logger, SSH bool) error {
368+
func ssh(port string, files []string, commandline string, auth Auth, l Logger, SSH bool) error {
369369
// Connect via ssh
370370
client, err := simplessh.ConnectWithPassword(port+":22", auth.Username, auth.Password)
371371
debug(l, "Connect via ssh ", client, err)
@@ -374,24 +374,29 @@ func ssh(port, file, commandline string, auth Auth, l Logger, SSH bool) error {
374374
}
375375
defer client.Close()
376376

377-
if !SSH {
378-
// Copy the sketch
379-
err = client.Upload(file, "/tmp/sketch"+filepath.Ext(file))
380-
debug(l, "Copy the sketch ", err)
377+
// Copy the sketch
378+
for _, file := range files {
379+
fileName := "/tmp/sketch" + filepath.Ext(file)
380+
if SSH {
381+
// don't rename files
382+
fileName = "/tmp/" + filepath.Base(file)
383+
}
384+
err = client.Upload(file, fileName)
385+
debug(l, "Copy "+filepath.Ext(file), err)
381386
if err != nil {
382387
return errors.Wrapf(err, "Copy sketch")
383388
}
389+
}
384390

385-
// very special case for Yun (remove once AVR boards.txt is fixed)
386-
if commandline == "" {
387-
commandline = "merge-sketch-with-bootloader.lua /tmp/sketch.hex && /usr/bin/run-avrdude /tmp/sketch.hex"
388-
}
391+
// very special case for Yun (remove once AVR boards.txt is fixed)
392+
if commandline == "" {
393+
commandline = "merge-sketch-with-bootloader.lua /tmp/sketch.hex && /usr/bin/run-avrdude /tmp/sketch.hex"
389394
}
390395

391396
// Execute commandline
392397
output, err := client.Exec(commandline)
393398
info(l, output)
394-
debug(l, "Execute commandline ", commandline, output, err)
399+
debug(l, "Execute commandline ", commandline, string(output), err)
395400
if err != nil {
396401
return errors.Wrapf(err, "Execute commandline")
397402
}

0 commit comments

Comments
 (0)