Skip to content

Commit c5bea04

Browse files
giulio93Giuliolucarin91
authored
Add function/api to enable ssh (for network mode) (#649)
Co-authored-by: Giulio <giulio@Giulios-MacBook-Pro.local> Co-authored-by: Luca Rinaldi <lucarin@protonmail.com>
1 parent a88abc9 commit c5bea04

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

cmd/arduino-app-cli/board/board.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func NewBoardCmd() *cobra.Command {
6666
fsCmd.AddCommand(newBoardListCmd())
6767
fsCmd.AddCommand(newBoardSetName())
6868
fsCmd.AddCommand(newSetPasswordCmd())
69+
fsCmd.AddCommand(newEnableNetworkModeCmd())
70+
fsCmd.AddCommand(newDisableNetworkModeCmd())
71+
fsCmd.AddCommand(newNetworkModeStatusCmd())
6972

7073
return fsCmd
7174
}
@@ -189,3 +192,58 @@ func newSetPasswordCmd() *cobra.Command {
189192
},
190193
}
191194
}
195+
196+
func newEnableNetworkModeCmd() *cobra.Command {
197+
return &cobra.Command{
198+
Use: "enable-ssh",
199+
Short: "Enable and start the SSH service on the board",
200+
Args: cobra.ExactArgs(0),
201+
RunE: func(cmd *cobra.Command, args []string) error {
202+
conn := cmd.Context().Value(remoteConnKey).(remote.RemoteConn)
203+
204+
if err := board.EnableNetworkMode(cmd.Context(), conn); err != nil {
205+
return fmt.Errorf("failed to enable SSH: %w", err)
206+
}
207+
208+
feedback.Printf("SSH service enabled and started\n")
209+
return nil
210+
},
211+
}
212+
}
213+
214+
func newDisableNetworkModeCmd() *cobra.Command {
215+
return &cobra.Command{
216+
Use: "disable-ssh",
217+
Short: "Disable and stop the SSH service on the board",
218+
Args: cobra.ExactArgs(0),
219+
RunE: func(cmd *cobra.Command, args []string) error {
220+
conn := cmd.Context().Value(remoteConnKey).(remote.RemoteConn)
221+
222+
if err := board.DisableNetworkMode(cmd.Context(), conn); err != nil {
223+
return fmt.Errorf("failed to disable SSH: %w", err)
224+
}
225+
226+
feedback.Printf("SSH service disabled and stopped\n")
227+
return nil
228+
},
229+
}
230+
}
231+
232+
func newNetworkModeStatusCmd() *cobra.Command {
233+
return &cobra.Command{
234+
Use: "status-ssh",
235+
Short: "Check the status of the network mode on the board",
236+
Args: cobra.ExactArgs(0),
237+
RunE: func(cmd *cobra.Command, args []string) error {
238+
conn := cmd.Context().Value(remoteConnKey).(remote.RemoteConn)
239+
240+
isEnabled, err := board.NetworkModeStatus(cmd.Context(), conn)
241+
if err != nil {
242+
return fmt.Errorf("failed to check network mode status: %w", err)
243+
}
244+
245+
feedback.Printf("Network mode is %s\n", map[bool]string{true: "enabled", false: "disabled"}[isEnabled])
246+
return nil
247+
},
248+
}
249+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arduino ALL=(ALL) NOPASSWD: /usr/sbin/dpkg-reconfigure openssh-server, /usr/bin/systemctl enable ssh, /usr/bin/systemctl start ssh, /usr/bin/systemctl disable ssh, /usr/bin/systemctl stop ssh

pkg/board/board.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package board
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"log/slog"
10+
"os/exec"
911
"regexp"
1012
"slices"
1113
"strings"
@@ -300,6 +302,60 @@ func SetUserPassword(ctx context.Context, conn remote.RemoteConn, newPass string
300302
return nil
301303
}
302304

305+
func EnableNetworkMode(ctx context.Context, conn remote.RemoteConn) error {
306+
if err := conn.GetCmd("sudo", "dpkg-reconfigure", "openssh-server").Run(ctx); err != nil {
307+
return fmt.Errorf("failed to reconfigure openssh-server: %w", err)
308+
}
309+
310+
if err := conn.GetCmd("sudo", "systemctl", "enable", "ssh").Run(ctx); err != nil {
311+
return fmt.Errorf("failed to enable ssh service: %w", err)
312+
}
313+
314+
if err := conn.GetCmd("sudo", "systemctl", "start", "ssh").Run(ctx); err != nil {
315+
return fmt.Errorf("failed to start ssh service: %w", err)
316+
}
317+
318+
return nil
319+
}
320+
321+
func NetworkModeStatus(ctx context.Context, conn remote.RemoteConn) (bool, error) {
322+
err := conn.GetCmd("systemctl", "is-enabled", "ssh").Run(ctx)
323+
if err != nil {
324+
var exitErr *exec.ExitError
325+
if errors.As(err, &exitErr) {
326+
if exitErr.ExitCode() != 0 {
327+
return false, nil
328+
}
329+
}
330+
return false, fmt.Errorf("failed to check ssh service status: %w", err)
331+
}
332+
333+
err = conn.GetCmd("systemctl", "is-active", "ssh").Run(ctx)
334+
if err != nil {
335+
var exitErr *exec.ExitError
336+
if errors.As(err, &exitErr) {
337+
if exitErr.ExitCode() != 0 {
338+
return false, nil
339+
}
340+
}
341+
return false, fmt.Errorf("failed to check ssh service status: %w", err)
342+
}
343+
344+
return true, nil
345+
}
346+
347+
func DisableNetworkMode(ctx context.Context, conn remote.RemoteConn) error {
348+
if err := conn.GetCmd("sudo", "systemctl", "disable", "ssh").Run(ctx); err != nil {
349+
return fmt.Errorf("failed to disable ssh service: %w", err)
350+
}
351+
352+
if err := conn.GetCmd("sudo", "systemctl", "stop", "ssh").Run(ctx); err != nil {
353+
return fmt.Errorf("failed to stop ssh service: %w", err)
354+
}
355+
356+
return nil
357+
}
358+
303359
func getSerial(conn remote.RemoteConn) (string, error) {
304360
f, err := conn.ReadFile(SerialPath)
305361
if err != nil {

0 commit comments

Comments
 (0)