github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/port.go (about) 1 // Copyright (C) 2015 Scaleway. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE.md file. 4 5 package commands 6 7 import ( 8 "fmt" 9 10 "github.com/scaleway/scaleway-cli/pkg/api" 11 "github.com/scaleway/scaleway-cli/pkg/utils" 12 ) 13 14 // PortArgs are flags for the `RunPort` function 15 type PortArgs struct { 16 Gateway string 17 Server string 18 SSHUser string 19 SSHPort int 20 } 21 22 // RunPort is the handler for 'scw port' 23 func RunPort(ctx CommandContext, args PortArgs) error { 24 serverID, err := ctx.API.GetServerID(args.Server) 25 if err != nil { 26 return err 27 } 28 server, err := ctx.API.GetServer(serverID) 29 if err != nil { 30 return fmt.Errorf("failed to get server information for %s: %v", serverID, err) 31 } 32 33 // Resolve gateway 34 if args.Gateway == "" { 35 args.Gateway = ctx.Getenv("SCW_GATEWAY") 36 } 37 var gateway string 38 if args.Gateway == serverID || args.Gateway == args.Server { 39 gateway = "" 40 } else { 41 gateway, err = api.ResolveGateway(ctx.API, args.Gateway) 42 if err != nil { 43 return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err) 44 } 45 } 46 47 command := []string{"netstat -lutn 2>/dev/null | grep LISTEN"} 48 err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.SSHUser, args.SSHPort, command, true, gateway) 49 if err != nil { 50 return fmt.Errorf("command execution failed: %v", err) 51 } 52 53 return nil 54 }