Program panics on other machines

I’m trying to build an executable that I can give users to run on their machines that will leverage golang.org/x/crypto/ssh to ssh as themself into a remote machine.
The problem is the binary built by go build main.go works for me on CentOS 6.10 but panics for my users who are on CentOS 7.6 when they run a copy of the binary on their machines. The error message is

http: panic serving 123.456.654.321:59530: runtime error: invalid memory address or nil pointer dereference
goroutine 23 [running]:
net/http.(*conn).serve.func1(0xc00021e000)
    /home/brian/go-1.13.3/src/net/http/server.go:1767 +0x139
panic(0x733de0, 0xa30e30)
/home/brian/go-1.13.3/src/runtime/panic.go:679 +0x1b2

The problem seems to be the ssh.Dial line of my program so here’s that section of code https://play.golang.org/p/6KI5PU22QyN
I’ve tried checking for proper use of pointers and I think I’m doing it correctly.
Perhaps either I shouldn’t use go build for something that I can distrubute or perhaps the net or ssh package expects to be running on the machine it was built on? I thought as long as I built on Linux x64 I could distribute to Linux x64 without problems.

2 Likes

I tested your code, and it appears that client is nil in my case. Executing defer client.Close() cause a segmentation violation. Changing your code to the following fixes the issue:

client, err := ssh.Dial("tcp", remoteHostname+":22", sshConfig)
fatalIfErr(err)
defer client.Close()   

You should test the error returned by ssh.Dial. When I run this code, I get the error message:

2019/10/22 09:28:14 ssh: handshake failed: knownhosts: key is unknown

This error is because the key of my host is not in the knownhosts file. If I execute ssh on the command line, I’m invited to add the key to the knownhosts file. If I accept and rerun the program, I get the error:

2019/10/22 09:32:04 ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

This is because I didn’t add the public key in the authorized_keys file yet. Once I add it, there is no more error, and the connection succeeds.

I can’t reproduce the panic message you show us. I doubt it is related to ssh because it refers to http server.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.