Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IZE-524 added command and pid display when the port is busy #504

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions internal/commands/tunnel_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2instanceconnect"
Expand All @@ -17,17 +29,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"
)

const sshConfig = `# SSH over Session Manager
Expand Down Expand Up @@ -379,8 +380,13 @@ func checkPort(port int) error {

l, err := net.ListenTCP("tcp", addr)
if err != nil {
logrus.Error(err)
return fmt.Errorf("port %d is not available. Please make sure there is no other process that is using the port %d", port, port)
command := fmt.Sprintf("lsof -i tcp:%d | grep LISTEN | awk '{print $1, \"(pid\", $2\")\"}'", port)
stdout, stderr, code, err := term.New(term.WithStdout(io.Discard), term.WithStderr(io.Discard)).Run(exec.Command("bash", "-c", command))
if code == 0 {
logrus.Error(err)
return fmt.Errorf("port %d is in use by %s. Please stop the process or remove the port assigment so it will be auto-assigned", port, strings.TrimSpace(stdout))
}
return fmt.Errorf("error during run command: %s (exit code: %d, stderr: %s)", command, code, stderr)
}

err = l.Close()
Expand Down