session.Run results in ssh.channelEOFMsg

190 views
Skip to first unread message

Jonathan Pittman

unread,
Dec 12, 2011, 4:00:46 PM12/12/11
to golang-nuts, golan...@googlegroups.com
Following the example client code for exp/ssh, I am getting this message after doing a session.Run("echo hi").

code...

session1, err := client.NewSession()
if err != nil {
        log.Fatal("Error creating session1: ", err)
}
log.Println("Session1 created.")

if err := session1.Run(*cmdString); err != nil {
        log.Fatal("Failed to Run: " + err.Error())
}
reader := bufio.NewReader(session1.Stdin)
line, _, _ := reader.ReadLine()
fmt.Println(line)
session1.Close()

cmdString in this case comes from flag.String().  This is the error message:

Failed to Run: wait: unexpected packet *ssh.channelEOFMsg received: &{0}

On the server side, it appears to accept authentication and shows the exec of the command.  I also ran this using a typical openssh client as well.

Using Go client...

sshd[13020]: Set /proc/self/oom_adj to 0
sshd[13020]: Connection from 192.168.1.101 port 45531
sshd[13020]: Postponed publickey for root from 192.168.1.101 port 45531 ssh2 [preauth]
sshd[13020]: Accepted publickey for root from 192.168.1.101 port 45531 ssh2
sshd[13020]: Found matching RSA key ...
sshd[13020]: Exec command 'echo hi'
sshd[13020]: Connection closed by 192.168.1.101
sshd[13020]: Transferred: sent 2600, received 2512 bytes
sshd[13020]: Closing connection to 192.168.1.101 port 45531

Using OpenSSH client...

sshd[12825]: Set /proc/self/oom_adj to 0
sshd[12825]: Connection from 192.168.1.101 port 36182
sshd[12825]: Postponed publickey for root from 192.168.1.101 port 36182 ssh2 [preauth]
sshd[12825]: Accepted publickey for root from 192.168.1.101 port 36182 ssh2
sshd[12825]: Found matching RSA key ...
sshd[12825]: Exec command 'echo hi'
sshd[12825]: Received disconnect from 192.168.1.101: 11: disconnected by user

Anyone else run into this or have an idea as to what is happening here?  (fwiw, I have tried commands other than "echo hi")

Dave Cheney

unread,
Dec 12, 2011, 4:07:58 PM12/12/11
to Jonathan Pittman, golang-nuts, golan...@googlegroups.com
Hi Jonathan,

Which release/revision is this?

Cheers

Dave

Sent from my iPhone

Jonathan Pittman

unread,
Dec 12, 2011, 4:14:15 PM12/12/11
to Dave Cheney, golang-nuts, golan...@googlegroups.com
6g version weekly.2011-12-06 10712

It was doing it with the previous release (weekly.2011-12-02) as well.  I am not sure if it was doing it further back than that.  I just got to this point recently after learning to dance with the ssh-agent.  FWIW, for these tests, I am still using a private key on disk.

Dave Cheney

unread,
Dec 12, 2011, 4:19:55 PM12/12/11
to Jonathan Pittman, golang-nuts, golan...@googlegroups.com
Sadly, the example code in doc.go is out of date wrt +tip. I think you've found a bug but you should try this code none the less.

To read from stdout on the remote process either call StdoutPipe() on the session or set session.Stdout to an io.Writer to collect the output. A bytes.Buffer is pretty good for this, especially with its String() method. 

See if that helps, looking at the difference between go and openssh client, it looks like the server is closing the connection on the go client prematurely.  

Cheers

Dave

Sent from my iPhone

On 13/12/2011, at 8:00, Jonathan Pittman <jonathan.m...@gmail.com> wrote:

Dave Cheney

unread,
Dec 12, 2011, 4:20:53 PM12/12/11
to Jonathan Pittman, golang-nuts, golan...@googlegroups.com
Thanks, you're able to call NewSession so this is not an authentication problem. 

Sent from my iPhone

Jonathan Pittman

unread,
Dec 12, 2011, 4:57:02 PM12/12/11
to Dave Cheney, golang-nuts, golan...@googlegroups.com
Indeed...  I am able to get the output this way.

session, err := client.NewSession()
if err != nil {
        log.Fatal("Error creating session: ", err)
}
log.Println("Session created.")

myBuf := new(bytes.Buffer)
session.Stdout = myBuf

if err := session.Run(*cmdString); err != nil {
        log.Println("Failed to Run: " + err.Error())
}

fmt.Println(myBuf.String())
session.Close()

As for the "unexpected packet" error, is that a bug or something that should be handled differently?  Does the fact that the error is an EOF simply mean it should be handled differently by me or by the code at http://weekly.golang.org/src/pkg/exp/ssh/session.go?h=unexpected+packet#L277 ?

Dave Cheney

unread,
Dec 13, 2011, 2:00:45 AM12/13/11
to Jonathan Pittman, golang-nuts, golan...@googlegroups.com
My guess is it shouldn't happen at all. Please let me look into it further this evening. 

Sent from my iPhone

Jonathan Pittman

unread,
Dec 13, 2011, 11:34:26 AM12/13/11
to Dave Cheney, golang-nuts, golan...@googlegroups.com
Thanks for you help on this!

Dave Cheney

unread,
Dec 13, 2011, 3:18:59 PM12/13/11
to Jonathan Pittman, golang-nuts
Not a problem. So far I haven't been able to generate the
unexpectedEOF error, but I can easily generate an eof with this
original code

r := buifio.NewReader(session1.Stdin)

this is because, unless set session1.Stdin is nil.

I'll need to do more digging to reproduce the problem.

Cheers

Dave

Jonathan Pittman

unread,
Dec 13, 2011, 9:43:16 PM12/13/11
to Dave Cheney, golang-nuts
Most of the time, I was getting the EOF before ever getting to the...

reader := bufio.NewReader(session1.Stdin)

...part of the code.  I found it to be hit and miss, but easily reproducible on my end.  I found it happened more often than not, but it would go back and forth.

Currently I am ignoring the specific error (matching the string) and using the bytes.Buffer as you suggested.  Results are making it through just fine.  I'll remove my little hack once things are squared away.
Reply all
Reply to author
Forward
0 new messages