<p>Hello all,</p>
<p>Though i've been a developer for ~12 years now, i'm new to Golang. I'm currently writing a small tool to help automate some developer environment stuff for the company. </p>
<p>I've had no problems creating a new *ssh.Client, but I'd like to automatically reconnect in the event of a network timeout. I know I can check against ssh.Conn to see if an initial connection has been made but that doesn't help with timeouts. I figure I can also check for errors on read/write then reconnect if needed, but I feel like there has to be an easier way to check if the connection still exists.</p>
<p>A few of the examples I came across on StackOverflow suggested reading a single byte and if that didn't return an error, you'd know the connection was still up. But that seemed odd to me as I wouldn't want to take a byte off the stream if something else ended up actually needing it.</p>
<p>Thanks all!</p>
<hr/>**评论:**<br/><br/>davecheney: <pre><p>I believe that the ssh protocol, and the go client support keep alive messages. Any error you get from the client should be considered fatal.</p></pre>pappogeomys: <pre><p>The ssh client does handle keepalive requests from the server, but doesn't implement the client side automatically. The library intended for users to send these requests manually using a global SendRequest.</p>
<p>IIRC you send an empty global request with any request-name that the server doesn't recognize (and I <em>think</em> you wait on the reply, but please try it out.)</p>
<pre><code>ok, _, err := conn.SendRequest("keepalive@golang.org", true, nil)
</code></pre></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传